2020-09-13

50: Create LibreOffice Extension That Contains Python Macros

<The previous article in this series | The table of contents of this series | The next article in this series>

This technique should work for also your OpenOffice extension. Using the extension, you can distribute your Python macros handily.

Topics


About: UNO (Universal Network Objects)
About: LibreOffice
About: Apache OpenOffice
About: The Python programming language

The table of contents of this article


Starting Context


  • The reader has a basic knowledge on LibreOffice or Apache OpenOffice.
  • The reader has a basic knowledge on the Python programming language.

Target Context


  • The reader will know how to create his or her LibreOffice or Apache OpenOffice extension that contains any Python macros.

Orientation


Creating your user-owned or application-owned Python macro has been addressed in a previous article.

Creating your in-document Python macro has been addressed in the previous article.

Executing any macro (user-owned, application-owned, or in-document) function from your UNO program will be addressed in a future article.

The details of Python macro programming will be dug into in some future articles.

Using an external full Python has been addressed in a previous article.

Stage Direction
Here are Hypothesizer 7, Objector 50A, and Objector 50B in front of a computer.


Main Body


1: You Can Distribute a Set of Python Macros, in Your LibreOffice or Apache OpenOffice Extension


Hypothesizer 7
If you are going to distribute a set of LibreOffice or Apache OpenOffice Python macros, including them in your extension may be a good option.

Objector 50A
"extension" is something that can be installed via the 'Extension Manager...' menu item, right?

Hypothesizer 7
Yes, it is, sir.

Objector 50A
I thought that any LibreOffice extension was something made by someone else.

Hypothesizer 7
. . . There is no reason why that "someone" cannot be you, sir.

Objector 50A
But it's difficult, right? "extension" sounds difficult.

Hypothesizer 7
Creating your extension is quite easy, in fact.

Objector 50A
People like you say so easily!

Hypothesizer 7
Well, LibreOffice or Apache OpenOffice extension is really a ZIP file; can't you create a ZIP file?

Objector 50A
Don't belittle me! I can create a ZIP file!

Hypothesizer 7
And you can edit a file, can't you?

Objector 50A
How can I create my Python macro, without being able to edit a file?

Hypothesizer 7
Indeed, sir.

Objector 50A
But I don't want to edit any long file.

Hypothesizer 7
Well, my sample here is 351 bytes length.

Objector 50A
Your sample is unusually short, right?

Hypothesizer 7
In fact, you can use the sample as it is, if you do not include some other kinds of artifacts in the extension.

Objector 50A
Hm, is that all?

Hypothesizer 7
You also have to be able to create some directories and copy your macro files.

Objector 50A
I knew it! You add more and more things like that; "you have to be able to do this; you have to be able to do that", and so on! You are despicable!

Hypothesizer 7
Actually, that is all.

Objector 50A
. . .

Objector 50B
Do I really need to create the extension? Doesn't just copying the macro files do?

Hypothesizer 7
It may, madam: as described in a previous article, just locating any macro file at a specific location makes it recognized as a macro.

However, the locations for the user-owned macro file may differ among environments, and the user may not have the permission to copy the macro file to the application-owned macro location. And packing also some other related files in the extension will be desirable.

Objector 50B
What do you mean by "some other related files"?

Hypothesizer 7
You may distribute also some UNO services (implemented in Java, for example), some GUI configuration files, and some key bindings configuration files.

Objector 50B
Ah, adding a menu item that invokes a macro . . .


2: Creating the Extension


Hypothesizer 7
Let us create the extension.

First, we create the extension ingredients base directory.

Stage Direction
Hypothesizer 7 opens a terminal in the Linux computer, and change the current directory to a specific location.

Objector 50A
You are using a terminal! You have changed the current directory!

Hypothesizer 7
Is that wrong?

Objector 50A
You didn't say that I had to be able to do them! You lied!

Hypothesizer 7
No, I did not: they are not particularly requirements; you can do your operations via a files explorer, if you like.

Objector 50A
Umm . . .

Stage Direction
Hypothesizer 7 executes these commands.

@bash Source Code
mkdir extensionAIngredients
cd extensionAIngredients

Hypothesizer 7
We create the meta files directory and the Python macros base directory.

Stage Direction
Hypothesizer 7 executes these commands.

@bash Source Code
mkdir META-INF
mkdir -p Scripts/python

Hypothesizer 7
We copy the Python module files or the Python package directories to the Python macros base directory.

Stage Direction
Hypothesizer 7 executes this command.

@bash Source Code
cp -r %somewhere%/theBiasPlanet Scripts/python/.

Objector 50A
Huh? What is that "theBiasPlanet" thing?

Hypothesizer 7
That is my Python package directory, under which there is a Python module file, 'pythonEnvironmentChecker/macros/PythonEnvironmentChecker.py'.

Objector 50A
Ah, so, Python macros can be in such deep directories . . .

Hypothesizer 7
And we edit a manifest file, 'META-INF/manifest.xml', with these contents.

@manifest.xml Source Code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">
<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
	<manifest:file-entry manifest:media-type="application/vnd.sun.star.framework-script" manifest:full-path="Scripts/python"/>
</manifest:manifest>

Stage Direction
Hypothesizer 7 executes this command, put the above contents in it, and save the file.

@bash Source Code
vim META-INF/manifest.xml

Hypothesizer 7
As the ingredients have been fully prepared, at last, we create the extension file.

Stage Direction
Hypothesizer 7 executes this command.

@bash Source Code
zip -r ../theBiasPlanet.pythonEnvironmentChecker.unoExtension.oxt *

Objector 50A
. . . . . . And?

Hypothesizer 7
That is all: 'theBiasPlanet.pythonEnvironmentChecker.unoExtension.oxt' is the extension.

Objector 50A
That's too easy, right?

Hypothesizer 7
I said that it was quite easy.


3: Testing


Hypothesizer 7
Let us install the extension.

Stage Direction
Hypothesizer 7 starts a LibreOffice instance, selects 'Tools' -> 'Extension Manager...' -> 'Add' -> the extension -> 'Open' -> 'Only for me' -> 'Close' -> 'Restart Now'. A LibreOffice instance is up again.

Hypothesizer 7
Let us execute a macro function in the extension.

Stage Direction
Hypothesizer 7 opens a Writer document and selects 'Tools' -> 'Macros' -> 'Run Macro...' -> 'My Macros' -> 'theBiasPlanet.pythonEnvironmentChecker.unoExtension.oxt' -> 'theBiasPlanet' -> 'pythonEnvironmentChecker' -> 'macros' -> 'PythonEnvironmentChecker' -> 'checkPythonEnvironment' -> 'Run'. Some things are written into the document.

Objector 50A
So, it has failed, right?

Hypothesizer 7
. . . It has succeeded.


References


<The previous article in this series | The table of contents of this series | The next article in this series>