I want to export dataframe C to an existing excel, saving it in a new tab. I tried:
writer=pd.ExcelWriter(r'H:\test.xlsx', engine='xlsxwriter')
C.to_excel(writer,sheet_name='c')
writer.save()
(Notice: Excel test is an existing and previously saved Excel, with different tables A, B in tabs a, b, which I want to keep)
This code however deletes the existing excel and creates a new one with only the tab "c". I want to keep the existing tabs "a" and "b".
What is the right way?
ps: I tried suggestion on link "How to save a new sheet in an existing excel file, using Pandas? " as follows:
writer=pd.ExcelWriter(r'H:\C.xlsx', engine='xlsxwriter')
y_pred_all2.to_excel(writer,sheet_name='cc')
writer.save()
writer.close()
from openpyxl import load_workbook
path=r'H:\C.xlsx'
book=load_workbook(path)
writer=pd.ExcelWriter(path, engine='xlsxwriter')
writer.book=book
C.to_excel(writer,sheet_name='returns')
This gives me error: AttributeError: 'Workbook' object has no attribute 'add_worksheet'