I am trying to clear all data in a sheet in excel and replace it with a pandas DataFrame. This sheet is then used in a pivot table in another sheet. First, what is the best way to replace the data using openpyxl from a pandas DataFrame. Then is it possible to refresh the pivot tables to the new data range?
I found some a few question here and here, but the following doesn't seem to overwrite the data of an existing sheet in an excel file.
df = pd.read_csv(PATH)
book = xls.load_workbook(PATH)
writer = pd.ExcelWriter(PATH, engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer,sheet_name='SHEET_NAME',index=False,encoding='utf8')
writer.save()
openpyxl has some documentaion about overwriting single cell and row values, but not an entire sheet of data.
Thanks for the help!