openpyxl.load_workbook(file, data_only=True doens't work?
Question:
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?
Answers:
From the documentation
openpyxl never evaluates formula
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.
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
There is an easy way to launch excel and get the formula values updated.
Sample Code Snippet
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open(inputFile)
workbook.Save()
workbook.Close()
excel.Quit()
# And for reading the data back we can use data_only mode as True.
oxl = openpyxl.load_workbook(inputFile,data_only=True)
Check the format of the cell in Excel.
I was running into this issue as well. The documentation indicated that you would have to open up the workbook through the excel application and resave it, then the value would return as the last calculated one. Such as
I did that and I still got ‘None’ as my return.
As with many excel/vba issues, it turned out it was a format issue. I had the cell formatted as ‘Accounting’ instead of ‘Number.’ After changing it to number, it worked.
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?
From the documentation
openpyxl never evaluates formula
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.
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
There is an easy way to launch excel and get the formula values updated.
Sample Code Snippet
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
workbook = excel.Workbooks.Open(inputFile)
workbook.Save()
workbook.Close()
excel.Quit()
# And for reading the data back we can use data_only mode as True.
oxl = openpyxl.load_workbook(inputFile,data_only=True)
Check the format of the cell in Excel.
I was running into this issue as well. The documentation indicated that you would have to open up the workbook through the excel application and resave it, then the value would return as the last calculated one. Such as
I did that and I still got ‘None’ as my return.
As with many excel/vba issues, it turned out it was a format issue. I had the cell formatted as ‘Accounting’ instead of ‘Number.’ After changing it to number, it worked.