python: actualice el marco de datos a la hoja de Excel existente sin sobrescribir el contenido en la misma hoja y otras hojas
Luchando por esto durante horas, así que decidí pedir ayuda a los expertos aquí:
Quiero modificar la hoja de Excel existente sin sobrescribir el contenido. Tengo otras hojas en este archivo de Excel y no quiero afectar otras hojas.
Creé un código de muestra, aunque no estoy seguro de cómo agregar la segunda hoja que quiero conservar.
t=pd.date_range('2004-01-31', freq='M', periods=4)
first=pd.DataFrame({'A':[1,1,1,1],
'B':[2,2,2,2]}, index=t)
first.index=first.index.strftime('%Y-%m-%d')
writer=pd.ExcelWriter('test.xlsx')
first.to_excel(writer, sheet_name='Here')
first.to_excel(writer, sheet_name='Keep')
#how to update the sheet'Here', cell A5:C6 with following without overwriting the rest?
#I want to keep the sheet "Keep"
update=pd.DataFrame({'A':[3,4],
'B':[4,5]}, index=pd.date_range('2004-04-30',
periods=2,
freq='M'))
He investigado SO. Pero no estoy seguro de cómo escribir un marco de datos en la hoja.
Ejemplo que he probado:
import openpyxl
xfile = openpyxl.load_workbook('test.xlsx')
sheet = xfile.get_sheet_by_name('test')
sheet['B5']='wrote!!'
xfile.save('test2.xlsx')