Getting Started with LibreOffice Calc and Python

Let’s get started with LibreOffice Calc & Python programming:

Software

LibreOffice

You will of course need LibreOffice, you can download the latest version here.
LibreOffice is shipped with a PyUNO bridge. This bridge binds its components with the Python language. It makes it possible to program LibreOffice with Python. (see at end of this article if you’re using a Linux OS ).

Python 3

You also need the Python 3 programming language on your computer.
It may be already installed. If not, please web search for ‘python 3’ for your Operating system.

IDE / Text Editor

You will also need a text editor or even better an IDE (Integrated Development Environment) to write your python program. The advantage of an IDE versus a simple text editor is that it offers tools to organize your code. It makes it easier to spot errors. An IDE offers various features to speed up and ease coding, like word auto-completion and search and replace.
If you are a programmer you may already have a preference for a simple editor or a clever one. I am using a free open-source IDE called geany. Other popular IDEs include Eclipse, code::block, and the famous Microsoft Visual Studio, or the open-source lightweight Visual Studio Code. They all have their pros and cons, here is the Top 20 of the most popular ones. Geany is perfect for ‘small jobs’ like we are going to do. Still, it’s also fine to use a text editor like notepad.

JRE install

If your computer does not have Java Runtime Environment already you will need to install it through the Oracle website. You can also follow instruction here. JRE is indeed necessary to use macros within LibreOffice.

APSO extension

By default LibreOffice Python Macros are independent from any LibreOffice file. To execute a python program within a LibreOffice Calc Spreadsheet, you need to go to Tools/Macro/Run Macro. Then, select the python program from a list corresponding to a folder on your Hard drive. And this list, for example ‘MyMacro’, would be accessible for any LibreOffice file (Calc, Text, Base, etc…). This is great. However, it is not helpful when you want a particular python program to be used specifically for a particular file.
APSO is a LibreOffice extension that helps with organising Python Macros within your LibreOffice files.
Using APSO you can also embed your program in a particular file and will be stored within that file. They will be linked to that file. For example, a LibreOffice Calc file will have a link to the Python program in its parameters. You will be able to see your program attached to LibreOffice documents currently opened.
More about this in my tutorial ‘Embedding vs. Storing Python Macros in LibreOffice‘.
Another advantage of APSO is its EditorKicker. You can set it up to use your preferred text editor or IDE. This allows you to edit your Python program.
You just need to download the APSO extension here and then install the tool via Tools/Extensions Manager.

Extra step for linux

I had to do one extra step when I used LibreOffice 5. This was under Linux mint Operating System. The step was adding some library. Without it it was not possible to do any Python macros within LibreOffice. This is an easy step, just open a terminal, type and execute:

sudo apt-get install libreoffice-script-provider-python

It will be the same for Ubuntu. For other linux distro you will need to check yourself but it’d be similar using the in-house package manager.

Documentation

It is not easy to find helpful documentation for programming Python Macros for LibreOffice when you start programming. Also, the tasks explained are usually too complicated for a beginner.

So tutolibro.org is the right place to start!
You can also use the official LibreOffice API references pages. I do use them a lot by searching particular method using the search box on the top right hand side.

Further notes

In my tutorials I want to develop simple and useful functions from scratch. Yet, sometime I use existing modules developed by other people. Like you would do for any program.
If you want to learn more about LibreOffice Calc itself, without python programming, you can check my time schedule tutorial.

If you like this tutorial, you could buy me a coffee to help me continue writing add-free tutorials.

Thank you!

8 Comments

  1. Nukool Chompuparn

    Thank you so much again.
    I use Fedora 30 Workstation KDE from Fedora Spins and LibreOffice 6.2.5.2.

    Reply
  2. Nukool Chompuparn

    I use Fedora 30 Workstation KDE from Fedora Spins and LibreOffice 6.2.5.2.
    In LibreOffice Calc, there is an messsage box showing right after I select Tools > Macros > Organize Macros > Python.
    The title caption is ‘JRE Required’.
    And the message caption is ‘LibreOffice requires a Java runtime environment (JRE) to perform this task. Please install a JRE and restart LibreOffice’.

    What could be the cause of this error ?

    Reply
    1. gweno (Post author)

      Hi Nukool. Yes Java Runtime Environment is required, I had it already installed on my Linux Mint station, and my Ubuntu one as well. Thanks for pointing it out, I will add a note in my post. It seems JRE is required for a lot of features in LibreOffice (like LibreOffice Base as well). You can download Java RE from the Oracle website.Thanks

      Reply
  3. Nukool Chompuparn

    1. The LO developer said such ‘Java Required’ is a bug and they have modified already in newer version.
    2. In apso website, it supports only LO 6.2. Now I have installed 6.3.0.4, will apso work on this LO version or should I wait for the announcement from apso website ?

    Reply
    1. gweno (Post author)

      Hi, thanks for sharing your findings about the Java bug. Regarding APSO they haven’t checked compatibility with L.O. 6.3 yet indeed, I had a look at 6.3 new features, I’d be surprised if APSO doesn’t work on it.

      Reply
  4. S

    HEllo , not sure if you are still monitoring this, but I came across your tutorial as a complete new B , I haven’t programed in 15 years, PERL. Looking to write a macro for LO calc.. I am having a problem with the ASPO , when I choose edit it looks as tho it opens an editor any crashes immediately.. Any Idea?

    Reply
    1. gweno (Post author)

      Hi. Can you check what editor is set up to edit python scripts? It’s in ‘Tools/Extension Manager’ in LibreOffice, select APSO from the list of extensions, then ‘Options’ and see the editor’s path. You can choose a text editor or an IDE there using its path. I hope it helps.

      Reply
  5. flywire

Leave a Comment

Your email address will not be published. Required fields are marked *