Coder Perfect

Using the command line, convert xlsx to csv in Linux.

Problem

On Linux, I’m looking for a solution to convert xlsx to csv files.

I don’t want to use PHP/Perl or anything similar because I’ll be processing millions of lines, so I’ll need something rapid. I found a tool called xls2csv in the Ubuntu repositories, however it only converts xls (Office 2003) files (which I now use), and I need compatibility for newer Excel files.

Any ideas?

Asked by user1390150

Solution #1

The spreadsheet application Gnumeric has a command-line utility called ssconvert that can convert between a number of spreadsheet formats:

$ ssconvert Book1.xlsx newfile.csv
Using exporter Gnumeric_stf:stf_csv

$ cat newfile.csv 
Foo,Bar,Baz
1,2,3
123.6,7.89,
2012/05/14,,
The,last,Line

To install on Ubuntu, follow these steps:

apt-get install gnumeric

To install on a Mac, follow these steps:

brew install gnumeric

Answered by jmcnamara

Solution #2

This is something LibreOffice can help you with:

libreoffice --headless --convert-to csv $filename --outdir $outdir

You might need to run this with sudo for reasons I don’t understand. By adding this line to your sudoers file, you can make LibreOffice work with sudo without requiring a password:

users ALL=(ALL) NOPASSWD: libreoffice

Answered by spiffytech

Solution #3

Gnumeric / LibreOffice would probably function fine if you already have a desktop environment, but on a headless server (such as Amazon Web Services), they require dozens of dependencies that you must additionally install.

This Python alternative was discovered:

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 provides connections to Bash, Python, Ruby, and Java-based alternatives.

Answered by andrewtweber

Solution #4

To convert all of my xlsx files in the current directory in bash, I used the libreoffice command:

for i  in *.xlsx; do  libreoffice --headless --convert-to csv "$i" ; done

Close all your Libre Office open instances before executing, or it will fail silently.

Spaces in the filename are handled by the command.

After a few years, I tried again, but it didn’t work. Although there are various suggestions in this discussion, the simplest answer was to run as root (or running a sudo libreoffice). It’s not elegant, but it’s quick.

In Windows, run the command scalc.exe.

Answered by neves

Solution #5

Use csvkit

in2csv data.xlsx > data.csv

Check out their fantastic documents for more information.

Answered by Holger Brandl

Post is based on https://stackoverflow.com/questions/10557360/convert-xlsx-to-csv-in-linux-with-command-line