Bart Friederichs
Bart Friederichs

Reputation: 33491

openpyxl won't open XLSX file when extension is not .xlsx

I am using openpyxl to read an Excel XLSX file, but openpyxl won't open it, unless I explicitly give it a .xlsx extension. Otherwise, I get this error:

openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm

I open the file like this:

wb_input = load_workbook(filename_without_extension)

When using file on the file, I see it is, in fact, an Excel 2007+ (xlsx) file.

[bf@localhost ~]$ file my_excel_file
my_excel_file: Microsoft Excel 2007+

And when copying to a filename with a .xlsx extension, openpyxl does open it.

How can I open a valid xlsx file without extension, using openpyxl?

Upvotes: 0

Views: 1665

Answers (1)

Bart Friederichs
Bart Friederichs

Reputation: 33491

Turns out load_workbook also accepts a file-like object, so this works:

with open(filename_without_extension, "rb") as wbook:
    wb_input = load_workbook(wbook)

Upvotes: 3

Related Questions