-1

This should be pretty straight forward, but I haven't been able to find anything in the documentation.

How can I open an excel workbook and clear all data on a specific sheet, and then write the data in a pandas DataFrame to A1 of that sheet?

Thanks!

bkeesey
  • 466
  • 4
  • 12

1 Answers1

1

Easiest and safest, delete and recreate

workbook.remove_sheet(sheet_name)
df1.to_excel("file_name", sheet_name=sheet_name)  
FloLie
  • 1,820
  • 1
  • 7
  • 19
  • Gotcha, so there isn't a good way to keep the sheet? I say this, because other sheets reference specific cells for formulas, and I was hoping to keep that intact. – bkeesey Jul 06 '21 at 13:02
  • 1
    Puh, what is the default behaviour if you just use the second line, without deleting first? – FloLie Jul 06 '21 at 13:03
  • When I use df.to_excel("file_name", sheet_name=sheet_name) it overwrites the entire excel workbook, and saves a new workbook with just one sheet with the given sheet name. So it works correctly, but deletes all other sheets in the workbook. – bkeesey Jul 06 '21 at 13:10
  • 2
    https://stackoverflow.com/questions/42370977/how-to-save-a-new-sheet-in-an-existing-excel-file-using-pandas – FloLie Jul 06 '21 at 13:14
  • 1
    I think that should help you with the keep the other sheets problem – FloLie Jul 06 '21 at 13:14
  • 1
    I have never tried keeping the references, I will take a look at it toniught – FloLie Jul 06 '21 at 13:15