I wrote below code to write to a workbook using pandas. Idea is to add a new sheet to the workbook if one with same name already exists, otherwise create a new workbook.
import pandas as pd
from pathlib import Path
from openpyxl import load_workbook
xls = pd.ExcelFile("test.xlsx")
df = pd.read_excel(xls, "Sheet1")
op_name = "op.xlsx"
my_file = Path(op_name)
if my_file.exists():
book = load_workbook(my_file)
writer = pd.ExcelWriter(op_name, engine='openpyxl', mode='a')
writer.book = book
df.to_excel(writer, sheet_name="r2")
else:
writer = pd.ExcelWriter(op_name, engine='openpyxl')
df.to_excel(writer, sheet_name="r1")
writer.save()
writer.close()
However I get the following error while opening the output workbook:
We found a problem with some content in 'op.xlsx'. Do you want us to try and recover as much as we can?
I have tried multiple variations of this code but nothing has made it work. Any suggestions on how to resolve this issue will be highly appreciated.
from Corrupted Workbook when written with openpyxl in pandas
No comments:
Post a Comment