Calling Python from Excel using xlwings
You have three options to call Python from Excel using xlwings
:
- The
Run
button under thexlwings
tab of the ribbon - Macros: These call Python from Excel
- User Defined Functions (UDFs) (Windows only)
Let’s have a look at the pros and cons of all three, as well as an example!
The Run button
The Run
button expects a function called main
in a Python module with the same name as your workbook. This is a quote from the documentation and a hard prerequisite. The main benefit of this method is that there is no VBA and no macros; you can use a normal XLSX file, which can be very useful in security-restricted situations where XLSM files are not allowed.
To try out the Run
button, follow these steps:
- Create a Python module called
sumitup.py
with the following code:import xlwings as xw def main(): wb = xw.Book.caller() &...