This article explains how to set up LibreOffice for Python macro and helps you to write your first Python macro in LibreOffice Calc and Writer.
Writing macros in LibreOffice in Basic is easier since it has been supported since the beginning because Basic is an older programming language. However, Python macros are a little difficult to set up in LibreOffice. In this tutorial, you can learn how to set up your system for Python macro and run a sample program.
Table of Contents
Introduction and system set-up
To access the LibreOffice API, you need to use the PyUNO component. The PyUNO package gives you access to LibreOffice workspace, document, objects and all the open files and components. When you run a Python script, it invokes the LibreOffice process using the PyUNO runtime.
The default installation of LibreOffice in Ubuntu, Linux Mint, Fedora and other distribution should work with Python because Python is installed in Linux systems by default.
However, if the Python option is unavailable (See the below image) in the menu,
Tools > Macros > Organize Macros, then run the following command from the terminal in Ubuntu and related distributions. Restart your system once complete.
Fedora Linux doesn’t require this package.
sudo apt install libreoffice-script-provider-python
After restarting, verify whether the Python option is available.
Windows users need to install Python in their system and LibreOffice as well. You can refer to the below guides if needed.
Python Coding Environment in LibreOffice
LibreOffice comes with a built-in macro editor for Basic programming language. But for Python, it doesn’t work. Hence, you have to write the Python code in a separate editor. If you are a beginner, I recommend using the simple editor Thonny, which works great for most of the work. Also, you can pick any Python editor of your choice from here.
Proceed to the next step after you install Thonny or any Python editor.
Python macro files location and user profile
There are two places where LibreOffice picks up the macro files (i.e. .py files). One is for the system location, which is common for all users. And the other one is user-specific location, and files are accessible to that user only.
If you are setting up the first time, the folders “Scripts” and “python” may not exist for the “user-specific” locations. If not, create them.
Writing your first Python Macro
Firstly, I will create a macro in Python for LibreOffice Calc. The macro should add a simple “hello world” text to the first cell A1 of the first Sheet of a LibreOffice Calc workbook.
In the Thonny editor, create a file and save it as “HelloWorldCalc.py” to ~/.config/libreoffice/4/user/Scripts/python (for Linux) and C:\Users\<user>\AppData\Roaming\LibreOffice\4\user\Scripts\python for windows.
In the first line, you have to import the uno library. You can do it by:
In earlier examples, I explained the concept of the currentDocument in LibreOffice or StartOffice object model in Basic macro. Similarly, for Python, you need to use the following statement to get an object of the running instance of LibreOffice Calc workbook (or Writer document). Make sure to add the statements inside a function.
oDoc = XSCRIPTCONTEXT.getDocument()
SCRIPTCONTEXT is an object of class ‘pythonscript.ScriptContext’ of pythonscript.py, which is installed as part of the uno package.
Alternatively, you can write the following to achieve the same handle as the current running document:
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
oDoc = desktop.getCurrentComponent()
Once the document object is ready, you can access the workbook sheets. Sheet1 starts at position 0 and so on. To get the first sheet, you can write the following, which gives you the first sheet by getSheets() method of the document object:
And to access a cell, you can use the
getCellByPosition(col, row) method of the
oSheet object. So, to access cell A1 (i.e. col=1, row=1), you can write the following:
oCell = oSheet.getCellByPosition(0,0)
And finally, you can use the following to add value to the first cell.
oCell.String = 'Hello World via Python'
Putting it all together, the entire code can be like this. Make sure to save the file before running.
# HelloWorld python script for LibreOffice Calc # # This file is part of the DebugPoint.com tutorial series for Python Macros in LibreOffice # # import uno def HelloWorldPythonCalc(): oDoc = XSCRIPTCONTEXT.getDocument() oSheet =oDoc.getSheets().getByIndex(0) oCell = oSheet.getCellByPosition(0,0) oCell.String = 'Hello World via Python' return None
Running the Macro
To run the above macro, you have to trigger it via LibreOffice main dialog (Calc or Writer or others). If you try to run it via the Thonny Python editor, it may run, but you can’t see the output because the XSCRIPTCONTEXT object can’t see the running LibreOffice instance from the Thonny process.
So, to run the macro, click
Tools > Organize Macros > Python.
In the Python Macros window, expand the My Macros section. You should see the Python file name and the function name listed.
Select the function name and hit Run to execute.
Note: If you cannot see your Python macro file, make sure you have put it in the proper path, as mentioned above. Also, if the Run is disabled (greyed out), then your Python macro has some syntax error. So, go back to the editor and execute. The Python editor will tell you about any syntax error present.
Here’s a demo screencast of the above sample program.
I hope this guide introduces you to writing macros in Python in LibreOffice. Please let me know if you have encountered any errors using the above steps in the comment box below.
This is the first article of the several Python macro series. Stay tuned for the upcoming tutorials.