LibreOffice Base libreoffice-base.png In case it's not obvious from its name, Base is part of the LibreOffice productivity suite, which includes Writer (word processing), Calc (spreadsheet), Impress (presentations), Draw (graphics), Charts (chart creation), and Math (formulas). Languages (Java, Python, C, LibreOffice 6.4 Basic, OLE) to illustrate the use of the API and demonstrate how to benefit from the included word processor, spreadsheet, presentation software, graphics program and database of LibreOffice 6.4. Some of the examples need a running office instance listening on a.
- LibreOffice / ˈ l iː b r ə / is a free and open-source office productivity software suite, a project of The Document Foundation (TDF). It was forked in 2010 from OpenOffice.org, which was an open-sourced version of the earlier StarOffice.The LibreOffice suite consists of programs for word processing, creating and editing of spreadsheets, slideshows, diagrams and drawings, working with.
- LibreOffice Basic libraries contain classes, routines and variables, Python modules contain classes, functions and variables. Common pieces of reusable Python or UNO features must be stored in My macros within (User Profile)/Scripts/python/pythonpath. Python libraries help organize modules in order to prevent module name collisions.
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 the script folder.
The main reason comes from your software – the LibreOffice and your script editor for python language need to using this path: C:Program Files (x86)LibreOffice 5shareScripts.
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 allows you to see the python files and also can be run with the Run button.
I used LibreOffice version: 126.96.36.199 to make one default python script.
I open my python script with the LibreOffice default interface ( not Writer, Calc, Base, etc.) and this allows me to run python scripts for any type of LibreOffice applications.
Let’s see the python script:
''Prints the Python version into the current document''
#if not, create a new one
#get the XText interface
#create an XTextRange at the end of the document
#add the python version to the string
tRange.String='The Python version is %s.%s.%s'%sys.version_info[:3]+' and the executable path is '+sys.executable
The output of the running script will be open under a 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 5programsoffice.bin.
The size of the text is 12, Default Style and the 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 to 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 syntax under Python 3 is:
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.)
These instructions are provided on Ubuntu 13.04 environment.
Libre Office 4.0 was the newest version that was installed in my PC. I was experimenting Macro programming in LibreOffice 4.0. Since Libre is newer the documentation provides for macro writing and the extension writing seems less. I spent days on fiding the stuff related with the topics, as I am new to this topic. Yet there are documentation linked with Apache OpenOffice project. The beginners find more troubles there.
Open Office Documentation
First open LibreOffice Writer and go through the following path.
- Tools->Macros->Organize Macros
Libreoffice Python Api
In the last submenu you may find both ‘Libre Office Basic’ and ‘Python’. If you get this menu click Python. Then a dialog will be appeared with available Python Macros.
If you did not get ‘Python’ along with ‘LibreOfficeBasic’, below instructions will get you there. First close LibreOffice and start the ubuntu terminal. Then install the package libreoffice-script-provider-python by typing the following command.
sudo apt-get install libreoffice-script-provider-python
As I was noted this package install the python3-uno package which works with some other python version(python 3.x) rather than python 2.7. Because of that when the libraries are imported to the Macro confilcts could occur. Becuase of that I installed python-uno package which refers to the python version 2.7 by running following command on the terminal.
sudo apt-get install python-uno
It will remove the already installed python3-uno. Now when you go to Tools->Macros->OrganizeMacros both ‘LibreOfficeBasic’ and ‘Python’ will be found. When you click python you will get the dialog shown below.
LibreOffice provides only the ‘Run’ facility for Python Macros. But for LibreOfficeBasic you will get all the facilities mentioned in the dialog. The macro “HelloWorld.py” is provided by the package script provider as a sample macro and it can be seen in the dialog.
Select the macro ‘LibreOfficeMacros->HelloWorld->HelloWorldPython’ and Run it. The string ‘Hello World’ will be printed on the opened LibreOffice Document. The code of ‘HelloWorld.py’ is shown below.
def HelloWorldPython( ):
- desktop = XSCRIPTCONTEXT.getDesktop()
- model = desktop.getCurrentComponent()
- if not hasattr(model, “Text”):
- model = desktop.loadComponentFromURL(
- “private:factory/swriter”,”_blank”, 0, () )
- text = model.Text
- tRange = text.End
- tRange.String = “Hello World (in Python)”
- return None
If the macro is not found in the dialog open a text document and paste the above mentioned code and save the file as ‘HelloWorld.py‘. Then move it to the directory,
You cannot just copy and paste, use terminal to copy the file.
Python Library For Libreoffice
sudo cp file_name_with_location /usr/lib/libreoffice/share/Scripts/python
Python Libreoffice Base
After that run the macro in LibreOffice. By following above steps you can write python macros and execute them. Try to build an extension.