Python Language Binding

From Apache OpenOffice Wiki
< Python
Revision as of 03:42, 22 June 2013 by Hanya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


The Python language binding allows developers to work with UNO in Python language.

This page describes detaile of the binding. Writing macros or UNO component in Python is described in dedicated pages for them.

uno Module

The binding is implemented in Python extension module pyuno and additional module uno. Some types are defined in uno module and the binding is not work without these values.

Classes for Types

uno.Enum(typeName, value)

uno.Type(typeName, typeClass)

uno.Char(value)

uno.ByteSequence(value)

uno.Any(type, value)

Module Functions

uno.getComponentContext()

uno.getConstantByName(constant)

uno.getTypeByName(typeName)

uno.createUnoStruct(typeName, *args)

uno.getClass(typeName)

uno.isInterface(obj)

uno.generateUuid()

uno.systemPathToFileUrl(systemPath)

uno.fileUrlToSystemPath(url)

uno.absolutize(path, relativeUrl)

uno.getCurrentContext()

uno.setCurrentContext(newContext)

uno.invoke(object, methodname, argTuple)

Type Mappings

The following table shows type mapping between UNO and Python for simple types.

UNO Python
void None
boolean bool
byte long/int[1]
short long
unsigned short long
long long
unsigned long long
hyper long
unsigned hyper long
float float
double float
char uno.Char
string unicode/str[2]
type uno.Type
any uno.Any[3]
  • [1] No long type is there in Python 3, the value converted into int.
  • [2] Python 3 uses Unicode string as normal string.
  • [3] It is used only with uno.invoke method.

Mapping of Integer Types

The numerical types are converted to different types depending on their range of value.

Mapping of String

Any string type is coming from UNO is unicode even that contain only 7 bit range characters. Both unicode and str are valid string to pass them to UNO.

Since Python 3, only str is valid but bytes is no longer converted as string type.

Maping of Type

This type is defined in uno module that constructor takes type name of the type and type class of the type. The new instance of the type will be checked by the binding to avoid invalid type. Its instance variables can be changed after the instantiation, but do not try to change them.

New value can be taken also with uno.getTypeByName() method without type class to specify.

The import hook provided by uno module allows to instantiate uno.Type in import statement with typeOf prefix. For example, from com.sun.star.lang import typeOfXEventListener adds typeOfEventListener variable and set its type value to it.

Maping of Any

This type is not passed from UNO because the value wrapped by it is extracted and converted into Python value.

The usage of this type is to tell the type of value with uno.invoke() method.

Personal tools