openpyxl.load_workbook(file, data_only=True doens't work?

懵懂的女人 提交于 2019-12-04 01:53:09

问题


Why does x = "None" instead of "500"? I have tried everything that I know and searched 1 hour for answer... Thank you for any help!

import openpyxl

wb = openpyxl.Workbook()
sheet = wb.active
sheet["A1"] = 200
sheet["A2"] = 300
sheet["A3"] = "=SUM(A1+A2)"

wb.save("writeFormula.xlsx")

wbFormulas = openpyxl.load_workbook("writeFormula.xlsx")
sheet = wbFormulas.active
print(sheet["A3"].value)

wbDataOnly = openpyxl.load_workbook("writeFormula.xlsx", data_only=True)
sheet = wbDataOnly.active
x = (sheet["A3"].value)
print(x) # None? Should print 500?

回答1:


From the documentation

openpyxl never evaluates formula




回答2:


Documentation says:

data_only controls whether cells with formulae have either the formula (default) or the value stored the last time Excel read the sheet.

So, if you have not used Excel to open that .xlsx file(writeFormula.xlsx) once, Excel won't have any data to store then. As a result, your program will return a NoneType value. If you want your program return '500', you should manually open 'writeFormula.xlsx'. Then, annotate the file creation part of your program. You will get '500'.

I have already tried it. And it works. Tell me if you have a different oppinion. Thanks.




回答3:


I just have the same questions. The solution is open the xlsx file manually and close it, then click save. After this operation, you can try the wbDataonly programming part and get the data 500



来源:https://stackoverflow.com/questions/35681902/openpyxl-load-workbookfile-data-only-true-doenst-work

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