An error is reported when using the openpyxl package to read Excel files: zipfile.BadZipFile: File is not a zip file

wrong description

When using openpyxl to open an Excel file and execute the load_workbook method, an error is reportedzipfile.BadZipFile: File is not a zip file

File "/home/youngyoung/util.py", line 69, in get_info_from_file
    with Excel(path) as f:
  File "/home/youngyoung/excel.py", line 111, in __enter__
    self._workbook = load_workbook(self.path)
  File "/home/youngyoung/py38env/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 344, in load_workbook
    reader = ExcelReader(filename, read_only, keep_vba,
  File "/home/youngyoung/py38env/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 123, in __init__
    self.archive = _validate_archive(fn)
  File "/home/youngyoung/py38env/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 95, in _validate_archive
    archive = ZipFile(filename, 'r')
  File "/home/youngyoung/python3.8.10/lib/python3.8/zipfile.py", line 1269, in __init__
    self._RealGetContents()
  File "/home/youngyoung/python3.8.10/lib/python3.8/zipfile.py", line 1336, in _RealGetContents
    raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

Check out some sayings online:

  1. openpyxlThe function used saveto save the data in Excela file.
  2. In the case that the save is not completed, load_workbookthe function is used to load the Excelfile.

  Solution: Do not use the function before executing the save function to save the data load_workbook.

My workaround: 

But I didn't write the Excel file, let alone save it, so this solution doesn't apply to my problem. Later, when I manually opened the Excel file, I found that it could not be opened. I found that the file was encrypted and locked, and I did not have permission to read the file. After removing the encryption lock of the file, the problem was solved.

Guess you like

Origin blog.csdn.net/m0_46829545/article/details/130593509