r/excel • 8 • 12d ago

Plugin Python embedded in Excel

Hi All,

This weekend for a bit of fun I built a free / source-available Excel add-in which hosts it's own Python runtime.

You can find a feature runtime and installation guide on the website:

https://sancarn.github.io/xll-python/index.html

fork it here: https://github.com/sancarn/xll-python

But here's an overview:

  • You can write user defined functions which execute like formula.
  • Returning a generator will make the result update every time it yield's a result.
  • You can write macros (e.g. reading / writing CSVs/databases from disk etc.)
  • There's an in-built IDE
  • You can debug formula in the IDE
  • There's a console/REPL where you can experiment with the available runtime
  • There's a package manager where you can install pip packages
  • There's a compiler so you can compile your own scripts into standalone .xll addins.

Hope you find this helpful! ~Sancarn

Edit: This project is AI:X. All code written by Astra & Sol 6.2; albeit with a lot of steering.

11 Upvotes

29 comments sorted by

View all comments

1

u/decimalturn 11d ago

In terms of portability, how does it work? What's the minimal setup process someone has to do on their computer if I send them an .xlsm file that I created using user-defined functions with xll-python?

1

u/sancarn 8 11d ago edited 11d ago

Hi /u/decimalturn. If you've created some python scripts / udfs, you can use the on-board packager to package up your scripts with the python engine into a distributable XLL.

At that point, you simply send that XLL to your colleague and they can use it immediately after loading it into Excel.

Under the hood, it packages the python script with PythonExcelRuntime.xll, which has the Python runtime and Excel bindings, without the IDE and other UI of xll-python.

1

u/decimalturn 11d ago

So the UDFs in an Excel file would always be able to find the addin needed for their evaluation as long as the addin is installed (and wasn't renamed)?

2

u/sancarn 8 10d ago edited 10d ago

I may have misunderstood what you were asking in the first place? The add-in presents 2 mechanisms to load python functions:

  1. Load them from a folder of scripts - Your colleague enables xll-python.xll and configures it to load your scripts from a worksheet functions startup folder.
  2. Load them from a packaged XLL - You send your colleague my-package.xll alongside the workbook. They enable that XLL in Excel. It already contains your Python code and runtime.

In general:

  • If you have an xlsm alone, the functions will not run, they will error with #NAME!.
  • If you have an xlsm with xll-python.xll added, the formulas will evaluate as desired, assuming the scripts are loaded.
  • If you have an xlsm with my-package.xll added, the formulas will evaluate as desired.

Edit: I could probably add something which allows one to embed the python scripts within a workbook... Sounds like a neat feature.

2

u/decimalturn 10d ago

Ok, that answers it.

Yeah, embedding is probably useful in case people can't be sent or download xll files.

1

u/Double_Cost4865 1 10d ago

So does that mean if I receive 10 workbooks with UDFs from across the team, I will be loading hundreds of megabytes worth of XLLs each? Since it includes runtime and dependencies. Would all 10 workers be running at the same time? That sounds like a lot of bloat, memory usage, and I wonder what it does to the start up times

1

u/sancarn 8 10d ago

No, if you open all 10 workbooks in the same Excel instance, they share one xll-python.xll add-in and one Python runtime inside Excel.

If you compile 10x python packages to 10x separate myAddin.xll files, and load all 10x add-ins then it is possible that 10x separate python runtimes are loaded. But it basically depends on the version of python that it was packaged with...

If one was packaged with python 3.13 and another with 3.14; then you will have 2 shared runtimes on disk. The runtime instances have to be different though, ofc, else you will have unintended IPC.

1

u/Double_Cost4865 1 10d ago

Yes, I meant the case where someone is sharing their compiled xll. So over months, I can see this becoming a big issue and a strain on resources.

1

u/sancarn 8 10d ago edited 10d ago

I disagree. Minor versions are released annually. Given the bundle is 16MB, it would take 20 addins across 20 years to hit the memory usage of a typical app on the PC. And that assumes CPython is updated annually too (which it isn't - CPython is currently on v3.13; Python's latest release is 3.15). So while it is a potential problem, it's unlikely to ever cause a major issue.

Comparatively, Brave browser is consuming 2GB of RAM on my existing machine for no reason whatsoever🤣 (Apparently 1x linkedin tab consumes 1.4GB RAM... 👀)

1

u/Double_Cost4865 1 10d ago

I don’t understand.

Take one compiled xll. Install numpy, pandas, and scipy (realistically, there’d be more, but let’s just go with these).

How big is xll, given that it includes it’s own runtime and dependencies?

1

u/sancarn 8 10d ago

The runtime is 17MB alone, the compressed project and dependencies is 60.5MB.

1

u/Double_Cost4865 1 10d ago

So together 78mb, what about uncompressed? That’s a lot. Imagine this in a large corporation, I work hundreds of workbooks on any given month.

2

u/sancarn 8 10d ago edited 10d ago

Same, but I wouldn't be compiling 100 separate packages for usage over my org? I'm not sure I understand your use case, and why the default xll-python doesn't suffice...?

What is more, if you have 10 separate python applications, and you launch them as 10 processes and they all import numpy/pandas etc. you have the same memory trade-off. The only way to prevent that is by having 1 global instance of python; but then you run into sandboxing issues. E.G. where 2 scripts can't define the same public module names etc.

Anyway, feel free to fork the repo and submit a PR if you think this needs changing.

1

u/Double_Cost4865 1 10d ago

I think ArcGIS approach would be better, where you have a single runtime with a single list of dependencies and you use it in all projects. Python version and dependencies don’t change for years. That would make it easier for teams/organisations to align on environments, prepare for migrations between version every X years, and it would save storage/memory and number of processes that need to run even if you have been shared hundreds of workbooks over years, since at most, you only need 1 process running.

1

u/sancarn 8 10d ago

I think there are trade-offs with both approaches. I would recommend if you are in an organisation you use the single xll-python instance, instead of compiling to individual add-ins. What you are describing is the model which unbundled xll-python uses, and you can have 1 repository of custom functions shared across a business.

Separate XLLs give teams more control over what exactly is distributed. An older reporting add-in can stay on pandas 2 while a new forecasting add-in uses pandas 3. If you had 1 shared library across the entire org, you force everyone to use 1 version. Maybe that's okay in small orgs, but in larger orgs I think that'd be challenging.

If there are particular features that you are missing from xll-python which exist in the separate xlls, that's something we can look into otherwise.