Openpyxl-read formulas results (Python 2.7)

2019-01-20 14:13发布

I am using openpyxl to read from an excel file. I am trying to read a cell whose value is calculated by a formula.

Regular reading functions returns formula script:

`wb= openpyxl.load_workbook('forecast.xlsx')`
`sheet = wb.get_sheet_by_name('Sheet3')`
`result=sheet["F6"].value`

I tried to use (data_only=True) flag like this:

wb= openpyxl.load_workbook('forecast.xlsx', data_only=True)

Result was that all formula cells turned to blanks. Only pure values remained.

Is there a way to read a cell calculated value using openpyxl?

Update:

From further reading I suspect the issue is about re-opening a file that was already modified with openpyxl. Once I open an original file, I get the values using 'data_only' flag. Once re-opening after some modification, formula cells turn to blanks.

Tried solving this by working with 2 files- so far without much success.

Anybody has a solution for this?

1条回答
放荡不羁爱自由
2楼-- · 2019-01-20 15:08
wb = openpyxl.load_workbook('forecast.xlsx', data_only=True)

Will read the actual value. (From what you have posted, I think you miss a comma that separates the arguments)

查看更多
登录 后发表回答