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:
openpyxl
The function usedsave
to save the data inExcel
a file.- In the case that the save is not completed,
load_workbook
the function is used to load theExcel
file.
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.