I use openpyxl to create Excel files from Python. I want to take a pandas Dataframe, insert it into a spreadsheet and then summarize column A, B and C with a for loop. (formula in cell A5, B5 and C6 ). However, I'm struggle with how to change the formula string in the loop so I summarize the correct column.
data = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
columns=['a', 'b', 'c'])
data
wb1 = openpyxl.Workbook()
ws1 = wb1.active
rows = dataframe_to_rows(data, index=False, header=False)
for r_idx, row in enumerate(rows, 2):
for c_idx, value in enumerate(row, 1):
ws1.cell(row=r_idx, column=c_idx, value=value)
for col in ws1.iter_cols(min_row=5, max_row=5, min_col=1, max_col=3):
for cell in col:
cell.value = '=SUMMA(A2:A4)' # can I use .format() here in a smart way so A2:A4 changes to # B2:B4 and so on?
wb1.save('test.xlsx')