问题
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