python3 unotools连接错误无法连接

时间:2016-07-25 22:37:47

标签: python python-3.x connection libreoffice-writer

我已经找到了答案,但到目前为止没有任何帮助。我有一个方法,我想用它来创建一个odt文件并用文本填充它。我还希望用户在创建文件时查看该文件。我使用的是python 3.4.3 unotools 0.3.3 LinuxMint 17.1 LibreOffice 4.2.8.2

问题:

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应用程序打开并保持打开状态。但是,连接似乎丢失了。
我希望有人能给我帮助,谢谢。

2 个答案:

答案 0 :(得分:0)

我不推荐这样的代码:

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

最好使用shell脚本启动soffice,然后调用python脚本。

但是,如果您决定在子进程中运行soffice,那么我建议将睡眠时间增加到至少15秒。

请参阅https://forum.openoffice.org/en/forum/viewtopic.php?t=1014

答案 1 :(得分:0)

感谢您的建议。我确实希望这个运行一个子进程。我试着延长时间,但仍然没有喜悦。
我现在正在使用Python odfpy 1.3.3软件包,在开始使用一两天后,我已经取得了更大的成功。