Replacing data in xlsx sheet with pandas dataframe


I have an xlsx file with multiple tabs, one of them being Town_names that already has some data in it.

I’d like to overwrite that data with a dataframe – Town_namesDF – while keeping the rest of the xlsx tabs intact.

I’ve tried the following:

with pd.ExcelWriter(r'path/to/file.xlsx', engine='openpyxl', mode='a') as writer:


But it ends up creating a new tab Town_names1 instead of overwriting the Town_names tab. Am I missing something? Thanks.

Asked By: Robert C



You could try this to store all of the other sheets temporarily and then add them back. I don’t think this would save any formulas or formatting though.


with pd.ExcelWriter(r'path/to/file.xlsx', engine='openpyxl', mode='a') as writer:
Answered By: MBA Coder

Since you want to overwrite, but there is no direct option for that(like in julia’s XLSX there is option for cell_ref). Simply delete the duplicate if it exists and then write.

with pd.ExcelWriter('/path/to/file.xlsx',engine = "openpyxl",  mode='a') as writer:
 workBook =
  print("worksheet doesn't exist")
  df.to_excel(writer, sheet_name='Town_names')
Answered By: Yugandhar

Well, I’ve managed to do this. This is not a clean solution and not fast at all, but I’ve made use of openpyxl documentation for working with pandas found here:

I’m effectively selecting the Town_names sheet, clearing it with ws.delete_rows() and then appending each row of my dataframe to the sheet.

wb = openpyxl.load_workbook(r'path/to/file.xlsx')
ws = wb.get_sheet_by_name('Town_names')
ws.delete_rows(0, 1000)'path/to/file.xlsx')

wb = openpyxl.load_workbook(r'path/to/file.xlsx')
activeSheet = wb.get_sheet_by_name('Town_names')

for r in dataframe_to_rows(Town_namesDF, index=False, header=True):

for cell in activeSheet['A'] + activeSheet[1]: = 'Pandas''path/to/file.xlsx')

A bit messy and I hope there’s a better solution than mine, but this worked for me.

Answered By: Robert C

Hi you could use xlwings for that task. Here is an example.

import xlwings as xw
import pandas as pd

filename = "test.xlsx"
df = pd.read_excel(filename, "Town_names")

# Do your modifications of the worksheet here. For example, the following line "df * 2".
df = df * 2 

app = xw.App(visible=False)
wb = xw.Book(filename)
ws = wb.sheets["Town_names"]

ws["A1"].options(pd.DataFrame, header=1, index=False, expand='table').value = df

# If formatting of column names and index is needed as xlsxwriter does it, the following lines will do it.
ws["A1"].expand("right").api.Font.Bold = True
ws["A1"].expand("down").api.Font.Bold = True
ws["A1"].expand("right").api.Borders.Weight = 2
ws["A1"].expand("down").api.Borders.Weight = 2
Answered By: mouwsy

since pandas version 1.3.0. there is a new parameter: "if_sheet_exists"
{‘error’, ‘new’, ‘replace’}

pd.ExcelWriter(r'path/to/file.xlsx', engine='openpyxl', mode='a', if_sheet_exists='replace')
Answered By: JorgeBB
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.