Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python convert Excel File (xls or xlsx) to/from ODS

I've been scouring the net to find a Python library or tool that can converts an Excel file to/from ODS format, but haven't been able to come across anything.

I need the ability to input and output data in either format. We don't need to worry about merged cells, formulas or anything non-straightforward.

like image 534
Ashwin Balamohan Avatar asked Mar 06 '13 20:03

Ashwin Balamohan


People also ask

Can openpyxl read XLS files?

openpyxl is a Python library to read/write Excel 2010 xlsx/xlsm/xltx/xltm files.

How to convert XLS to xlsx in Python?

To convert xls to xlsx: $ pip install pyexcel pyexcel-xls pyexcel-xlsx. Run Python Script as: import pyexcel as p p.save_book_as(file_name='your-file-in.xls', dest_file_name='your-new-file-out.xlsx') If you do not need a program, you could install one additinal package pyexcel-cli::

How to read the ODS files with Python?

Before reading the ODS files with Python we need to install additional packages like: odfpy + Pandas (if not installed or upgrade to latest). To install odfpy + Pandas use next commands: Now we can read the first sheet of the worksheet by calling Pandas method read_excel ():

Is there a way to automate excel with Python?

You didn't say which platform you are using, but if it's Windows or Mac, and you have Excel installed, then the most straightforward way to automate Excel is probably xlwings. In principle this will allow you to use Python to open the .xlsx file in Excel (an actual, running instance of Microsoft Excel) and do "save as" to a .xls file.

How to convert XLS to xlsx and vice versa?

To convert xls to xlsx: If you do not need a program, you could install one additinal package pyexcel-cli:: The transcoding procedure above uses xlrd and openpyxl. 2. To convert between xlsx and xls vice and versa: You need to have win32com installed on your machine.


1 Answers

If you have libreoffice installed, you can do a python execution wrapper around its headless mode:

$ /usr/bin/libreoffice --headless --invisible -convert-to ods /home/cwgem/Downloads/QTL_Sample_data.xls 
convert /home/cwgem/Downloads/QTL_Sample_data.xls -> /home/cwgem/QTL_Sample_data.ods using OpenDocument Spreadsheet Flat XML
$ /usr/bin/libreoffice --headless --invisible -convert-to xls /home/cwgem/QTL_Sample_data.ods 
convert /home/cwgem/QTL_Sample_data.ods -> /home/cwgem/QTL_Sample_data.xls using

Which would be a bit easier than trying to do it through the library route.

like image 61
cwgem Avatar answered Sep 20 '22 02:09

cwgem