Python openpyxl data_only=True returning None

我只是一个虾纸丫 提交于 2019-12-17 14:47:35

问题


I have a simple excel file: A1 = 200 A2 = 300 A3 = =SUM(A1:A2)

this file works in excel and shows proper value for SUM, but while using openpyxl module for python I cannot get value in data_only=True mode

Python code from shell:

wb = openpyxl.load_workbook('writeFormula.xlsx', data_only = True)
sheet = wb.active
sheet['A3']
<Cell Sheet.A3> # python response
print(sheet['A3'].value)
None # python response

while:

wb2 = openpyxl.load_workbook('writeFormula.xlsx')
sheet2 = wb2.active
sheet2['A3'].value
'=SUM(A1:A2)'  # python response

any suggestions what am I doing worng?


回答1:


It depends upon the provenance of the file. data_only=True depends upon the value of the formula being cached by an application like Excel. If, however, the file was created by openpyxl or a similar library, then it's probable that the formula was never evaluated and, thus, no cached value is available and openpyxl will report None as the value.




回答2:


I have replicated the issue with Openpyxl and Python.

I am currently using openpyxl version 2.6.3 and Python 3.7.4. Also I am assuming that you are trying to complete an exercise from ATBSWP by Al Sweigart.

I tried and tested Charlie Clark's answer, considering that Excel may indeed cache values. I opened the spreadsheet in Excel, copied and pasted the formula into the same exact cell, and finally saved the workbook. Upon reopening the workbook in Python with Openpyxl with the data_only=True option, and reading the value of this cell, I saw the proper value, 500, instead of the wrong value, the None type.

I hope this helps.



来源:https://stackoverflow.com/questions/36116162/python-openpyxl-data-only-true-returning-none

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