I have a pandas dataframe df
which I want to overwrite to a sheet Data
of an excel file while preserving all the other sheets since other sheets ha
You can do it using openpyxl:
import pandas as pd
from openpyxl import load_workbook
book = load_workbook(filename)
writer = pd.ExcelWriter(filename, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, "Data")
writer.save()
You need to initialize writer.sheets
in order to let ExcelWriter
know about the sheets. Otherwise, it will create a new sheet with the name that you pass.