Openpyxl读取的公式结果(Python 2.7) [英] Openpyxl-read formulas results (Python 2.7)

查看:499
本文介绍了Openpyxl读取的公式结果(Python 2.7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用openpyxl从excel文件中读取.我正在尝试读取其值由公式计算的单元格.

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`

我试图这样使用(data_only = True)标志:

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.

是否可以使用openpyxl读取单元格的计算值?

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

更新:

通过进一步阅读,我怀疑问题出在与重新打开已经用openpyxl修改过的文件有关.打开原始文件后,使用'data_only'标志获取值.修改后重新打开后,公式单元格将变为空白.

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.

试图通过处理2个文件来解决此问题-到目前为止,没有太大的成功.

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

有人对此有解决方案吗?

Anybody has a solution for this?

推荐答案

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)

这篇关于Openpyxl读取的公式结果(Python 2.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆