xlsx and xlsm files return badzipfile: file is not a zip file

59,145

Solution 1

It's because you created empty .xlsx with no metadata which is an empty file with no cells formatting. use exel or equivalent spreadsheet software to save empty file in that directory, which will create an xlsx file with cell formating, after saving try to load file using openpyxl

Solution 2

If you call openpyxl.load_workbook with file-like object, be sure to open it in binary mode.

Solution 3

I did something really stupid and got the same error. Basically, today was my first time trying this and I got it to work with the 'Automate' example and then tried my Excel. Didn't work! Took me a while to realize the error was due to having workbook password protected. The error doesn't match that at all, but when I removed the protection from the workbook, it worked! What can I say but 'duh' and 'yeah!'?

Solution 4

The XLSX or XLS or XLSM files you are trying to open are excel recovery files start with "~". you can check by:

for file in path.glob('*.xlsx'):print(file)

you can skip those files by checking,get filename from full path:

filename=str(filename).split("\\")[-1:][0]

checking if the filename starts with "~" as all recovery files will start with "~"

if filename[0]!="~"

Solution 5

I'm late to the party, but I received the same error, but for different reasons.

On my mac, when I cut and pasted the .xlsx file into the directory I desired, it didn't actually place the file itself, it placed a symbolic link. That, for some reason, would raise the same error. I remedied this by opening the file, and using "save as".

Share:
59,145
zw1ck
Author by

zw1ck

Updated on October 08, 2021

Comments

  • zw1ck
    zw1ck over 2 years

    I'm trying to open both an xlsx file and an xlsm file both give me the same error

    badzipfile: file is not a zip file

    here is what I'm typing:

    import openpyxl
    wb=openpyxl.load_workbook('c:\\users\\me\\documents\\filename.xlsm', keep_vba=True)
    wb2=openpyxl.load_workbook('c:\\users\\me\\documents\\filename2.xlsx')
    

    both load_workbook commands result in the same error. They both exist at that location. why am I getting this error?