2016-07-25 162 views
0

我已經搜索了答案,但目前爲止沒有任何幫助。我有一個方法可以用來創建一個odt文件並用文本填充它。我還希望用戶在創建文件時查看該文件。我使用Python 3.4.3 unotools 0.3.3 LinuxMint 17.1 LibreOffice的4.2.8.2python3 unotools連接錯誤無法連接

問題:

unotools.errors.ConnectionError: failed to connect: ('socket,host=localhost,port=8100', {}) 

的unotools樣品從工作細端 - 創建並保存沒有錯誤一sample.odt。我的代碼草案:

def writer_report(self): 
    subprocess.Popen(["soffice", "--accept='socket,host=localhost,port=8100;urp;StarOffice.Service'"]) 
    time.sleep(5) # using this to give time for LibreOffice to open - temporary 
    context = connect(Socket('localhost', '8100')) 
    writer = Writer(context) 
    writer.set_string_to_end('world\n') 
    writer.set_string_to_start('hello\n') 
    writer.store_to_url('output.odt','FilterName','writer8') 
    writer.close(True) 

的LibreOffice的應用程序打開並保持打開狀態。但是,連接似乎丟失了。
我希望有人能給我幫助,謝謝。

回答

0

我不建議這樣的代碼:

subprocess.Popen(...) 
time.sleep(...) 

這是更好地使用shell腳本啓動soffice,然後調用Python腳本。

但是,如果您決定在子流程中運行soffice,那麼我建議將睡眠時間增加至少15秒。

請參閱https://forum.openoffice.org/en/forum/viewtopic.php?t=1014

+0

感謝您的建議。我試圖延長時間,但仍然沒有快樂。 –

0

感謝您的建議。我確實希望這個運行一個子進程。我試圖延長時間,但仍然沒有快樂。
我現在正在使用Python odfpy 1.3.3包,在開始使用一兩天後,我已經獲得了更多的成功。