How do I read/write both xlsx and xls files in Python?

2k views Asked by At

I have a web application (based on Django 1.5) wherein a user uploads a spreadsheet file.

I've been using xlrd for manipulating xls files and looked into openpyxl which claims to support xlsx/xlsm files.

So is there a common way to read/write both xls and xlsx files?

Another option could be to convert the uploaded file to xls and use xlrd. For this I looked into gnumeric and ssconvert, this would be favorable since all my existing code in written using xlrd and I will not have to change the existing codebase.

So should I change the library I use or go with the conversion solution?

Thanks in advance.

2

There are 2 answers

5
Charlie Clark On

xlrd can read both xlsx and xls files, so it's probably simplest to use that. Support for xlsx isn't as extensive as openpyxl but should be sufficient.

There's a risk of losing information in converting xlsx to xls because xlsx files can be much larger.

0
Samantra On
  1. According to xlrd's CHANGES, they dropped support for .xlsx files as of version 2.0 (since 2010).
  2. A quick look at python-excel.org list finds that there are still no tools supporting both formats (in 2023).

The imho simplest solution is to roll back to xlrd version 1.x, that may still be acceptable if the newest Excel features are not planned to be supported.