soffice --accept="socket,host=localhost,port=2002;urp;"
# -*- coding: utf-8 -*- import os import subprocess import sys import time import uno NoConnectException = uno.getClass( "com.sun.star.connection.NoConnectException") ############################################################################### def init_office(): """ Test Libre/Open Office to be launched in the listening mode """ connection_string = "uno:socket,host=localhost,port=2002;urp;\ StarOffice.ComponentContext" oLocal = uno.getComponentContext() oResolver = \ oLocal.ServiceManager.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", oLocal) if oResolver: oContext = oResolver.resolve(connection_string) oDesktop = oContext.ServiceManager.\ createInstanceWithContext("com.sun.star.frame.Desktop", oContext) ############################################################################### def start_office(timeout=30, attempt_period=0.1, office='soffice --accept="socket,host=localhost,port=2002;urp;"'): """ Starts Libre/Open Office with a listening socket. @type timeout: int @param timeout: Timeout for starting Libre/Open Office in seconds @type attempt_period: int @param attempt_period: Timeout between attempts in seconds @type office: string @param office: Libre/Open Office startup string """ ########################################################################### def start_office_instance(office): """ Starts Libre/Open Office with a listening socket. @type office: string @param office: Libre/Open Office startup string """ # Fork to execute Office if os.fork(): return # Start OpenOffice.org and report any errors that occur. try: retcode = subprocess.call(office, shell=True) if retcode < 0: print (sys.stderr, "Office was terminated by signal", -retcode) elif retcode > 0: print (sys.stderr, "Office returned", retcode) except OSError as e: print (sys.stderr, "Execution failed:", e) # Terminate this process when Office has closed. raise SystemExit() ########################################################################### waiting = False try: init_office() except NoConnectException as e: waiting = True start_office_instance(office) if waiting: steps = int(timeout/attempt_period) exception = None for i in range(steps + 1): try: init_office() break except NoConnectException as e: exception = e time.sleep(attempt_period) else: if exception: raise NoConnectException(exception) else: raise NoConnectException() ############################################################################### start_office()
import pyoocalc pyoocalc.Document()
import pyoocalc doc = pyoocalc.Document(autostart=True) doc.new_document()
Source: https://habr.com/ru/post/274775/
All Articles