2023-12-11

LibreOffice Python Macros

Python macros in LibreOffice are a powerful tool that can help automate repetitive tasks and increase productivity. There are three level of macros:

  1. System level : located in a subfolder of LibreOffice's installation directory (typically C:\Program Files\LibreOffice\share\Scripts\python), macros available for all users.
  2. User level: for Windows OS the location is %APPDATA%\LibreOffice\4\user\Scripts\python and are generally available only for a specific user.
  3. Document level: embedded in the document and accessible only when the document is open.

Libreoffice does not have an integrated text editor for python macros. However, they can be created with any text editor, including vim. Any LibreOffice document in reality is just a ZIP file, and VIM has a built-in plugin for editing ZIP files.

As shown in the video below the following Python script can create the proper folder structure and python macros files inside a LibreOffice document from the command line.

    #!/usr/bin/env python
    # filename: add_filefolder_to_LO.py
    # Import the modules
    import sys
    import zipfile
    import os

    # Get the zip file name from the first argument
    zip_file_name = sys.argv[1]

    # Create a ZipFile object in append mode
    zip_file = zipfile.ZipFile(zip_file_name, 'a')

    # Loop through the remaining arguments
    for arg in sys.argv[2:]:
      # Check if the argument starts with -d or -f
      if arg.startswith('-d'):
        # Get the folder name after the -d flag
        folder_name = arg[2:]
        # Create an empty folder in the zip file
        zip_file.writestr(folder_name + '/', '')
      elif arg.startswith('-f'):
        # Get the file name after the -f flag
        file_name = arg[2:]
        # Create an empty file in the zip file
        zip_file.writestr(file_name, '')
      else:
        # Ignore invalid arguments
        print('Invalid argument:', arg)

    # Close the zip file
    zip_file.close()

It's also necessary to include in the manifest.xml file, just before the closing tag, a reference to the added files and folders.

    <manifest:file-entry manifest:full-path="Scripts/python/documentmacros.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" />

A Python macro can be defined in this new Python file located within the file structure of the LibreOffice document. As shown in the video, it is just a standard Python function that interacts with LibreOffice components using the PyUNO module, which provides access to the LibreOffice API.

Python Console

LibreOffice has its own Python console, which is in the same folder where the main soffice.exe binary is located. In this REPL, you can import the PyUNO module.

Typically from the command prompt:

   ...> "C:\Program Files\LibreOffice\program\python.exe"

When you pass the module name (uno) to the help() function, it displays part of the documentation, as shown in the figure below.