LibreOffice 26.8 Hjälp
Det går att anropa Python-skript från LibreOffice Basic-makron och bland annat åstadkomma följande viktiga funktioner:
Det går att identifiera ComputerName eller upptäcka OSName,
Basic-funktionen FileLen() och com.sun.star.ucb.SimpleFileAccess.API-funktionen getSize() har en övre gräns på 2 Gigabyte filstorlek som Python hjälper till att övervinna,
com.sun.star.util.PathSettings kan normaliseras,
och många fler.
A reasonable exposure to LibreOffice Basic and to Application Programming Interface (API) features is recommended prior to perform inter-language calls from Basic to Python, to JavaScript or any other script engine.
Python scripts can be personal, shared, or embedded in documents. In order to execute them, LibreOffice Basic needs to be provided with Python script locations. Locating com.sun.star.script.provider.XScript interface compliant UNO objects allows the execution of Python scripts:
Option Explicit
Public Function GetPythonScript(macro As String, _
Optional location As String) As com.sun.star.script.provider.Xscript
''' Grab Python script object before execution
' Argument:
' macro : as "library/module.py$macro" or "module.py$macro"
' location: as "document", "share", "user" or ENUM(eration)
' Resultat:
' hittade com.sun.star.script.provider.XScript UNO-tjänst'''
If IsMissing(location) Then location = "user"
Dim mspf As Object ' com.sun.star.script.provider.MasterScriptProviderFactory
Dim sp As Object ' 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 is an Array
file_len = script.invoke(Array(systemFilePath), Array(), Array())
normalizedPath = script.invoke(Array(systemFilePath), Array(), Array())
Below ComputerName, and GetFilelen routines are calling their Python counterparts, using aforementioned GetPythonScript function. Exception handling is not detailed.
Option Explicit
Option Compatible ' Properties are supported
Private scr As Object ' com.sun.star.script.provider.XScript
Private Property Get ComputerName As String
'''Workstation name'''
scr = GetPythonScript("Platform.py$computer_name", "document")
ComputerName = scr.invoke(Array(), Array(), Array())
End Property ' ComputerName
Private Function GetFilelen(systemFilePath As String) As Currency
'''File size in bytes'''
scr = GetPythonScript("Os/Path.py$get_size", Script.ISEMBEDDED)
GetFilelen = scr.invoke(Array(systemFilePath), Array(), Array(),)
End Function ' GetFilelen
Private Type _SCRIPT_LOCATION
ISEMBEDDED As String ' document script
ISPERSONAL As String ' user script
ISSHARED As String ' LibreOffice macro
End Type ' _SCRIPT_LOCATION
Public Function Script() As Object ' Text enumeration
Static enums As _SCRIPT_LOCATION : With enums
If .ISEMBEDDED = "" Then
.ISEMBEDDED = "document" ' document script
.ISPERSONAL = "user" ' user scripts
.ISSHARED = "share" ' LibreOffice macro
End If : End With ' enums
Script = enums
End Function ' Script
Two different Python modules are called. They can either be embedded in the current document, either be stored on the file system. Argument type checking is skipped for clarity:
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)
The calling mechanism for personal or shared Python scripts is identical to that of embedded scripts. Library names are mapped to folders. Computing LibreOffice user profile and shared modules system file paths can be performed as detailed in Getting session information. Below OSName, HelloWorld and NormalizePath routines are calling their Python counterparts, using aforementioned GetPythonScript function. Exception handling is not detailed.