You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have recently worked on a project where I needed to insert pandas dataframes into an existing Excel workbook (.xlsx) file.
I wrote the following function:
defappend_df_to_excel(filename, df, sheet_name='Sheet1', startcol='A', startrow=1, **to_excel_kwargs):
""" Append a DataFrame [df] to existing Excel file [filename] into [sheet_name] Sheet. If [filename] doesn't exist, then this function will create it. Parameters: filename : File path or existing ExcelWriter (Example: '/path/to/file.xlsx') df : dataframe to save to workbook sheet_name : Name of sheet which will contain DataFrame. (default: 'Sheet1') startrow : upper left cell row to dump data frame - Excel row (starting from 1) not Python row (starting from 0) startcol : upper left cell column to dump data frame - Excel column (character) to_excel_kwargs : arguments which will be passed to `DataFrame.to_excel()` [can be dictionary] Returns: None """# ignore [engine] parameter if it was passedif'engine'into_excel_kwargs:
to_excel_kwargs.pop('engine')
writer=pd.ExcelWriter(filename, engine='openpyxl')
try:
# try to open an existing workbookwriter.book=load_workbook(filename)
# copy existing sheetswriter.sheets= {ws.title:wsforwsinwriter.book.worksheets}
exceptFileNotFoundError:
# file does not exist yet, we will create itpass# Convert Excel rows into Python rowsifstartrow>0:
startrow=startrow-1# Convert Excel columns into Python columnsstartcol=col2num(startcol) -1# write out the new sheetif'header'into_excel_kwargs:
header=to_excel_kwargs['header']
del(to_excel_kwargs['header'])
else:
header=Falsedf.to_excel(writer, sheet_name, startrow=startrow, startcol=startcol, header=header, index=False, **to_excel_kwargs)
writer.save()
Could this be optional behaviour of df.to_excel()
Problem description
It is sometimes necessary to fill data into templates provided in excel without overwriting other data or formulae in the excel workbook. Being able to do so using df.to_excel (and then developing the functions to make this relatively efficient if you have to write to many places in the same file) seems a natural and useful extension.
The text was updated successfully, but these errors were encountered:
Code Sample, a copy-pastable example if possible
I have recently worked on a project where I needed to insert pandas dataframes into an existing Excel workbook (.xlsx) file.
I wrote the following function:
Could this be optional behaviour of df.to_excel()
Problem description
It is sometimes necessary to fill data into templates provided in excel without overwriting other data or formulae in the excel workbook. Being able to do so using df.to_excel (and then developing the functions to make this relatively efficient if you have to write to many places in the same file) seems a natural and useful extension.
The text was updated successfully, but these errors were encountered: