A simple Python Macro in LibreOffice 4.0

These instructions are provided on Ubuntu 13.04 environment.
Libre Office 4.0 was the newest version that was installed in my PC. I was experimenting Macro programming in LibreOffice 4.0. Since Libre is newer the documentation provides for macro writing and the extension writing seems less. I spent days on fiding the stuff related with the topics, as I am new to this topic. Yet there are documentation linked with Apache OpenOffice project. The beginners find more troubles there.

Open Office Documentation

LibreOffice Documentation

First open LibreOffice Writer and go through the following path.

    Tools->Macros->Organize Macros

In the last submenu you may find both ‘Libre Office Basic’ and ‘Python’. If you get this menu click Python. Then a dialog will be appeared with available Python Macros.

If you did not get ‘Python’ along with ‘LibreOfficeBasic’, below instructions will get you there. First close LibreOffice and start the ubuntu terminal. Then install the package libreoffice-script-provider-python by typing the following command.

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

As I was noted this package install the python3-uno package which works with some other python version(python 3.x) rather than python 2.7. Because of that when the libraries are imported to the Macro confilcts could occur. Becuase of that I installed python-uno package which refers to the python version 2.7 by running following command on the terminal.

sudo apt-get install python-uno

It will remove the already installed python3-uno. Now when you go to Tools->Macros->OrganizeMacros both ‘LibreOfficeBasic’ and ‘Python’ will be found. When you click python you will get the dialog shown below.


LibreOffice provides only the ‘Run’ facility for Python Macros. But for LibreOfficeBasic you will get all the facilities mentioned in the dialog. The macro “HelloWorld.py” is provided by the package script provider as a sample macro and it can be seen in the dialog.

Select the macro ‘LibreOfficeMacros->HelloWorld->HelloWorldPython’ and Run it. The string ‘Hello World’ will be printed on the opened LibreOffice Document. The code of  ‘HelloWorld.py’ is shown below.


def HelloWorldPython( ):

    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    if not hasattr(model, “Text”):
    model = desktop.loadComponentFromURL(
    “private:factory/swriter”,”_blank”, 0, () )
    text = model.Text
    tRange = text.End
    tRange.String = “Hello World (in Python)”
    return None


If the macro is not found in the dialog open a text document and paste the above mentioned code and save the file as ‘HelloWorld.py‘. Then move it to the directory,


You cannot just copy and paste, use terminal to copy the file.

sudo cp file_name_with_location /usr/lib/libreoffice/share/Scripts/python

After that run the macro in LibreOffice. By following above steps you can write python macros and execute them. Try to build an extension.


6 thoughts on “A simple Python Macro in LibreOffice 4.0

  1. Pingback: Kumpulan Pranala Pembuatan Macro di Libreoffice | RESTAVA

  2. Thank you very much for your time putting this together. After days googling, this is by far the best guide I have seen so far! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s