How to convert xlsx files to xls or csv format on Linux

The xlsx file name extension is a new file format based on XML, used by Microsoft Office Excel 2007 spreadsheet application. The letter "x" appended to the existing "xls" extension used by earlier versions of Excel, signifies that the format is based on XML. If you would like to open or edit an xlsx file created by Excel 2007, on an earlier version of Excel or any other spreadsheet application, you need to convert xlsx files first.

There are four different ways you can convert xlsx files on Linux. The first method is to use Gnumeric. Gnumeric is a free spreadsheet program which can import or export data in different file formats such as CSV, .xls and .xlsx. Gnumeric comes with a command-line utility called ssconvert which can convert between different spreadsheet file formats. In order to convert xls files to xls or csv format by using Gnumeric, first install Gnumeric. Once Gnumeric is installed, you can use ssconvert.

$ sudo apt-get install gnumeric

Note that ssconvert accepts arguments in the following format.

$ ssconvert --export-type=ID [input file] [output file]

In the above, "--export-type" is optional, and without "--export-type", ssconvert can infer export type from the extension of output file. If you want to explicitly specify export type, ID can be one of the following.

  • "Gnumeric_Excel:excel_dsf" : Microsoft Excel 97/2000/XP, 5.0/95 format
  • "Gnumeric_Excel:xlsx" : Microsoft Excel 2007
  • "Gnumeric_pdf:pdf_assistant" : Portable Document Format (PDF)
  • "Gnumeric_stf:stf_csv" : Comma Separated Values (CSV)

So if you would like to convert xlsx files to csv format using ssconvert:

$ ssconvert input.xlsx output.csv
$ ssconvert --export-type=Gnumeric_stf:stf_csv input.xls output.txt

The drawback of the above method of converting xlsx files using Gnumeric is that you need to install Gnumeric which may be too bloated software to install just for file conversion. A more lightweight way is to use xlsx2csv which is a python tool for xlsx to csv conversion.

$ git clone https://github.com/dilshod/xlsx2csv.git
$ cd xlsx2csv
$ ./xlsx2csv.py input.xlsx output.csv

Besides Gnumeric and xlsx2csv, you can also use OpenOffice to perform xlsx format conversions. OpenOffice comes with a command-line utility called unoconv which can convert .xlsx files.

$ sudo apt-get install unoconv
$ unoconv -f csv input.xlsx

Note that once you install unoconv using apt-get, it will install OpenOffice package as well.

The final method of converting xlsx files is to use Google Spreadsheets. Google Spreadsheets can import Excel 2007 files. Thus, all you have to do is to import your .xlsx file on to Google Docs Spreadsheet, click "Download as" menu, and choose an appropriate file format: Excel (.xls), PDF, CSV, Text. Note that document files which can be uploaded to Google Docs for conversion cannot be larger than 2MB.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

6 thoughts on “How to convert xlsx files to xls or csv format on Linux

  1. How do we convert large csv files to xls? gnumeric cannot convert more than about 65580 lines to xls at the same time. The csv file has to be split then converted. Do we have an efficient way to do it?

    • A simply python code (based on XlsxWriter) will convert csv to xls:

      import os
      import glob
      import csv
      from xlsxwriter.workbook import Workbook
      
      for csvfile in glob.glob(os.path.join('.', '*.csv')):
          workbook = Workbook(csvfile + '.xlsx')
          worksheet = workbook.add_worksheet()
          with open(csvfile, 'rb') as f:
              reader = csv.reader(f)
              for r, row in enumerate(reader):
                  for c, col in enumerate(row):
                      worksheet.write(r, c, col)
          workbook.close()
      
      

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *