Python with excel (Openpyxl) - take the result of the formula instead of the formula itself

Asked

Viewed 178 times

2

Hey, guys, what’s up? First time around. I have a spreadsheet in excel with several formulas, and I wanted my python code to take for me only the result of these formulas, and not the formula itself, example:

import openpyxl
ficha=str(input("Informe o nome da ficha: "))
wb = openpyxl.load_workbook(ficha+'.xlsx')
NH=wb["PERÍCIAS"]['D4'].value
print(NH)

And in the print result, we have:

=IF(E4 ="DX",ATTRIBUTES! B5+F4,IF(E4="IQ",ATTRIBUTES! B7+F4,IF(E4="ST",ATTRIBUTES! B3+F4,IF(E4="HT",ATTRIBUTES! B9+F4,""))))

It gives me what I wrote in the excel cell, the formula, rather than a simple "12" which is the result of this kkkk account. Is it possible to do what I want? Thank you in advance.

1 answer

0

The function load_workbook() can receive the named parameter data_only, defining whether cells containing formula should hold the formula itself (default), or should hold the result of the last run of the formula (the last time excel read the spreadsheet and ran it).

So:

  • data_only = True -> The Property value return the last value resulting from the last execution of the formula.
  • data_only = False -> The Property value will return the formula stored in the cell. (default)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.