-2

I have this excel file

employee Salary Month Sum
10000 5 50000
20000 7 14000

So now in the sum column, there is a formula =A2*B2, for all the rows in the table.

Now, how to read these formulas in python from my excel sheet and make them dynamic so if I want to change the formula to =A2+B2 for all the rows,in future I can do it easily.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Related question: [How to save xls as csv so that formulas show as text?](https://superuser.com/questions/466419/how-to-save-xls-x-as-csv-file-so-that-formulas-show-as-text) – Stef Jun 14 '21 at 08:59
  • Maybe this can give you a start: [How can I see the formulas of an excel spreadsheet in pandas / python?](https://stackoverflow.com/questions/42102674/how-can-i-see-the-formulas-of-an-excel-spreadsheet-in-pandas-python) – Wizhi Jun 14 '21 at 09:02
  • But then how to edit it? – Mr.GodFather Jun 14 '21 at 09:16
  • What have you tried so far? – Charlie Clark Jun 14 '21 at 10:07

1 Answers1

0

You can try this simple example using openpyxl for multiplication and sum between two numbers in each row.

For the multiplication example:

from openpyxl import Workbook

wb = Workbook()
ws = wb.active

ws['A1'] = 10000
ws['A2'] = 20000
ws['B1'] = 5
ws['B2'] = 7

ws['C1'].value = ws['A1'].value * ws['B1'].value
ws['C2'].value = ws['A2'].value * ws['B1'].value
print(ws['C1'].value, ws['C2'].value)

For the sum example:

ws['C1'].value = ws['A1'].value + ws['B1'].value
ws['C2'].value = ws['A2'].value + ws['B2'].value
print(ws['C1'].value,ws['C2'].value)
Eureka JX
  • 66
  • 3