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 load

  • Automatic setup: Creates virtual environment (venv/) and installs dependencies

  • External 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
# 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.0

Build the XLL

Use the xllify CLI to embed your Python files:

Example
xllify MyAddin.xll main.py --py-entrypoint main --py-requirements requirements.txt

Arguments:

  • --py-entrypoint: Required - The module to run (without .py extension)

  • --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:

1

Extracts Python files to %LOCALAPPDATA%\xllify\<xllname>\python\

2

Creates a virtual environment at %LOCALAPPDATA%\xllify\<xllname>\venv\

3

Installs xllify-python package into the venv

4

Installs requirements.txt dependencies (if bundled)

5

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:

Example
# Navigate to the extracted Python directory
cd %LOCALAPPDATA%\xllify\MyAddin\python

# Edit files with your favorite editor
code main.py

Changes take effect immediately after you save the Python file. If you change the function signatures, you will need to restart Excel.

If you rebuild the XLL with updated Python code, you must delete the extraction directory to force re-extraction (see Re-extracting files after rebuilding).

Re-extracting files after rebuilding

If you rebuild the XLL and want to re-extract the embedded Python:

1

Close Excel

2

Delete: %LOCALAPPDATA%\xllify\MyAddin\

3

Reopen Excel (fresh extraction will occur)

Troubleshooting

Python files not extracted
  • Check %LOCALAPPDATA%\xllify\<xllname>\xllify.log for errors

  • Verify Python is installed and on PATH

  • Check disk permissions

Environment setup failed
  • Ensure Python is installed: python --version

  • Check that Python is on your system PATH

  • Try deleting the extraction directory and reloading

Dependencies not installing
  • Check requirements.txt was bundled correctly: xllify MyAddin.xll main.py --py-requirements requirements.txt

  • Manually install: cd %LOCALAPPDATA%\xllify\MyAddin && venv\Scripts\pip install -r requirements.txt

Need to update Python code
  • Rebuild the XLL with updated files

  • Delete: %LOCALAPPDATA%\xllify\MyAddin\

  • Reopen Excel (fresh extraction + setup)

Last updated