Calling a Python Function in Excel
One of the main features of PyXLL is being able to call a Python function from a formula in an Excel
First start by creating a new Python module and writing a simple Python function. To expose that function
to Excel all you have to do is to apply the
xl_func decorator to it.:
from pyxll import xl_func
return "Hello, %s" % name
Save your module and edit the pyxll.cfg file again to add your new module to the list of modules to load and
add the directory containing your module to the pythonpath.
modules = <add your new module here>
pythonpath = <add the folder containing your Python code here>
Reload PyXLL by going to the Addins menu in Excel and selecting PyXLL -> Reload.
Now in a worksheet you can type a formula using your new Python function.:
If you make any mistakes in your code or your function returns an error you can check the log file
to find out what the error was, make and necessary changes to your code and reload PyXLL again.
The user guide has comprehensive coverage of how to use all the features of PyXLL and
a quick look through there will quickly bring you up to speed on how to do most things with PyXLL.
Many of PyXLL’s features are demonstrated in the examples included in download. These are a good place to
start to learn more about what PyXLL can do.
More example code can be found on PyXLL’s GitHub page.
If there is anything specifically you’re trying to achieve and can’t find an example or help in
the user guide please contact us and we will do our best to help.