I have been trying to convert formulas in an Excel workbook to equivalent Python functions or statements in a Python script.
At my workplace we have older Excel workbooks that we use for calculations in engineering processes (like designing concrete structures). These calculations involve many formulas and references between formulas. I want to convert the formulas in those workbooks to 'equivalent' Python functions, the result being that those Excel workbooks are then replaced by Python scripts (for reasons concerning maintainability of these technical documents and workflow validation).
To demonstrate the principle with a basic example.
If there is an Excel workbook with one sheet (name = Sheet1)
value in cell A1 = 2
value in cell A2 = 3
value in cell A3 = '=A1+A2' = 5
I want to convert this to the following Python function:
def A3(A1,A2):
return A1+A2
Alternatively using a global variable
A3 = 0
def A3(A1,A3):
A3 = A1 + A3
Another more complex example involving a conditional statement:
Excel workbook with two sheets ('Sheet1' and 'Sheet2')
Sheet1!A1 = 2
Sheet1!A2 = 3
Sheet1!A3 = False
Sheet2!A1 = '=IF(Sheet1!A3 == True; Sheet1!A1+Sheet1!A2; Sheet1!A1*Sheet1!2)' = 6
In Python would become (or something similar):
def Sheet2!A1(Sheet1!A1,Sheet1!A2):
if Sheet!A3 == True:
return Sheet1!A1+Sheet1!A2
else:
return Sheet1!A1 * Sheet1!A2
I have tried using openpyxl (for reading workbooks) in combination with formulas (the module) and pycel, but I cannot figure out how to get such a conversion.
If I understand correctly the formulas package and pycel package can identify and convert Excel functions (to Python right?). Or maybe they just do so during runtime?
Specifically I want to know how to, essentially, compile the Excel formulas to Python functions.
The general question: is there a way to automate the conversion of Excel workbooks to equivalent Python scripts?