What is bound method python programming

Programming with Python scripts

A Python macro is a function in a .py file that is identified as a module. In contrast to LibreOffice Basic and its dozen functions or services of UNO objects, Python macros use the UNO single object XSCRIPTCONTEXT, together with JavaScript and BeanShell. The global tuple g_exportedScripts lists explicitly selectable macros from a module. Python modules have autonomous code logic and are independent of each other.

Global variable XSCRIPTCONTEXT

Real basic UNO functions can be derived from the global variable XSCRIPTCONTEXT. See the LibreOffice API for a full description of XSCRIPTCONTEXT. XSCRIPTCONTEXT methods are summarized:



In Basic as assigned

getDocument ()

The document reference that the script can work with.


getDesktop ()

The desktop reference on which the script can be run.


getComponentContext ()

The component context that the script can use to create other Uno components.


The shared installation scripts HelloWorld and Capitalize illustrate UNO-related macros that use the global variable XSCRIPTCONTEXT.

The standard Python output file is not available when starting Python macros via Tools - Macros - Run Macro ... For more information, see On-Screen Input / Output.

Module import

XSCRIPTCONTEXT is not made available to imported modules.

LibreOffice Basic libraries contain classes, routines and variables, Python modules contain classes, functions and variables. Common parts of reusable Python or UNO functions need to be stored in My Macros in (User Profile) / Scripts / python / pythonpath. Python libraries help organize modules to prevent module name collisions. Import uno.py into shared modules.

Real BASIC UNO facilities can be derived using the uno.py module. See Python Interactive Shell for a complete module description with the Python dir () and help () commands.



In Basic mapped as

absolutize ()

Returns an absolute file url from the specified urls.

createUnoStruct ()

Creates a UN structure or exception specified by typeName.

CreateUNOStruct ()

fileUrlToSystemPath ()

Returns a system path.

ConvertFromURL ()

getClass ()

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

getComponentContext ()

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

GetDefaultContext ()

Enum ()

getConstantByName ()

Finds the value of an IDL constant by its explicit name.

See API constant groups

isInterface ()

Returns True if obj is a class of a UNO interface.

systemPathToFileUrl ()

Returns a file url for the specified system path.

ConvertToURL ()

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

More Python Basic examples

Python UNO

Basic UNO functions

ctx = uno.getComponentContext ()

smgr = ctx.getServiceManager ()

obj = smgr.createInstanceWithContext (.., ctx)

CreateUnoService ()

See Open a Dialog

CreateUnoDialog ()

See Create 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)


desktop = smgr.createInstanceWithContext (DESK, ctx)

doc = desktop.CurrentComponent


Import an embedded module

Similar to LibreOffice Basic, which supports the browsing and dynamic loading of libraries, Python libraries can be browsed and imported if necessary. For more information on library containers, see the LibreOffice Application Programming Interface (API) or download the LibreOffice Software Development Kit (SDK).

Importing an embedded Python document module is shown below. The exception handling is not described in detail:

Imprint (Legal Info) | Privacy Policy | Statutes (non-binding English translation) - Statutes (binding German version) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.