Python
Implementing functions in Python
Python code is always run as one or more external processes - it never runs embedded inside Excel.
You still bundle your Python scripts into the XLL for simple distribution:
Single file deployment: All Python code is bundled into the XLL
Automatic extraction: Python files are extracted to
%LOCALAPPDATA%\xllify\<xllname>\on first loadAutomatic setup: Creates virtual environment (
venv/) and installs dependenciesExternal execution: Python runs as a separate process, communicating with Excel via IPC
Development-friendly: You can edit the extracted Python files during development
Building an XLL
Prepare your Python files
Create your Python modules as normal:
# main.py
import numpy
import xllify
@xllify.fn("PYRAND")
def pyrand(data):
return numpy.random.random()Create requirements.txt (optional)
List your dependencies:
numpy>=1.24.0Build the XLL
Use the xllify CLI to embed your Python files:
xllify MyAddin.xll main.py --py-entrypoint main --py-requirements requirements.txtArguments:
--py-entrypoint: Required - The module to run (without.pyextension)--py-requirements: Optional - Path to requirements.txt to embed
What happens on first load
When Excel loads the XLL for the first time, xllify automatically:
Extracts Python files to %LOCALAPPDATA%\xllify\<xllname>\python\
Creates a virtual environment at %LOCALAPPDATA%\xllify\<xllname>\venv\
Installs xllify-python package into the venv
Installs requirements.txt dependencies (if bundled)
Starts the Python process automatically
This process takes 10-20 seconds on first load. Subsequent loads are instant since the environment is already set up.
Try it out
You can now enter =PYRAND() into a cell. A random number will appear in the cell.
Editing extracted Python files during development
After extraction, you can edit the Python files directly:
# Navigate to the extracted Python directory
cd %LOCALAPPDATA%\xllify\MyAddin\python
# Edit files with your favorite editor
code main.pyChanges take effect immediately after you save the Python file. If you change the function signatures, you will need to restart Excel.
Re-extracting files after rebuilding
If you rebuild the XLL and want to re-extract the embedded Python:
Close Excel
Delete: %LOCALAPPDATA%\xllify\MyAddin\
Reopen Excel (fresh extraction will occur)
Troubleshooting
Last updated