Python Guide - Introduction

    From The Document Foundation Wiki


    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+

    sudo apt install libreoffice-script-provider-python

    Test support for Python macros

    Open a new document in Writer. Select menu Tools ▸ Macros ▸ Run macro..., the "Macro Selector" dialog appears. In the Library section select LibreOffice Macros ▸ HelloWorld, in the Macro Name section select HelloWorldPython, and click button Run.

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

    Test macro python

    Where to 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

    • GNU/Linux
    /usr/lib/libreoffice/share/Scripts/python/

    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 </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 be 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

    Return to Index