Sign up to take part
Registered users can ask their own questions, contribute to discussions, and be part of the Community!
Added on July 20, 2023 9:27PM
Likes: 0
Replies: 1
How do I deal with this error -
I am getting this error:
this is my library code:
import dataiku
import pandas as pd, numpy as np
from pandas import ExcelWriter
from openpyxl import load_workbook, BadZipFile
def is_valid_excel(file_name):
try:
load_workbook(file_name)
return True
except BadZipFile:
return False
def write_dataframe_to_excel(file_name, sheetname, dataframe):
if is_valid_excel(file_name):
# Open the Excel file
writer = pd.ExcelWriter(file_name, mode="a", engine="openpyxl", if_sheet_exists="replace")
with writer:
dataframe.to_excel(writer, sheet_name=sheetname, index=False)
# Save the changes and close the Excel file
writer.save()
else:
# If the file is not a valid Excel file, create a new one
writer = pd.ExcelWriter(file_name, engine="openpyxl")
with writer:
dataframe.to_excel(writer, sheet_name=sheetname, index=False)
# Save the changes and close the Excel file
writer.save()
Hi @AS14
,
You'll need to replace 'from openpyxl import BadZipFile' with:
<span class="hljs-keyword">from</span> zipfile <span class="hljs-keyword">import</span> BadZipFile