Python Guide - Introduction

From The Document Foundation Wiki
Jump to: navigation, search


Return to Index

Other languages:
English • ‎español • ‎français • ‎italiano


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).

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

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.
 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


Return to Index