简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM