Using Python to load template excel file, insert a DataFrame to specific lines and save as a new file
Sounds like a job for xlwings. You didn't post any test data, but modyfing below to suit your needs should be quite straight-forward.
import xlwings as xw
wb = xw.Book('your_excel_template.xlsx')
wb.sheets['Sheet1'].range('A1').value = df[your_selected_rows]
wb.save('new_file.xlsx')
wb.close()
I have written a package for inserting Pandas DataFrames to Excel sheets (specific rows/cells/columns), it's called pycellframe
:
https://pypi.org/project/pycellframe/
It has very simple and short documentation, and the method you need is incell_frame
So, let's say we have a Pandas DataFrame called df
which we have to insert in the Excel file ("MyWorkbook") sheet named "MySheet" from the cell B5, we can just use incell_frame
function as follows:
from pycellframe import incell_frame
from openpyxl import load_workbook
workbook = load_workbook("MyWorkbook.xlsx")
worksheet = workbook["MySheet"]
incell_frame(worksheet=worksheet,
dataframe=df,
row_range=(5, 0),
col_range=(2, 0))
0 as the value of the second element of row_range
or col_range
means that there is no ending row or column specified, if you need specific ending row/column you can replace 0 with it.