I\'m looking for a way to convert xlsx files to csv files on Linux.
I do not want to use PHP/Perl or anything like that since I\'m looking at processing several mill
As others said, libreoffice
can convert xls files to csv. The problem for me was the sheet selection.
This libreoffice Python script does a fine job at converting a single sheet to CSV.
Usage is:
./libreconverter.py File.xls:"Sheet Name" output.csv
The only downside (on my end) is that --headless
doesn't seem to work. I have a LO window that shows up for a second and then quits.
That's OK with me, it's the only tool that does the job rapidly.
If you already have a Desktop environment then I'm sure Gnumeric / LibreOffice would work well, but on a headless server (such as Amazon Web Services), they require dozens of dependencies that you also need to install.
I found this Python alternative:
https://github.com/dilshod/xlsx2csv
$ easy_install xlsx2csv
$ xlsx2csv file.xlsx > newfile.csv
Took 2 seconds to install and works like a charm.
If you have multiple sheets you can export all at once, or one at a time:
$ xlsx2csv file.xlsx --all > all.csv
$ xlsx2csv file.xlsx --all -p '' > all-no-delimiter.csv
$ xlsx2csv file.xlsx -s 1 > sheet1.csv
He also links to several alternatives built in Bash, Python, Ruby, and Java.
If .xlsx
file has many sheets, -s
flag can be used to get the sheet you want. For example:
xlsx2csv "my_file.xlsx" -s 2 second_sheet.csv
second_sheet.csv
would contain data of 2nd sheet in my_file.xlsx
.
You can do this with LibreOffice:
libreoffice --headless --convert-to csv $filename --outdir $outdir
For reasons not clear to me, you might need to run this with sudo. You can make LibreOffice work with sudo without requiring a password by adding this line to you sudoers file:
users ALL=(ALL) NOPASSWD: libreoffice