Openpyxl 1.8.5: Reading the result of a formula typed in a cell using openpyxl
openpyxl support either the formula or the value of the formula. You can select which using the data_only
flag when opening a workbook. However, openpyxl does not and will not calculate the result of a formula. There are libraries out there like pycel which purport to do this.
How to get value of cell instead formula value?
Edit: In latest version (3.0.6+) the api was changed to disable the user of the first option (internal_value
property) and thus only the second option now works. (Thanks Akshat)
You can either
- Use
row[0].internal_value
.
Or
- Use
data_only=True
when you load your workbook.
(For example,workbook = openpyxl.load_workbook("yourxlsx.xlsx", data_only=True)
)
The second option is good when you are sure you don't wish to ever get the original formula from the workbook.
openpyxl formula blank in output excel
Okay... So, I have something that works now. When using load_workbook()
, there is a data_only
flag you can use (from here):
There are several flags that can be used in load_workbook.
- data_only controls whether cells with formulae have either the formula (default) or the value stored the last time Excel read the sheet.
I do more operations on the same Excel file than just creating the average row in the question, and when doing these operations, I open the workbook like so:
wb = load_workbook(filename, data_only=True)
Turns out that this makes the formulas not work anymore! When I removed this flag everywhere where I open the workbook, the AVERAGE formula works. Make sure that if you select a range of columns, then to still use :
, but if you use a formula with multiple arguments, then separate them with ,
and not ;
.
Oh and btw, you can load the file in Excel in a different language; as long as you put the formula in English in your code, it will translate appropriately.
Related Topics
Why Is 'Self' in Python Objects Immutable
Expand the Line with Specified Width in Data Unit
What Is the Most Efficient Way of Counting Occurrences in Pandas
How to Loop Through a List by Twos
How to Integrate Flask & Scrapy
Pip Connection Failure: Cannot Fetch Index Base Url Http://Pypi.Python.Org/Simple/
Prevent Python from Caching the Imported Modules
How to Change Spacing Between Ticks in Matplotlib
Valueerror: Could Not Broadcast Input Array from Shape (224,224,3) into Shape (224,224)
How to Change UI in Same Window Using Pyqt5
Get Loop Count Inside a For-Loop
Count Frequency of Values in Pandas Dataframe Column
Convert from Ascii String Encoded in Hex to Plain Ascii
Activate Python Virtualenv in Dockerfile