Programmeren met Python-scripts

Een Python-macro is een functie binnen een .py-bestand, geïdentificeerd als een module. In tegenstelling tot LibreOffice Basic en zijn dozijn UNO-objecten functies of services, gebruiken Python-macro's de XSCRIPTCONTEXT UNO-object, gedeeld met JavaScript en Beanshell. De globale tuple g_exportedScripts vermeldt expliciet selecteerbare macro's uit een module. Python-modules bevatten autonome codelogica en zijn onafhankelijk van elkaar.

XSCRIPTCONTEXT Globale Variabele

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:

Methodes

Omschrijving

Toegewezen in Basic als

getDocument()

De documentreferentie waarop het script kan werken.

ThisComponent

getDesktop()

De documentreferentie waarop het script kan werken.

StarDesktop

getComponentContext()

De componentcontext die het script kan gebruiken om andere uno-componenten te maken.

GetDefaultContext


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

tip

Standaard uitvoerbestand van Python is niet beschikbaar bij het uitvoeren van Python-macro's via het menu Extra - Macro's - Macro uitvoeren. Bekijk Invoer/uitvoer naar scherm voor meer informatie.


Module-import

warning

XSCRIPTCONTEXT wordt niet voorzien voor geïmporteerde modules.


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.

Functies

Omschrijving

Toegewezen in Basic als

absolutize()

Retourneert een absolute URL van het bestand van de gegeven URL's.

createUnoStruct()

Maakt een UNO-struct of uitzondering, aangegeven door typeName.

CreateUNOStruct()

fileUrlToSystemPath()

Retourneert een systeempad.

ConvertFromURL()

getClass()

Geeft de klasse van een concrete UNO-uitzondering, structuur of interface terug.

getComponentContext()

Geeft de context van het UNO-component terug dat gebruikt wordt om de Python looptijd te initiëren.

GetDefaultContext()

Enum()

getConstantByName()

Zoekt de waarde van een IDL-constante op op basis van z'n explicite naam.

Zie API-constante groepen

isInterface()

Geeft Waar terug, als obj een klasse is van een UNO-interface.

systemPathToFileUrl()

Geeft een bestand-URL terug op basis van het systeempad.

ConvertToURL()


LibreLogo en TableSample installatie-gedeelde scripts gebruiken uno.py module.

Meer Python-Basic voorbeelden

Python UNO

Basis UNO-kenmerken

ctx = uno.getComponentContext()

smgr = ctx.getServiceManager()

obj = smgr.createInstanceWithContext( .. , ctx)

CreateUnoService()

Zie Openen van een Dialoogvenster

CreateUnoDialog()

Zie Het maken van een luisteraar

CreateUnoListener()

Zie UNO-gegevenstypes

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


Een ingebedde module importeren

Vergelijkbaar met LibreOffice Basic (dat het doorbladeren en het dynamisch laden van bibliotheken ondersteunt) kunnen Python-bibliotheken op verzoek worden doorzocht en worden geïmplementeerd. Voor meer informatie over bibliotheekcontainers, zie LibreOffice Application Programming Interface (API) of download LibreOffice Software Development Kit(SKD).

Het importeren van een Python-document embedded module is hieronder weergegeven, het afhandelingen van uitzonderingen is niet beschreven:


            import uno, sys
            
            def load_library(library_name: str, module_name=None):
                """ laad bibliotheek en importeer module
                
                Afgeleid van 'Bibliothèque de fonctions' door Hubert Lambert
                op https://forum.openoffice.org/fr/forum/viewtopic.php?p=286213"""
                doc = XSCRIPTCONTEXT.getDocument()  # huidige document
                url = uno.fileUrlToSystemPath( \
                    '{}/{}'.format(doc.URL, 'Scripts/python'+library_name))  # ConvertToURL()
                if not url in sys.path:  # voeg pad toe, wanneer nodig
                    sys.path.insert(0, url)  # doclib heeft voorrang
                if module_name:  # importeer, indien gevraagd
                    return zipimport.zipimporter(url).load_module(module_name)
            
            def import_embedded_python():
                ui = load_library("my_gui",'screen_io')  # voeg <lib> pad toe + importeer <module>
                ui.MsgBox(sys.modules.keys())
            
            g_exportedScripts = (import_embedded_python,)  # Public macros