0

我一直在寻找答案,但到目前为止没有任何帮助。我有一个方法,我想用它来创建一个 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 应用程序打开并保持打开状态。但是,连接似乎丢失了。
我希望有人可以给我帮助,谢谢。

4

2 回答 2

0

我不推荐这样的代码:

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

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

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

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

于 2016-07-26T16:51:44.160 回答
0

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

于 2016-07-27T22:32:35.213 回答