LibreOffice 25.8 Súgó
A Python-makró egy .py fájlban lévő, modulként azonosított függvény. A LibreOffice Basic és a tucatnyi UNO-objektum függvény vagy szolgáltatás használatával ellentétben a Python-makrók a XSCRIPTCONTEXT UNO egyszeres objektumot használják, amelyet a JavaScript és a BeanShell is használ. A g_exportedScripts globális tuple explicit módon felsorolja a modulból kiválasztható makrókat. A Python-modulok önálló kódlogikát tartalmaznak, és egymástól függetlenek.
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:
| Metódusok | Leírás | 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 | 
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 menu. Refer to Input/Output to Screen for more information.
XSCRIPTCONTEXT is not provided to imported modules.
LibreOffice Az alapkönyvtárak osztályokat, rutinokat és változókat, a Python-modulok osztályokat, függvényeket és változókat tartalmaznak. Az újrafelhasználható Python vagy UNO funkciók közös darabjait a Saját makrókban kell tárolni a (User Profile)/Scripts/python/pythonpath alatt. A Python könyvtárak segítenek a modulok rendszerezésében, hogy elkerüljék a modulnevek ütközését. Importálja az uno.py-t a megosztott modulokon belül.
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.
| Függvények | Leírás | 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() | 
LibreLogo, NamedRanges, SetCellColor and TableSample preinstalled scripts use uno.py module.
| Python UNO | Az UNO alapvető jellemzői | 
|---|---|
| ctx = uno.getComponentContext() smgr = ctx.getServiceManager() obj = smgr.createInstanceWithContext( .. , ctx) | CreateUnoService() | 
| See Opening a Dialog | CreateUnoDialog() | 
| CreateUnoListener() | |
| See UNO data types | 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):
                """ load library and import module
                
                Adapted from 'Bibliothèque de fonctions' by Hubert Lambert
                a következő oldalon: https://forum.openoffice.org/fr/forum/viewtopic.php?p=286213"""
                doc = XSCRIPTCONTEXT.getDocument()  # current document
                url = uno.fileUrlToSystemPath( \
                    '{}/{}'.format(doc.URL, 'Scripts/python'+library_name))  # ConvertToURL()
                if not url in sys.path:  # add path if necessary
                    sys.path.insert(0, url)  # doclib takes precedence
                if module_name:  # import if requested
                    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