Python Uno Libreoffice

admin

Description There are a lot of python libraries for dealing with Libre/OpenOffice via API (UNO). As for me, one of the most interesting projects is pyoo. It supports a lot of feature from open/save documents up to cell merging and working with charts and diagrams. But none of them implements number of functions wich I need. Online searching turned up some tutorials about automating LibreOffice with Python and Excel with Perl, and also, more importantly, a Perl module that can indeed interface with LibreOffice. Frustratingly though, the docs for that module seem to consist of a single example and a rather unhelpful suggestion to go read the API docs. Conflicts: python3-uno, python3.3-uno: Description: Python-UNO bridge (support for old python 2) The Python-UNO bridge allows use of the standard LibreOffice API: with the Python scripting language. It additionally allows: others to develop UNO components in Python, thus Python UNO components. Reference documentation of the C UNO runtime and a set of C/C base and helper functions and classes to abstract from the system layer. Development Tools: Overview and a short description of the UNO development tools. Examples: A collection of examples in different programming languages (Java, Python, C, Basic, OLE, CLI).

This question is seeking advice from the experts. I have a LibreOffice spreadsheet that is largely based upon Basic macros, but have set up a Python/Latex subsystem that can produce formatted PDF reports by assembling various Latex templates, with such things as barcodes, qrcodes and other art/images. This is done by reading tex templates into Python Templates, passing to them a data dictionary (that would be loaded with spreadsheet data), writing them as temp files to disk, and then compiling them with Latex. What I need to do is connect the data from the spreadsheet to the subsystem. There is no need to return a value to the spreadsheet, just to produce the pdf report on disk (for now).
Firstly there is a need to pass the data via Basic to the Python subsystem. Secondly I am concerned about portability, ie I would like to simply pass a file that can be installed easily on another machine (spreadsheet and subsystem), that just works (on a machine that has xelatex of course). Thirdly there are various approaches:
1. Write an extension to LibreOffice calc
2. Launch a separate process by macro
3. Write a wrapper function for Python (is this possible?)
4. Are plugins possible (see 1 above?)
UnoI had hoped that I could use LibreOffice's own built in PDF facilities to produce this output. However the difficulty is the production of barcodes, qrcodes, and other art, latex is much more flexible in this respect.
This initial item has about 15 data elements that need to be passed from the spreadsheet to Python, but I can forsee much more complex and elaborate reports in future. What would be your advice as to the best way to structure the system, and the best way to pass the data?

A Python macro is a function within a .py file, identified as a module. Unlike LibreOffice Basic and its dozen of UNO objects functions or services, Python macros use the XSCRIPTCONTEXT UNO single object, shared with JavaScript and BeanShell. The g_exportedScripts global tuple explicitly lists selectable macros from a module. Python modules hold autonomous code logic, and are independent from one another.

XSCRIPTCONTEXT Global Variable

Genuine Basic UNO facilities can be inferred from XSCRIPTCONTEXT global variable. Refer to LibreOffice API for a complete description of XSCRIPTCONTEXT. XSCRIPTCONTEXT methods summarize as:

Python Libreoffice Calc

Methods

Description

Mapped in Basic as

getDocument()

The document reference on which the script can operate.

ThisComponent

getDesktop()

The desktop reference on which the script can operate.

StarDesktop

getComponentContext()

The component context which the script can use to create other uno components.

GetDefaultContext


Python Uno Libreoffice

HelloWorld and Capitalise installation shared scripts illustrate UNO-related macros making use of XSCRIPTCONTEXT global variable.

Python standard output file is not available when running Python macros from Tools - Macros - Run Macro menu. Refer to Input/Output to Screen for more information.


Module import

XSCRIPTCONTEXT is not provided to imported modules.

PythonPython uno

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. Import uno.py inside shared modules.

Genuine BASIC UNO facilities can be inferred using uno.py module. Use Python interactive shell to get a complete module description using dir() and help() Python commands.

Functions

Description

Mapped in Basic as

absolutize()

Returns an absolute file url from the given urls.

createUnoStruct()

Creates a UNO struct or exception given by typeName.

CreateUNOStruct()

fileUrlToSystemPath()

Returns a system path.

ConvertFromURL()

getClass()

Returns the class of a concrete UNO exception, struct, or interface.

getComponentContext()

Returns the UNO component context used to initialize the Python runtime.

GetDefaultContext()

Enum()

getConstantByName()

Looks up the value of an IDL constant by giving its explicit name.

See API constant groups

isInterface()

Returns True, when obj is a class of a UNO interface.

systemPathToFileUrl()

Returns a file URL for the given system path.

ConvertToURL()

Python

LibreLogo and TableSample installation shared scripts use uno.py module.

More Python-Basic samples

Python UNO

Basic UNO features

ctx = uno.getComponentContext()

smgr = ctx.getServiceManager()

obj = smgr.createInstanceWithContext( .. , ctx)

CreateUnoService()

See Opening a Dialog

CreateUnoDialog()

See Creating a Listener

CreateUnoListener()

See UNO data types

CreateUnoValue()

CreateObject()

EqualUnoObjects()

ctx = uno.getComponentContext()

smgr = ctx.getServiceManager()

GetProcessServiceManager()

def hasUnoInterfaces(obj, *interfaces):

return set(interfaces).issubset(t.typeName for t in obj.Types)

HasUnoInterfaces()

IsUnoStruct()

ctx = uno.getComponentContext()

smgr = ctx.getServiceManager()

DESK = 'com.sun.star.frame.Desktop'

desktop = smgr.createInstanceWithContext(DESK , ctx)

StarDesktop

desktop = smgr.createInstanceWithContext(DESK , ctx)

doc = desktop.CurrentComponent

ThisComponent


Importing an embedded Module

Similarly to LibreOffice Basic that supports browsing and dynamic loading of libraries, Python libraries can be explored and imported on demand. For more information on library containers, visit LibreOffice Application Programming Interface (API) or download LibreOffice Software Development Kit (SDK).

Importing a Python document embedded module is illustrated below, exception handling is not detailed:

Python Uno Libreoffice Tutorial

Related Topics