zunächst:
@mikele: Vielen Dank für Deinen Denkanstoss im Thread viewtopic.php?f=12&t=41974#p115800
Um missverständnisse zu vermeiden, habe ich mal meinen kompletten Test Code hier eingebracht. Das Script wird in einem Terminal gestartet. Es soll ein definiertes Calc Workbook öffnen und nach 5 sekunden wieder schließen. Speichern, bearbeiten und was sonst noch ist im Augenblick nicht relevant.
Der Code macht was er soll, nur die Zeile
Code: Alles auswählen
desktop = XSCRIPTCONTEXT.getDesktop()Code: Alles auswählen
import sys
import subprocess
import uno
import time
from com.sun.star.beans import PropertyValue
DEBUG=True # False if no debugging needed
# Start LibreOffice in headless mode
def start_libreoffice():
try:
process = subprocess.Popen([
r"soffice",
#"--headless",
"--accept=socket,host=localhost,port=2002;urp;StarOffice.Servicemanager",
"--minimized"
])
# An dieser Stelle wird der LO Dialog "Dokumentwiederherstellung" angezeigt.
if DEBUG:
print("LibreOffice process started")
return process
except Exception as e:
print(f"Failed to start LibreOffice: {e}")
return None
# Connect to LibreOffice
def connect_to_libreoffice():
local_context = uno.getComponentContext()
resolver = local_context.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", local_context)
# context in other codes = ctx
context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
return context
# Load python_test.ods document
def getWorkbook(context):
try:
args = (PropertyValue(Name='Hidden', Value=False),PropertyValue(Name="MacroExecutionMode",Value=1),)
if DEBUG:
print ("props sind eingerichtet")
path="/home/gerd/Dokumente/python_test.ods"
docurl = uno.systemPathToFileUrl(path)
if DEBUG:
print ("path ist gesetzt ..")
#desktop = XSCRIPTCONTEXT.getDesktop()
# funktioniert nicht. Fehler ist:
# name 'XSCRIPTCONTEXT' is not defined
desktop = context.ServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", context)
if DEBUG:
print ("desktop definiert .... ")
document = desktop.loadComponentFromURL(docurl,"_default",0,args)
if DEBUG:
print ("calc dokument ist geladen!")
return document
except Exception as e:
print(f"Calc File nicht gefunden: {e}")
return None
def mri(target):
ctx = uno.getComponentContext()
mri = ctx.ServiceManager.createInstanceWithContext("mytools.Mri",ctx)
#mri = create_instance("mytools.Mri", True)
mri.inspect(target)
# Main execution
if __name__ == "__main__":
try:
# Start LibreOffice
libreoffice_process = start_libreoffice()
#API server take time to fire up
# therefor: start 10 seconds
time.sleep(10)
# Connect to LibreOffice
context = connect_to_libreoffice()
# load an exiting spreadsheet document
doc = getWorkbook(context)
### ----- extension not loadable -----
# if DEBUG:
# print("mri wird aufgerufen")
# mri(doc)
# if DEBUG:
# print("mri wurde aufgerufen")
###
if doc == None : # Es wurde kein CALC File geladen
sys.exit()
else: # CALC File wurde geladen, könnte jetzt bearbeitet werden
time.sleep(5)
# 5 sekunden bis zum Schließen
# hier kommt der Code zum Bearbeiten des workbook hin
# Close the document
doc.close(True)
except Exception as e:
print(f"An error occurred: {e}")
finally:
# Close document and
# Stop LibreOffice
if libreoffice_process:
doc.close(True)
libreoffice_process.terminate()
print("LibreOffice process terminated.")
Gruß
Gerd Erich

