PyXLL is an Excel add-in that enables you to run Python in Excel.
Use Microsoft Excel as a user friendly front-end to your Python code. No VBA, just Python!
PyXLL is an Excel Add-In that enables developers to extend Excel’s capabilities with Python code.
PyXLL makes Python a productive, flexible back-end for Excel worksheets, and lets you use the familiar Excel user interface to interact with other parts of your information infrastructure.
With PyXLL, your Python code runs in Excel using any common Python distribution(e.g. Anaconda, Enthought’s Canopy or any other CPython distribution from 2.3 to 3.10).
Because PyXLL runs your own full Python distribution you have access to all third party Python packages such as NumPy, Pandas and SciPy and can call them from Excel.
Example use cases include:
Read more about PyXLL features on the features page.
PyXLL runs Python code in Excel according to the specifications in its config file, in which you configure how Python is run and which modules PyXLL should load. When PyXLL starts up it loads those modules and exposes certain functions that have been tagged with PyXLL decorators.
For example, an Excel user defined function (UDF) to compute the n th Fibonacci number can be written in Python as follows:
from pyxll import xl_func
@xl_func
def fib(n):
"Naiive Fibonacci implementation."
if n == 0:
return 0
elif n == 1:
return 1
return fib(n-1) + fib(n-2)
The xl_func
decorated function fib
is detected by PyXLL and exposed to Excel as a user-defined
function.
Excel types are automatically converted to Python types based on an optional function signature. Where there is no simple conversion (e.g. when returning an arbitrary class instance from a method) PyXLL stores the Python object reference as a cell value in Excel. When another function is called with a reference to that cell PyXLL retrieves the object and passes it to the method. PyXLL keeps track of cells referencing objects so that once an object is no longer referenced by Excel it can be dereferenced in Python.
There are many different Python packages for working with Excel.
The majority of these are for reading and writing Excel files (e.g. openpyxl and xlsxwriter).
PyXLL is very different to these other packages. Instead of just allowing you to read and write Excel files, PyXLL integrates Python into Excel. This allows you to run Python inside of Excel to extend Excel’s capabilities with your own Python code!
It is also possible to interact with Excel using a technology called COM. This allows a separate process to call into Excel and script it. PyXLL is different as it actually embeds Python inside the Excel process, rather than calling into it from an external process. This has huge implications for performance and means that PyXLL is by far the fastest way of integrating Python and Excel.
By integrating Python into Excel, PyXLL not only achieves the best possible performance, but it is also able to support many more features that are not possible using COM alone or only reading and writing files.
Just some of the features available in PyXLL that are not available in these other packages include:
PyXLL is used by large teams across many different industries and is designed to be able to be distributed to non-technical, non-Python users easily. If this is something you need please contact us and we will be happy to help.
For more information about other Python tools for Excel please see our blog post Tools for Working with Excel and Python, or for a more detailed comparision of PyXLL and xlwings please see this FAQ article What is the difference between PyXLL and xlwings.
All PyXLL subscriptions include technical support and upgrades to new releases.
If you’re not sure if PyXLL is right for your project or not, why not take advantage of our free 30 day trial to see for yourself? If you need any help getting started then just let us know.
Existing users might want to study What’s new in PyXLL 5. Those upgrading from earlier versions will should read “Important notes for upgrading from previous versions”. If you prefer to learn by watching, perhaps you would prefer our video guides and tutorials.
Note that you cannot mix 32-bit and 64-bit versions of Excel, Python and PyXLL – they all must be the same.
Install the add-in according to the installation instructions, making sure to update the configuration file if necessary. For specific instructions about installing with Anaconda or Miniconda see Using PyXLL with Anaconda.
Once PyXLL is installed you will be able to try out the examples workbook that is included in the download. All the code used in the examples workbook is also included in the download.
Note that any errors will be written to the log file, so if you are having difficulties always look in the log file to see what’s going wrong, and if in doubt please contact us.
After you’ve installed PyXLL below is an exercise to show you how to write your first Python user-defined function.
To begin with follow the instructions for first time users to install PyXLL.
You can use PyXLL’s command line tool to install the PyXLL add-in into Excel:
>> pip install pyxll
>> pyxll install
One of the main features of PyXLL is being able to call a Python function from a formula in an Excel workbook.
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
@xl_func
def hello(name):
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.
[PYXLL]
modules = <add the name of your new module here>
[PYTHON]
pythonpath = <add the folder containing your Python module here>
Go to the Addins menu in Excel and select PyXLL -> Reload. This causes PyXLL to reload the config and Python modules, allowing new and updated modules to be discovered.
Now in a worksheet you will find you can type a formula using your new Python function.:
=hello("me")
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 the necessary changes to your code and reload PyXLL again.
The documentation explains how to use all the features of PyXLL, and contains a complete API reference. PyXLL’s features are also well 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 documentation please contact us and we will do our best to help.