LibreOffice 24.2 Hjelp
Å anrope Python-skript fra LibreOffice Grunnleggende makroer, er mulig, og verdifulle funksjoner kan nås som:
Maskinnavnidentifikasjon eller OSName oppdagelse er mulig,
Basic FileLen() funksjon og com.sun.star.ucb.SimpleFileAccess.getSize() API-funksjonen viser en øvre grense på 2 Gigabyte filstørrelse som Python hjelper med å overstyre,
com.sun.star.util.PathSettings kan bli normalisert,
og mange flere
A reasonable exposure to LibreOffice Basic and to Application Programming Interface (API) funksjoner anbefales før du utfører inter-språk samtaler fra Basic til Python, til JavaScript eller en annen skriptmotor.
Python-skript kan være personlige, delte eller innebygde i dokumentene. For å kjøre de, må LibreOffice Basic være utstyrt med funksjon, finne Python-skriptplasseringer.com.sun.star.script.provider.XScript grensesnittkompatible UNO-objekter tillater kjøring av Python-skript:
         Option Explicit
             
         Public Function GetPythonScript(macro As String, _
                 Optional location As String) As com.sun.star.script.provider.Xscript
             "'Grip Python-skriptobjekt før utførelse
             ' Argumenter:
             '    makro   : som "library/module.py$macro" eller "module.py$macro"
             '    lokasjon: som "dokument", "del", "bruker" eller ENUM(erasjon)
             ' Resultat:
             '    plassert com.sun.star.script.provider.XScript UNO service'''
             If IsMissing(location) Then location = "user"
             Dim mspf As Object ' com.sun.star.script.provider.MasterScriptProviderFactory
             Dim sp som objekt ' com.sun.star.script.provider.XScriptProvider compatible
             Dim uri As String
             If location="document" Then
                 sp = ThisComponent.getScriptProvider()
             Else
                 mspf = CreateUnoService("com.sun.star.script.provider.MasterScriptProviderFactory")
                 sp = mspf.createScriptProvider("")
             End If
             uri = "vnd.sun.star.script:"& macro &"?language=Python&location="& location
             GetPythonScript = sp.getScript(uri)
         End Function ' GetPythonScript
      workstation_name = script.invoke(Array(), Array(), Array())
opSysName = script.invoke(Array(), in_outs, Array()) ' in_out er en matrise
file_len = script.invoke(Array(systemFilePath), Array(), Array())
normalizedPath = script.invoke(Array(systemFilePath), Array(), Array())
Under Maskinnavn, og GetFilelen rutiner kaller Python-kollegene sine, ved hjelp av nevnte GetPythonScript -funksjon. Unntakshåndteringen er ikke detaljert.
         Option Explicit
         Alternativet, kompatible 'Egenskaper støttes
             
         Private scr As Object ' com.sun.star.script.provider.XScript
             
         Private Property Get ComputerName As String
             '' 'Arbeidsstasjonsnavn' ''
             scr = GetPythonScript("Platform.py$computer_name", "document")
             ComputerName = scr.invoke(Array(), Array(), Array())
         End Property ' ComputerName
             
         Private Function GetFilelen(systemFilePath As String) As Currency
             '' 'Filstørrelse i byte' ''
             scr = GetPythonScript("Os/Path.py$get_size", Script.ISEMBEDDED)
             GetFilelen = scr.invoke(Array(systemFilePath), Array(), Array(),)
         End Function ' GetFilelen
             
         Private Type _SCRIPT_LOCATION
             ISEMBEDDED Som streng 'dokumentskript
             ISPERSONAL Som streng 'brukerskript
             ISSHARED som Streng ' LibreOffice makro
         End Type ' _SCRIPT_LOCATION
             
         Public Function Script() As Object ' Text enumeration
             Static enums As _SCRIPT_LOCATION : With enums
             If .ISEMBEDDED = "" Then
                 .ISEMBEDDED = "dokument" ' dokument skript
                 .ISPERSONAL = "bruker" ' brukerskript
                 .ISSHARED = "del" ' LibreOffice makro
             End If : End With ' enums
             Script = enums
         End Function ' Script
      To forskjellige Python-moduler anropes. De kan enten være innebygd i det gjeldende dokumentet, eller lagres i filsystemet. Kontroll av argumenttype hoppes over for tydlighet:
Platform.py
         # -*- coding: utf-8 -*-
         from __future__ import unicode_literals
          
         import platform
          
         def computer_name() -> str:
             return platform.node()
          
         def OSname() -> str:
             return platform.system()
      Os/Path.py
         # -*- coding: utf-8 -*-
         from __future__ import unicode_literals
          
         import os.path
          
         def get_size(systemFilePath: str) -> str:
             return str(os.path.getsize(systemFilePath))
          
         def normalyze(systemPath: str) -> str:
             return os.path.normpath(systemPath)
      Oppkallemekanismen for personlige eller delte Python-skript er identisk med den for innebygde skript. Biblioteknavn blir tilordnet mapper. Beregning av LibreOffice brukerprofil og systemfilstier for delte moduler kan utføres som beskrevet i Få øktinformasjon . Under OSName , HelloWorld og NormalizePath rutiner kaller Python-kollegene sine ved å bruke nevnte GetPythonScript -funksjon. Unntakshåndteringen er ikke detaljert.
         Option Explicit
         Alternativet kompatible 'Egenskaper støttes
             
         Private scr As Object ' com.sun.star.script.provider.XScript
             
         Private Property Get OSName As String
             '''Plattform navn som "Linux", "Darwin" eller "Windows"'''
             scr = GetPythonScript("Platform.py$OSname", Script.ISPERSONAL)
             OSName = scr.invoke(Array(), Array(), Array()) 
         End Property ' OSName
             
         Private Sub HelloWorld()
             '''LibreOffice Python delte eksempler'''
             scr = GetPythonScript("HelloWorld.py$HelloWorldPython", Script.ISSHARED)
             scr.invoke(Array(), Array(), Array(),)
         End Sub ' HelloWorld
             
         Public Function NormalizePath(systemFilePath As String) As String
             '''Strip overflødig '\..' i banen'''
             scr = GetPythonScript("Os/Path.py$normalyze", "user")
             NormalizePath = scr.invoke(Array(systemFilePath), Array(), Array())
         End Function ' NormalizePath
      LibreOffice innebygd Python inneholder mange standardbiblioteker å dra nytte av. De har et rikt funksjonssett, for eksempel, men ikke begrenset til:
argparse Parser for kommandolinjealternativer, argumenter og underkommandoer
cmath Matematiske funksjoner for komplekse tall
csv CSV-filer lesing og skriving
datetime Ekte dato og tidstyper
json JSON koder og dekoder
math Matematiske funksjoner
re Regulære uttrykk operasjoner
socket Nettverksgrensesnitt på lavt nivå
sys Systemspesifikke parametere og funksjoner
unittest and trace Enhetstesting rammeverk og Spor Python-kjøring
xml.etree.ElementTree ElementTree XML API