Python Macros in LibreOffice on a Macbook
To be able to execute Python macros in LibreOffice it is necessary to configure your macOS to be able to execute the code. First of all, you need to install LibreOffice.
There are several ways to store Macros in LibreOffice:
- Macros could be used by all users of the device
- Macros could only be executed by the current user
- Macros could be executed by the user of a document or template
The macros must be stored in different paths if you want one of those three options.
For all users
Macros that should be used by all users of an Apple device must be stored in
/Applications/LibreOffice.app/Contents/Resources/Scripts/python/
Store your macros as usual in a file with the filename <fileName>.py, After that you will be able to see the file inside the macro list in LibreOffice
For the current user only
Macros that should be used by all users of an Apple device must be stored in
–/Library/Application Support/LibreOffice/4/user/Scripts/python
In my case, the subpath Scrips/python did not exist so I had to create the path.
mkdir -p –/Library/Application Support/LibreOffice/4/user/Scripts/python
If you store your python files here they will be visible in LibreOffice.
For the user of the current document or template
In this case Script/python folder must be created inside the document itself. Each LibreOffice file, whether it is an ods, odt or odp file, is some kind of a zip file. If go to the filesystem and unzip the file
unzip <myFile>.<odt|ods|odp>
A structure of subdirectories will be created containing several kinds of files. It may look like this:
To add a Python macro to your document you need to create two subdirectories inside this directory structure. If you work with some kind of unix-like OS execute
mkdir -p Scripts/python
and copy your macro into the python directory. Last but not least you need to zip the structure again into an ods, odt or odp file.
cd directly to the top-level folder of your unzipped file like you see in the picture and execute the zip command
zip -r - . |dd of=~/<yourTargetDirectory>/<yourFileName>.<ods|odt|odp>
This command may look strange to you. The parameter -r includes all subdirectories, and the dash - sends the output to the standard output (the screen). This will be used by dd (disk dump) to create a new file.
If you open the file in LibreOffice it will be recognized as a non-regular file because you added some files to the file structure. LibreOffice can repair this and give you the ability to open the document. The macro will be visible but cannot be executed because of the macro security in LibreOffice. You can adjust this behavior in the settings menu.
The next article will explain how to create a macro that adds some text and tables to your document.
have fun!