Read Excel cell value and not the formula computing it -openpyxl Read Excel cell value and not the formula computing it -openpyxl python python

Read Excel cell value and not the formula computing it -openpyxl


wb = openpyxl.load_workbook(filename, data_only=True)

The data_only flag helps.


As @alex-martelli says, openpyxl does not evaluate formulae. When you open an Excel file with openpyxl you have the choice either to read the formulae or the last calculated value. If, as you indicate, the formula is dependent upon add-ins then the cached value can never be accurate. As add-ins outside the file specification they will never be supported. Instead you might want to look at something like xlwings which can interact with the Excel runtime.


As @Charlie Clark mentioned you could use xlwings (if you have MS Excel). Here an example

say you have an excel sheet with formulas, for the example I define one with openpyxl

from openpyxl import Workbook, load_workbookwb=Workbook()ws1=wb['Sheet']ws1['A1']='a'ws1['A2']='b'ws1['A3']='c'ws1['B1']=1ws1['B2']=2ws1['B3']='=B1+B2'wb.save('to_erase.xlsx')

As mentioned, if we load the excel again with openpyxl, we will not get the evaluated formula

wb2 = load_workbook(filename='to_erase.xlsx',data_only=True)wb2['Sheet']['B3'].value

you can use xlwings to get the formula evaluated by excel:

import xlwings as xwwbxl=xw.Book('to_erase.xlsx')wbxl.sheets['Sheet'].range('B3').value

which returns 3, the expected value.

I found it quite useful when working with spreadsheets with very complicated formulas and references between sheets.