Unable to use Python macro as a spreadsheet function in LibreOffice Calc (v25.2.3.2)

Hello everyone,

I’m using the latest version of LibreOffice (25.2.3.2) on Linux, and I’m trying to use a Python macro as a spreadsheet function in Calc — but I haven’t been able to get it to work so far.

Here’s a minimal test script I’m using (just for debugging purposes):

python

def test_func():
    return "Hello from Python"

# Expose the function to LibreOffice
g_exportedScripts = (test_func,)

What I’ve tried:

  • I placed the file at:
    ~/.config/libreoffice/4/user/Scripts/python/test.py
  • The macro and the function are visible via:
    Tools → Macros → Run Macro → My Macros → test → test_func
  • I lowered the macro security in:
    Tools → Options → LibreOffice → Security → Macro Security → Low
  • I fully restarted LibreOffice

The problem:

When I try to use =test_func() directly in a cell, I get the error #NAME?.
From what I understand, this means Calc doesn’t recognize the function at all.

I searched in forums, LibreOffice documentation, and even asked ChatGPT — but I still haven’t found a clear explanation or solution. The official documentation on Python macros is really sparse, especially when it comes to using them as spreadsheet functions.

My question:

Has anyone successfully used a Python macro as a Calc formula?
Is it even supported natively, or do I need to use a workaround (like Basic wrappers or UNO services)?
Any pointers or working examples would be greatly appreciated!

Thanks in advance for your help :pray:

Loan

In LibreOffice teminology, an “add-in” is an extension providing additional spreadsheet functions.
These functions appear in the formula wizard and in the side bar with argument names and descriptions. They do NOT appear in the macro dialogs, simply because they are no macros.
Add-in functions can be written in any language but Basic (, C++, Python, Java, JavaScript, BeanShell).
User-defined macro functions can be written in Basic exclusively.
Apache OpenOffice Community Forum - [Calc][oxt] A function for all python string methods - (View topic) (a very useful Python Add-In)
https://extensions.libreoffice.org/en/extensions/show/1984 (a 9x9 array function solving Sudoku puzzles)

1 Like

Why? They can be written in Basic, too.

Really? Show me.

I won’t; but I will show a direction: the only problem with Basic is implementing UNO interfaces - but for that, there is CreateUnoListener Basic function, that allows to implement any UNO interface.

It is possible to implement LibreOffice: AddIn Service Reference in Basic?

:slight_smile: Let me try to do it, I am going to use the pystring as a starting point, with a “hello world” output. Takes some time :slight_smile:

I stand corrected. The problem is not implementing interfaces (that is possible); the problem is the constructor of the implementation. The code that instantiates that instance knows how to do that for Java or Python or Native, but not for Basic. :slight_smile: Thanks @Villeroy!

1 Like

It is simply impossible. The implementation of Calc’s UDF (user-defined functions) only uses Basic. So there is absolutely no point in trying what you tried, and asking if others succeeded. It could be possible to suggest / support a new feature of UDF in Python (but I’m skeptical; that is a can of worms; if a native Python solution is needed, it should be implemented not as UDF, but as a proper extension defining and implementing a function - and that is possible, see @Villeroy’s comment). For now, your only option is having Basic functions, that can, in turn, call Python code.

1 Like