1

i know there are a few posts on this but i cant see to find my answer. I have an excel sheet (abc.xlsx) with a pivot table (on sheet2) pulling info from "RawData", i need to update "RawData" ( a full replace of sheet is fine). when i try the code below, it creates "RawData2" instead

path = r"C:\Users....\ABC.xlsx"
writer = pd.ExcelWriter(path,engine='openpyxl', mode='a')

if os.path.exists(path):
    book = openpyxl.load_workbook(path)
    writer.book = book

df.to_excel(writer, sheet_name="RawData", index=False)
writer.save()
writer.close()

Note i also tried the xlsxwriter engine instead, but that replaced entire worksheet:

path = r"C:\Users....\ABC.xlsx"
writer = pd.ExcelWriter(path, engine = 'xlsxwriter')
df.to_excel(writer, sheet_name = 'RawData', index=False)
writer.save()
writer.close()
msulol
  • 67
  • 6
  • see [this](https://stackoverflow.com/a/47740262/9375102) answer by MaxU I used it many a time when I was trying to escape excel hell (: – Umar.H Jun 16 '20 at 19:43
  • wow that is a long code, i am not that familiar with what to change in the helper code. would you be able to help with the fields you have above? i tried to replace the "def.." line with the usage recommendation and it didnt work. "def append_df_to_excel(path, df, sheet_name = 'RawData') – msulol Jun 16 '20 at 20:14
  • you need to create the object in your own enviroment (your notebook / script) then call it `append_df_to_excel('d:/temp/test.xlsx', df, sheet_name='Sheet2', index=False, startrow=25)` – Umar.H Jun 16 '20 at 20:58
  • 1
    oh got it, i was adjusting the "def line" the above needs to be a new line right after. thank you. that worked! – msulol Jun 16 '20 at 22:25
  • Let's mark this as a duplicate so others can find the answer too – Umar.H Jun 16 '20 at 22:40
  • Yep, i linked the 2. thanks – msulol Jun 17 '20 at 01:44

0 Answers0