PythonからLibre / OpenOfficeをリッスンモードで自動的に起動する

Libre / Open Officeは、UNO APIを介してオフィスと連携する機能を提供します。 オフィスに連絡できるようにするには、リスニングモードで実行する必要があります。



例:

soffice --accept="socket,host=localhost,port=2002;urp;"
      
      





このアプローチは非常に論理的であり、オフィス開発者の観点から理解できますが、多くの不便をもたらします。 特に、リスニングモードでLibre / Open Officeを自分で実行する必要があります。 個人的には、開発者が怠け者であり、オフィスの立ち上げ機能を提供しなかった理由がわかりません。 まあ、大丈夫、すべてが行われていれば、プログラマは必要ありませんでした。 したがって、私たちは自分で問題を解決します。



この問題を解決する最も簡単な方法は、スクリプトファイルにオフィスの起動ラインを配置することです。 その中で、最初にオフィスを実行し、次にアプリケーションを実行します。 しかし、たとえばライブラリであり、スクリプトファイルからアクセスする方法がない場合はどうでしょう。 さらに、オフィスの開始を待つだけでなく、リスニングモードになるまで待つ必要があります。 一般に、このアプローチはテストの問題にのみ適しています。

私は次の実装に決めました。

1.オフィスをリスニングモードで起動するプロセスを分岐します。

2.一定の頻度で、試行が成功するまでオフィスへの連絡を試みます。

3.一定時間後にオフィスへの接続が成功しない場合、 com.sun.star.connection.NoConnectException例外をスローします



 # -*- 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()
      
      







この例では、0.1秒の頻度でLibre / Open Officeを30秒間起動しようとします。

関数init_office()は 、リスニングモードで実行されているオフィスの存在のテストとして使用されます。 代わりに、他のライブラリを使用できます。



以前の記事「 PyOOCalc-レポート生成ライブラリ、PythonのLibre / Open Office Calcアカウント 」で、特定のカテゴリのタスクでLibre / OpenOfficeを使用する方が簡単である方法について説明しました。 しかし、 PyOOCalcライブラリにリスニングモードでオフィスを自動的に開始する機能がなく 、上記のコードは次のように書き換えることができます。

init_office()関数の代わりに、 次のように記述できます。

 import pyoocalc pyoocalc.Document()
      
      





他のライブラリでもかまいません。 Libre / Open Officeへの接続を試みるメソッドを呼び出す必要があります。



また、 PyOOCalcライブラリにオフィスを自動的に開始する機能を追加しました。

使用例:

 import pyoocalc doc = pyoocalc.Document(autostart=True) doc.new_document()
      
      






All Articles