openpyxl load_workbook() on a legit .xlsx file leads to a zipfile.BadZipFile error

I was able to replicate the problem. It is pandas related. Everything works just fine up to pandas 1.1.5
In pandas 1.2.0 they did some changes

At the time when you instantiate pd.ExcelWriter with

writer = pd.ExcelWriter(report_path, engine="openpyxl")`

it creates empty file with size 0 bytes and overwrites the existing file and then you get error when try to load it. It is not openpyxl related, because with latest version of openpyxl it works fine with pandas 1.1.5.

The solution – specify mode="a", change the above line to

writer = pd.ExcelWriter(report_path, engine="openpyxl", mode="a")

Alternatively – look at @CharlieClark solution or this solution where they loads the file before instantiating the pd.ExcelWriter..

Leave a Comment