fbpx

Introducing PyXLL 3

Since the beginning, PyXLL development focused on the things that really matter for creating useful real-world spreadsheets; worksheet functions and macro functions. Without these all you can do is just drive Excel by poking numbers in and reading numbers out. At the time the first version of PyXLL was released, that was already possibly using COM, and so providing yet another API to do the same was seen as little value add. On the other hand, being able to write functions and macros in Python opens up possibilities that previously were only available in VBA or writing complicated Excel Addins in C++ or C#.

With the release of PyXLL 3 integrating your Python code into Excel has become more enjoyable than ever. Many things have been simplified to get you up and running faster, and there are some major new features to explore.

If you are new to PyXLL have a look at the Getting Started section of the documentation.

All the features of PyXLL, including these new ones, can be found in the Documentation

Customising the Ribbon

Screen Shot 2016-02-29 at 15.57.12

Ever wanted to write an addin that uses the Excel ribbon interface? Previously the only way to do this was to write a COM addin, which requires a lot of knowledge, skill and perseverance! Now you can do it with PyXLL by defining your ribbon as an XML document and adding it to your PyXLL config. All the callbacks between Excel and your Python code are handled for you.

See the Customising the Ribbon for more detailed information or try the example included in the download.

 

RTD (Real Time Data) Functions

rtd

PyXLL can stream live data into your spreadsheet without you having to write any extra services or register any COM controls. Any Python function exposed to Excel through PyXLL can return a new RTD type that acts as a ticking data source; Excel updates whenever the returned RTD publishes new data.

See Real Time Data for more detailed information or try the example included in the download.

Function Signatures and Type Annotation

xl_func and xl_macro need to know the argument and return types to be
able to tell Excel how they should be called. In previous versions that was always done by passing a ‘signature’ string to these decorators.

Now in PyXLL 3 the signature is entirely optional. If a signature is not supplied PyXLL will inspect the function and determine the signature for you.

If you use Python type annotations when declaring the function, PyXLL will use those when determining the function signature. Otherwise all arguments and the return type will be assumed to be var.

Default Keyword Arguments

Python functions with default keyword arguments now preserve their default value when called from
Excel with missing arguments. This means that a function like the one below
when called from Excel with b or c missing will be invoked with the correct default values for b and c.

@xl_func
def func_with_kwargs(a, b=1, c=2):
    return a + b + c

Easier Setup for Anaconda and Virtualenvs

Many problems with installing PyXLL come down to the fact that Python isn’t installed as the default Python installation on the PC. This means that PyXLL can’t find the Python dll or the Python packages
as their locations are not set globally.

This affects any distribution that doesn’t set itself up as the default Python install (most notably Anaconda, PortablePython and any virtualenv).

There were ways to get around this and make it work, but it always should have been easier. Now it is!

To set which Python to use with PyXLL now all you have to do is set it up in your pyxll.cfg file

[PYTHON]
executable = <your python.exe>

When this is set PyXLL will try to find the right Python DLL to load and try to locate where the Python libraries are installed. If it still can’t find either of those you can tell it by setting dll and/or pythonhome in the same section of the config.

Deep Reloading

If you’ve used PyXLL for a while you will have noticed that when you reload PyXLL only the modules listed in your pyxll.cfg file get reloaded. If you are working on a project that has multiple modules and not all of them are added to the config those won’t get reloaded, even if modules that are listed in the config file import them.

PyXLL can now track all the imports made by each module listed in the config file, and when you reload PyXLL all of those modules will be reloaded in the right order.

This feature is enabled in the config file by setting

[PYXLL]
deep_reload = 1

Error Caching

Sometimes it’s not convenient to have to pick through the log file to determine why a particular cell is failing to calculate.

The new function get_last_error takes an XLCell or a COM Range and returns the last exception (and traceback) to have occurred in that cell.

This can be used in menu functions or other worksheet functions to give end users better feedback about any errors in the worksheet.

Python Functions for Reload and Rebind

PyXLL can now be reloaded or it can rebind its Excel functions using the new Python functions reload and rebind.

Better win32com and comtypes Support

PyXLL has always had some integration with the pythoncom module, but it required some user code to make it really useful. It didn’t have any direct integration with the higher level win32com package or the
comtypes package.

The new function xl_app returns the current Excel Application instance either as a pythoncom PyIDispatch instance, a win32com.client.Dispatch instance or a wrapped comtypes POINTER(IUnknown) instance.

You may specify which COM library you want to use with PyXLL in the pyxll.cfg file

[PYXLL]
com_package = <win32com, comtypes or pythoncom>