Openpyxl-read formulas results (Python 2.7)

℡╲_俬逩灬. 提交于 2019-11-27 08:24:01

问题


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:


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)



来源:https://stackoverflow.com/questions/37478237/openpyxl-read-formulas-results-python-2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!