pd.read_excel randomly does not work, giving BadZipFile

Viewed 31

I have a script that runs weekly, and as of this week it failed with BadZipFile when trying to read an excel file with pandas. No changes have been made to the script and the dependency versions have not changed. Python 3.7, pandas 1.3.5, openpyxl 3.0.10. Here is the stack trace:

 df = pd.read_excel(filename, skiprows=1)
 File "/usr/local/lib/python3.7/site-packages/pandas/util/_decorators.py", line 311, in wrapper
 return func(*args, **kwargs)
 File "/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 364, in read_excel
 io = ExcelFile(io, storage_options=storage_options, engine=engine)
 File "/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 1192, in __init__
 content_or_path=path_or_buffer, storage_options=storage_options
 File "/usr/local/lib/python3.7/site-packages/pandas/io/excel/_base.py", line 1090, in inspect_excel_format
 zf = zipfile.ZipFile(stream) # type: ignore[arg-type]
 File "/usr/local/lib/python3.7/zipfile.py", line 1258, in __init__
 self._RealGetContents()
 File "/usr/local/lib/python3.7/zipfile.py", line 1325, in _RealGetContents
 raise BadZipFile("File is not a zip file")zipfile.BadZipFile:
 File is not a zip file

EDIT - I've retried the operation a few times with the same result. I can open, read, and edit the file manually, but for some reason the script is getting an error. I've tried opening the file with openpyxl and xlrd - get the same result with both.

1 Answers

The 3rd party in charge of uploading the file canceled their automation which created the script, and they completely rewrote it from scratch and started it again. This ended up solving my problem, so it is likely that somehow the file was actually corrupted even though I could open it and the data looked correct.

Related