I would like to split a spreadsheet (ods or xlsx) into multiple csv files, one for each sheet in the spreadsheet.
I would like to do this without launching a graphical app and preferably in a one liner.
Any ideas?
Though the linked duplicate provides a possible solution in one of the answers (not in the accepted one) and was helpful. The solution does not work with ODS files as I requested, and I consider the question to be sufficiently different.
Well,
libreoffice
can convert documents from a script (i.e. in headless mode without opening a GUI). To convert any spreadsheet format to CSV, its simplest form would look like e.g.However, this just takes the first sheet of your document and converts that, ignoring all others. It also lacks an option to select the sheet to convert, sadly.
So we're going to need an external tool, like xlsx2csv. It's an open source Python (both 2 and 3) script that converts XLSX files to CSV, and supports extracting all sheets into separate files.
Ubuntu already comes with Python installed, but maybe you need to install
pip
first, its package manager. I'm going for Python 3, but you could change all commands below to run it with 2 as well:Then you can install
xlsx2csv
withpip3
into your user's package directory, usingAfter that, the executable script can be found in
~/.local/bin/xlsx2csv
.Now if you don't have it in XLSX format already, let's convert that ODS spreadsheet with
libreoffice
:Then we use
xlsx2csv
to extract all sheets. It will create a folderOUTPUTFOLDER
and place all extractedSHEETNAME.csv
in there:Using
xls2csv
(to convert .XLS) tool ofcatdoc
package installsudo apt install catdoc
:This
xls2csv -b ' ' EXCEL.xls
reads MS-Excel file and puts its content as comma-separated data on standard output and with-b STRING
we are telling sheets to delimited with what characters (or string; which we defined a actual newline here).Use
xlsx2csv
(to convert .XLSX); installsudo apt install xlsx2csv
:The
-s 0
means print all sheets.Take a look at
\thanks{Bruni}
-O 'separator= format= ...'
is used to control the csv format details-S
to create a different output file for each sheet