import openpyxl from openpyxl.utils import FORMULAE # 打开 Excel 文件 workbook = openpyxl.load_workbook('example.xlsx') # 选择工作表 sheet = workbook['Sheet1'] # 设置单元格公式 sheet['A1'] = 1 sheet['A2'] = 2 sheet['A3'] = 3 sheet['A4'] = '=SUM(A1:A3)' # 计算公式 FORMULAE.COMPLEX_EXPRESSIONS = {} workbook.calculation_on_load = True workbook.calculate() # 保存 Excel 文件 workbook.save('example.xlsx')