Convert XLSX to CSV in Linux with Command Line in Linux

LinuxOpen SourceOperating System

XLSX file is a standard file extension for the modern Microsoft Excel spreadsheet. We use of these files to analyze and organize data and they usually contain numerical data separated by rows and columns within a cell.

On the other hand, a CSV file is basically a delimited text file that uses a comma to separate values, and each line of this file is usually a data record.

While both the xlsx and csv formats of storing data are way different, as one makes use of tables with rows and columns, while the other uses commas to separate the values, but that doesn’t mean we can’t convert one into the other.

There are several online tools that one can use, but when it comes to converting a xlsx into a csv using the linux terminal, it’s a little more complicated than uploading the file somewhere and getting the csv as an output.

There are different command line applications that we can make use to do the same, I’ll show two of the most commonly used applications that help us to convert the xlsx into csv using the linux command line.

The first one is the gnumeric spreadsheet application. The gnumeric spreadsheet application comes with a command line utility called ssconvert that we will make use of to convert the xlsx into a csv.

In order to use the ssconvert, we first need to install the gnumeric spreadsheet application on our local machine.

For Ubuntu

apt-get install gnumeric

For Mac

brew install gnumeric

Now we have gnumeric on our local machines, the next step is to make use of ssconvert utility and we are done.

Write the command shown below to the terminal to convert a.xlsx file into b.csv

ssconvert Sample_XLSX.xlsx Sample_CSVFile.csv

It should be noted that both these files are present in my local environment, you can simply replace them with whatever files you have on your local machine.

Output

$ssconvert Sample_XLSX.xlsx Sample_CSVFile.csv
Using exporter Gnumeric_stf:stf_csv

Now, if I check the contents of my Sample_CSVFile which was created with the help of the above command, I’ll get the following output −

immukul@192 Downloads % cat SampleCSVFile_11kb.csv
1,"Eldon Base for stackable storage shelf, platinum",Muhammed MacIntyre,3,-213.25,38.94,35,Nunavut,Storage & Organization,0.8
2,"1.7 Cubic Foot Compact ""Cube"" Office Refrigerators",BarryFrench,293,457.81,208.16,68.02,Nunavut,Appliances,0.58
3,"Cardinal Slant-D? Ring Binder, Heavy Gauge Vinyl",Barry French,293,46.71,8.69,2.99,Nunavut,Binders and Binder Accessories,0.39
4,R380,Clay Rozendal,483,1198.97,195.99,3.99,Nunavut,Telephones and Communication,0.58

You can clearly notice that records are separated by commas, and hence the file is valid.

One alternate way to achieve the above is to make use of libreoffice, in that case we just need to write the following command to the terminal to convert a xlsx file to csv.

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

The above command will convert all the files that are present in the current directory to their csv namesake.

Output

Before the command

immukul@192 cc % ls -ltr
total 40
-rw-r--r--@ 1 immukul staff 5425 Jul 3 11:48 Sample_XLSX.xlsx

After the command

immukul@192 cc % ls -ltr
total 16
-rw-r--r--@ 1 immukul staff 5425 Jul 3 11:48 Sample_XLSX.csv
raja
Published on 29-Jul-2021 11:11:05
Advertisements