Python Forum

Full Version: Openpyxl manipulate excel write formula
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
I have a demo about calculated each score with target gap as attached, formula is : ='Log'!C3-'Log'!C$2, ='Log'!C4-'Log'!C$2, ='Log'!C5-'Log'!C$2,and stored result in sheet Demo by each row executed .
Actually use formula is ='Log'!C3-'Log'!C2,how to change to except functions(='Log'!C3-'Log'!C$2),any comment will be appreciated!


import openpyxl
from openpyxl import utils

wb=openpyxl.load_workbook("summary.xlsx")
ws=wb['Demo']
ws1=wb['Log']

min_row=ws.min_row
max_row=ws.max_row
min_col=ws.min_column
max_col=ws.max_column

min_row1=ws1.min_row
max_row1=ws1.max_row
min_col1=ws1.min_column
max_col1=ws1.max_column

print(min_row,max_row,min_col,max_col)
print(min_row1,max_row1,min_col1,max_col1)

for row in range(min_row+1,max_row+1):
    for col in range(min_col+1,max_col+1):
        key=ws.cell(row=row,column=col).coordinate
        start=ws.cell(row=2,column=col).coordinate
        end=ws.cell(row=row+1,column=col).coordinate
        print(type(end))
        print(end)
        ws[key]=f'={utils.quote_sheetname(ws1.title)}!{end}-{utils.quote_sheetname(ws1.title)}!{start}'

wb.save('summary.xlsx')