Κλήση Python Scripts από Basic
Κλήση δέσμης ενεργειών (scripts) Python από μακροεντολές Basic του LibreOffice είναι εφικτή και σημαντικά χαρακτηριστικά μπορούν να ληφθούν όπως:
Ταυτοποίηση ComputerName ή εντοπισμός OSName είναι εφικτά,
Η συνάρτηση Basic FileLen() και com.sun.star.ucb.SimpleFileAccess.getSize() Η συνάρτηση API εμφανίζει ανώτερο όριο μεγέθους αρχείου 2 Gigabytes που το Python βοηθά να ξεπεραστεί,
com.sun.star.util.PathSettings μπορεί να κανονικοποιηθεί,
και πολλά άλλα.
Συνιστάται λογική έκθεση στο LibreOffice Basic και σε γνωρίσματα της διασύνδεσης προγραμματισμού εφαρμογών (API) πριν την εκτέλεση διαγλωσσικών κλήσεων από Basic σε Python, σε JavaScript ή οποιονδήποτε άλλο μηχανισμό δεσμών ενεργειών.
Ανάκτηση δεσμών ενεργειών Python
Οι δέσμες ενεργειών Python μπορεί να είναι προσωπικές, κοινόχρηστες, ή ενσωματωμένες σε έγγραφα. Για να τις εκτελέσετε, το LibreOffice Basic πρέπει να παρέχεται με θέσεις δεσμών ενεργειών Python. Ο εντοπισμός com.sun.star.script.provider.XScript σύνδεσης συμβατής με αντικείμενα UNO επιτρέπει την εκτέλεση δεσμών ενεργειών Python:
Option Explicit
Public Function GetPythonScript(macro As String, _
Optional location As String) As com.sun.star.script.provider.Xscript
'''Αιχμαλωσία αντικειμένου δέσμης ενεργειών Python πριν την εκτέλεση
'Ορίσματα:
' macro : ως "library/module.py$macro" ή "module.py$macro"
' θέση: ως "document", "share", "user" ή ENUM(eration)
'Αποτέλεσμα:
' εντοπίστηκε com.sun.star.script.provider.XScript UNO υπηρεσία'''
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 συμβατό
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
Εκτέλεση δεσμών ενεργειών Python
Σύνταξη
workstation_name = script.invoke(Array(), Array(), Array())
opSysName = script.invoke(Array(), in_outs, Array()) ' in_out είναι ένας πίνακας
file_len = script.invoke(Array(systemFilePath), Array(), Array())
normalizedPath = script.invoke(Array(systemFilePath), Array(), Array())
Ενσωματωμένα παραδείγματα δεσμών ενεργειών
Κάτω από τις ρουτίνες ComputerName και GetFilelen καλούνται οι αντίστοιχές τους Python, χρησιμοποιώντας την προμνημονευθείσα συνάρτηση GetPythonScript. Η διαχείριση της εξαίρεσης δεν είναι λεπτομερής.
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)
Personal or Shared Scripts Examples
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.
Option Explicit
Option Compatible ' Properties are supported
Private scr As Object ' com.sun.star.script.provider.XScript
Private Property Get OSName As String
'''Platform name as "Linux", "Darwin" or "Windows"'''
scr = GetPythonScript("Platform.py$OSname", Script.ISPERSONAL)
OSName = scr.invoke(Array(), Array(), Array())
End Property ' OSName
Private Sub HelloWorld()
'''LibreOffice Python shared sample'''
scr = GetPythonScript("HelloWorld.py$HelloWorldPython", Script.ISSHARED)
scr.invoke(Array(), Array(), Array(),)
End Sub ' HelloWorld
Public Function NormalizePath(systemFilePath As String) As String
'''Strip superfluous '\..' in path'''
scr = GetPythonScript("Os/Path.py$normalyze", "user")
NormalizePath = scr.invoke(Array(systemFilePath), Array(), Array())
End Function ' NormalizePath
Python standard modules
LibreOffice embedded Python contains many standard libraries to benefit from. They bear a rich feature set, such as but not limited to:
argparse Parser for command-line options, arguments and sub-commands
cmath Mathematical functions for complex numbers
csv CSV files reading and writing
datetime Genuine date and time types
json JSON encoder and decoder
math Mathematical functions
re Regular expression operations
socket Low-level networking interface
sys System-specific parameters and functions
unittest and trace Unit testing framework and Track Python execution
xml.etree.ElementTree ElementTree XML API