Macros/Python Guide/Introduction

Return to Index

= Introduction =

What is PyUNO?
LibreOffice allows the users to write macros in several interpreted languages, one of which is Python. PyUNO is the component that gives users access to the application programming interface (API hereinafter by its acronym Application Programming Interface) of LibreOffice with Python.

Installation
On some operating systems, such as Ubuntu 18.04 LTS, you may need to install an additional OS package. On Ubuntu, the package is called libreoffice-script-provider-python and contains files such as scriptproviderforpython.rdb (XML metadata) and pythonscript.py (Python infrastructure).


 * In Ubuntu 18.04+

Test support for Python macros
Open a new document in Writer. Select menu, the "Macro Selector" dialog appears. In the section select, in the  section select , and click button.

If you see this result, your system can run Python macros.



Where save macros?
There is no built-in way to edit Python scripts so you have to use your own text editor. There are 3 places where you can put your code.

Profile USER folder, macros available only for USER

 * GNU/Linux: /home/USER/.config/libreoffice/4/user/Scripts/python
 * Windows: %APPDATA%\LibreOffice\4\user\Scripts\python
 * macOS: ~/Library/Application Support/LibreOffice/4/user/Scripts/python/

Directory of LibreOffice, macros available for all users
/usr/lib/libreoffice/share/Scripts/python/
 * GNU/Linux

The mentioned paths are default. In custom installations they can be different. If folders do not exist, you should create them while respecting the capitalization.

Inside a document

 * Any ODF file, really is a ZIP file, you can extract this file like extract normally this type files. In the root, create folder Scripts/python/ and copy inside any python file, for example: mymacros.py
 * You should see

myfile |  ...  ├── META-INF │  └── manifest.xml ├── Scripts │  └── python │      └── mymacros.py  ...


 * Edit file manifest.xml into folder META-INF and add lines, just before tag close 


 * The final file should be like:


 * Now, zip the complete folder again. Caution, not zip extern folder, zip the content of folder

Return to Index