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

前端 未结 13 1507
灰色年华
灰色年华 2020-12-30 05:48

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 wh

相关标签:
13条回答
  • 2020-12-30 06:15

    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!'?

    0 讨论(0)
  • 2020-12-30 06:15

    My hunch is that either you openpyxl version is not the latest (2.3.1) or that there is a problem with your source file. To upgrade to the newest version to openpyxl, use:

    pip install openpyxl --upgrade
    

    Is the source file encrypted at all?

    0 讨论(0)
  • 2020-12-30 06:18

    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

    0 讨论(0)
  • 2020-12-30 06:18

    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]!="~"
    
    0 讨论(0)
  • 2020-12-30 06:24

    In my case the file I accessed had been previously opened and not properly closed by the Workbook.save() method. Because I was debugging my code and failed on this command due to an incorrect path the file became corrupt. I had to delete the file since it could not be restored and create a new one.

    0 讨论(0)
  • 2020-12-30 06:29

    In my case I received this error because I was reading a very large file that I had just downloaded. I copied it to another location before it had finished downloading and so got the error when trying to read an incomplete/corrupt file.

    0 讨论(0)
提交回复
热议问题