简体   繁体   中英

OpenPyXl recalculate after changing some cells value

I'm trying to use OpenPyXL to:

  • Open an .xlsx file: A1 predefined value is 4
  • Read calculated value of a cell (B2), ex. B2 formula is A1 + 3 ex. B2 formula is A1 + 3 that is 7
  • Change value of A1 with different number, ex. 10 ex. 10
  • Read calculated value of a cell (B2)

I expected that the last result must be 10 + 3 = 13 but the result not changed after changing A1 cell value.

import openpyxl

wb = openpyxl.load_workbook("1.xlsx", data_only=False)

ws['B4'] = "C2"

print(ws2['B3'].value)

I found that must open workbook using data_only=False to calculate formula, but seems it calculate formula in loading workbook and can't recalculate it after some changes.

what can I do to recalculate formula manually after some changes?

Openpyxl doesn't have the capability to evaluate cell formulas. It can only read out the formula results that were stored in the .xlsx file during the last time when the file was saved by an Excel application.

So basically you'll have to open and save the .xlsx file with an Excel application in order to get recalculated formula results.

Note: calling and commanding an Excel application directly from python is possible with for example the xlwings package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM