Reputation: 937
I would like to export data from dataframe to an excel, that has already its format-layout (colours, cells, etc.)
This code overwrite the all sheet, instead I would like the export data without changing excel layout.
is that possible?
writer = pd.ExcelWriter('C:/pandas_positioning.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='my_data',
startrow=7, startcol=4, header=False, index=False)
writer.save()
Upvotes: 2
Views: 3973
Reputation: 7522
Do it manually.
Example:
WB = 'C:/pandas_positioning.xlsx'
WS = 'my_data'
writer = pd.ExcelWriter(WB, engine='openpyxl')
wb = openpyxl.load_workbook(WB)
wb.create_sheet(WS)
writer.book = wb
writer.sheets = {x.title: x for x in wb.worksheets}
ws = writer.sheets[WS]
for icol, col_name in zip(range(len(df.columns)+1), df.columns):
ws.cell(1, icol+2, col_name)
for irow, row_name in zip(range(len(df.columns)+1), df.index):
ws.cell(irow+2, 1, row_name)
for (_, row), icol in zip(df.iterrows(), range(len(df.index) + 1)):
for (_, cell), c in zip(row.iteritems(), range(len(df.columns) + 1)):
ws.cell(icol+2, c+2).value = cell
writer.save()
Upvotes: 1