These decorators are used to expose Python functions to Excel as menu items.
This is using the ‘old style’ Add-Ins menu in Excel. For ribbon toolbars, please see Customizing the Ribbon.
xl_menu is a decorator for creating menu items that call Python functions. Menus appear in the ‘Addins’ section of the Excel ribbon from Excel 2007 onwards, or as a new menu in the main menu bar in earlier Excel versions.
name (string) – name of the menu item that the user will see in the menu
menu (string) – name of the menu that the item will be added to. If a menu of that name doesn’t already exist it will be created. By default the PyXLL menu is used
sub_menu (string) – name of the submenu that this item belongs to. If a submenu of that name doesn’t exist it will be created
order (int) – influences where the item appears in the menu. The higher the number, the further down the list. Items with the same sort order are ordered lexographically. If the item is a sub-menu item, this order influences where the sub-menu will appear in the main menu. The menu order my also be set in the config (see configuration).
sub_order (int) – similar to order but it is used to set the order of items within a sub-menu
menu_order (int) – used when there are multiple menus and controls the order in which the menus are added
allow_abort (boolean) – If True the function may be cancelled by the user pressing Esc. A KeyboardInterrupt exception is raised when Esc is pressed. If not specified the behavior is determined by the allow_abort setting in the config (see PyXLL Settings).
shortcut (string) –
Assigns a keyboard shortcut to the menu item. Shortcuts should be one or more modifier key names (Ctrl, Shift or Alt) and a key, separated by the ‘+’ symbol. For example, ‘Ctrl+Shift+R’.
If the same key combination is already in use by Excel it may not be possible to assign a menu item to that combination.
Example usage:
from pyxll import xl_menu, xlcAlert
@xl_menu("My menu item")
def my_menu_item():
xlcAlert("Menu button example")
See Menu Functions for more details about using the xl_menu decorator.