Python Guide - Introduction
TDF LibreOffice Blogs communautaires Weblate (traduction) ODFAuthors Nextcloud Redmine Ask LibreOffice | Donner
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+
sudo apt install libreoffice-script-provider-python
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?
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
- OSX
~/Library/Application Support/LibreOffice/4/user/Scripts/python/
Directory of LibreOffice, macros available for all users
- GNU/Linux
/usr/lib/libreoffice/share/Scripts/python/
- GNU/Linux
This paths are default, in custom installations, can be different. If folders not exist, you should create, 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.
- Any ODF file, really is a ZIP file, you can extract this file like extract normally this type files. In the root, create folder
myfile | ... ├── META-INF │ └── manifest.xml ├── Scripts │ └── python │ └── mymacros.py ...
- Edit file manifest.xml into folder META-INF and add lines, just before tag close </manifest:manifest>
<manifest:file-entry manifest:full-path="Scripts/python/mymacros.py" manifest:media-type="" /> <manifest:file-entry manifest:full-path="Scripts/python/" manifest:media-type="application/binary" /> <manifest:file-entry manifest:full-path="Scripts/" manifest:media-type="application/binary" />
- The final file should see like:
<?xml version="1.0" encoding="UTF-8"?> <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0"> <manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="application/vnd.oasis.opendocument.spreadsheet"/> <manifest:file-entry manifest:full-path="Thumbnails/thumbnail.png" manifest:media-type="image/png"/> <manifest:file-entry manifest:full-path="settings.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="manifest.rdf" manifest:media-type="application/rdf+xml"/> <manifest:file-entry manifest:full-path="Configurations2/" manifest:media-type="application/vnd.sun.xml.ui.configuration"/> <manifest:file-entry manifest:full-path="meta.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/> <manifest:file-entry manifest:full-path="Scripts/python/mymacros.py" manifest:media-type="" /> <manifest:file-entry manifest:full-path="Scripts/python/" manifest:media-type="application/binary" /> <manifest:file-entry manifest:full-path="Scripts/" manifest:media-type="application/binary" /> </manifest:manifest>
- Now, zip the complete folder again. Caution, not zip extern folder, zip the content of folder