Tag Archives: BadZipFile: File is not a zip file

BadZipFile: File is not a zip file [How to Solve]

Save data to excel file with openpyxl, and an error is reported: badzipfile: file is not a zip file

If the saved file does not exist, use pd.excelwriter directly

If the saved file already exists, use The openpyxl.load_workbook loads the existing file, and then uses pd.excelwriter

If the order of pd.excelwriter and openpyxl.load_workbooks is reversed, an error is reported: badzipfile: file is not a zip file

def append2sheet(filename,data):
	
	if not os.path.exists(file_name):
		ew = pd.ExcelWriter(file_name)
		data.to_excel(ew,sheet_name = 'sheet1')
		ew.save()

	else:
		wb = load_workbook(file_name)
		ew = pd.ExcelWriter(file_name)
		ew.book = wb 
		data.to_excel(ew,sheet_name = 'new')
		ew.save()