0

I try to using pandas and openpyxl to write new data to existing excel(xlsx) file to avoid overwritten old data. Here is the reference link. How to write to an existing excel file without overwriting data (using pandas)? I keep getting zipfile.BadZipFile: File is not a zip file error, I googled it, someone said it was because I did not save the file, someone said it was due to the wrong format or the xlsx file. To rule out those possible cause, I just write the simple code like below with simple xlsx file.

writer = pd.ExcelWriter('testfile.xlsx', engine='openpyxl')
writer.book = load_workbook('testfile.xlsx')

writer.save()

xlsx file looks like below A B C aa bb cc But I still get the same error code. Any suggestion on how to resolve it?

chun xu
  • 393
  • 1
  • 4
  • 13
  • This is because you are overwriting the workbook with an empty file and then trying to read it. Load the workbook first and then assign it. Better still: do not use the same filename for the original and the new file. – Charlie Clark Mar 09 '21 at 13:20
  • Thank you for pointing the root cause. I found this thread which helped me to figure out the solution. https://stackoverflow.com/questions/39049148/python-update-dataframe-to-existing-excel-sheet-without-overwriting-contents-on – chun xu Mar 09 '21 at 23:44

0 Answers0