Ajuda del LibreOffice 25.2
Una macro Python és una funció dins d'un fitxer .py identificat com un mòdul. A diferència del LibreOffice Basic i la seua dotzena d'objectes unO funcions o serveis macros Python utilitzen un sol objecte XSCRIPTCONTEXT UNO compartit amb JavaScript i BeanShell. Els mòduls gexportedScripts globals llista explícitament macros seleccionables des d'un mòdul. Els mòduls Python mantenen una lògica de codi autònoma i són independents entre ells.
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:
| Mètodes | Descripció | Al Basic es correspon amb | 
|---|---|---|
| getDocument() | La referència del document en el que pot operar l'script. | ThisComponent | 
| getDesktop() | La referència de l'escriptori en el que pot operar l'script. | StarDesktop | 
| getComponentContext() | The component context which the script can use to create other uno components. | GetDefaultContext | 
GA HelloWorld i Capitalise instal·lació scripts compartits il·lustren macros relacionades amb l'UNO fent ús de la variable global XSCRIPTCONTEXT.
Python standard output file is not available when running Python macros from menu. Refer to Input/Output to Screen for more information.
GA XSCRIPTCONTEXT no es proporciona als mòduls importats.
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.
| Funcions | Descripció | Al Basic es correspon amb | 
|---|---|---|
| absolutize() | Retorna un URL de fitxer absolut dels URL indicats. | |
| createUnoStruct() | Creates a UNO struct or exception given by typeName. | CreateUNOStruct() | 
| fileUrlToSystemPath() | Retorna un camí del sistema. | 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. | Vegeu els grups constants d'API | 
| isInterface() | Retorna Cert, si l'objecte és una classe d'una interfície UNO. | |
| systemPathToFileUrl() | Retorna un URL de fitxer per al camí de sistema indicat. | ConvertToURL() | 
LibreLogo, NamedRanges, SetCellColor and TableSample preinstalled scripts use uno.py module.
| UNO en Python | Funcionalitats bàsiques de l'UNO | 
|---|---|
| ctx = uno.getComponentContext() smgr = ctx.getServiceManager() obj = smgr.createInstanceWithContext( .. , ctx) | CreateUnoService() | 
| See Opening a Dialog | CreateUnoDialog() | 
| CreateUnoListener() | |
| Vegeu els tipus de dades UNO | CreateUnoValue() CreateObject() | 
| Use "==" or "!=" comparison operators | 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 | 
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:
            import uno, sys, zipimport
            
            def load_library(library_name: str, module_name=None):
                """ càrrega de la biblioteca i importació del mòdul
                
                Adaptat de «Bibliothèque de fonctions» d'Hubert Lambert
                a https://forum.openoffice.org/fr/forum/viewtopic.php?p=286213"""
                doc = XSCRIPTCONTEXT.getDocument()  # document actual
                url = uno.fileUrlToSystemPath( \
                    '{}/{}'.format(doc.URL, 'Scripts/python'+library_name))  # ConvertToURL()
                if not url in sys.path:  # addició del camí, si cal
                    sys.path.insert(0, url)  # doclib takes precedence
                if module_name:  # importació, si s'ha sol·licitat
                    return zipimport.zipimporter(url).load_module(module_name)
            
            def import_embedded_python():
                ui = load_library("my_gui",'screen_io')  # add <lib> path + import <module>
                ui.MsgBox(sys.modules.keys())
            
            g_exportedScripts = (import_embedded_python,)  # Public macros