LinkedInTwitterFacebook

Python and LibreOffice – part 001.

As you know the LibreOffice includes several applications that make it the most powerful Free and Open Source office suite on the market: Writer (word processing), Calc (spreadsheets), Impress (presentations), Draw (vector graphics and flowcharts), Base (databases), and Math (formula editing).
This tutorial is about LibreOffice and python scripts used like macros.
First you need to assign the permission for your user to write into script folder.
The main reason come from your software – the LibreOffice and your script editor for python language need to using this path: C:\Program Files (x86)\LibreOffice 5\share\Scripts.
If you using one of the linux OS then he path will be: /Applications/LibreOffice.app/Contents/Resources/Scripts/python/.
Your script can be run using the LibreOffice macro : Tools – Macros – Organize Macros – Python.
This allow you to see the python files and also can be run with Run button.
I used LibreOffice version: 5.2.3.3 to make one default python script.
I open my python script with the LibreOffice default interface ( not Writer, Calc, Base, etc.) and this allow me to run python scripts for any type of LibreOffice apllications.
Let’s see the python script:

The output of the running script will be open under new document named Untitled 1.
The content of this result is: The Python version is 3.3.5 and the executable path is C:\Program Files (x86)\LibreOffice 5\program\soffice.bin.
The size of the text is 12, Default Style and font is Liberation Serif.
The python script is simple: Is like take the interface model and put some text into that.
About hasattr part is just for reduce your possible errors and avoid confusing differences in behavior between Python 2 and 3.
Don’t use Python’s hasattr() unless you’re writing Python 3-only code and understand how it works.
The sintax under Python 3 is:
hasattr(object, name)

The arguments are an object and a string. The result is True if the string is the name of one of the object’s attributes, False if not. (This is implemented by calling getattr(object, name) and seeing whether it raises an AttributeError or not.)