With PyXLL, Python functions can be called from Excel in multiple different ways.
Calling a Python function from an Excel worksheet
Calling functions from an Excel worksheet is a very common way to expose Python code to Excel users.
Your Python function is registered using the
xl_func decorator, and once PyXLL has been loaded (or reloaded) then that function is available to be called from an Excel worksheet in the same way as any other Excel function.
from pyxll import xl_func
return "Hello, %s" % name
The module that you write your function in has to be added to the list of modules in the pyxll.cfg config file.
See Worksheet Functions (UDFs) for more details.
Calling a Python function as an Excel macro
Excel macros are another way to call user code (including Python code) from Excel. Macros can be attached to controls, like buttons, so that the user can trigger the code. They can also be called from VBA.
You can read more about macro functions in Python in the section of the docs Macro Functions.
Using a Python function as callback from the Excel ribbon
Adding controls to the Excel ribbon is a great way to present a nice user interface in Excel.
The ribbon is extended by writing some XML and adding it to the pyxll.cfg config file.
Callbacks referenced in the ribbon XML file refer to Python functions, and so if you have a function you want to call in response to a user pressing a button in the ribbon it’s as simple as adding that function (using the full name, eg
your_module.function) to the button definition in the XML file.
<tab id="CustomTab" label="Custom Tab">
<group id="ContentGroup" label="Content">
<button id="textButton" label="Text Button"
The ribbon definition above links the button “Text Button” to the Python function
For more information about customizing the ribbon see Customizing the Ribbon.
Adding right click context menus to call Python functions
Right-clicking on a cell in Excel brings up a context menu. You can add your own items to this context menu and link them to Python functions, so that your function will be called whenever that context menu item is chosen.
This is done by customizing the ribbon XML file (as above), except this time instead of adding an element to the
ribbon section, we add it to the
<?xml version="1.0" encoding="UTF-8"?>
<!-- The ribbon and context menus can be specified in the same file -->
label="Toggle Case Upper/Lower/Proper"
The above XML code adds a right click context menu item that will call the Python function
context_menus.toggle_case when selected.
See Context Menu Functions for more details.
Adding menus to the Excel Add-Ins tab with Python
Adding menus to the Excel Add-Ins tab is one of the simplest ways to add Python functions so they can be called from Excel.
xl_menu decorator to any Python function will automatically add it to a menu in the Excel Add-Ins tab. When the menu item is selected, the Python function is called.
from pyxll import xl_menu, xlcAlert
See Menu Functions for more information.