简体   繁体   English

LibreOffice convert.docx to.pdf 并行运行不正常

[英]LibreOffice convert .docx to .pdf in parallel not working well

I have a lot of docx files to be converted to pdf.我有很多 docx 文件要转换为 pdf。 Converting them one by one takes long time.将它们一一转换需要很长时间。 So I write a python scripts to convert them in parallel:所以我写了一个 python 脚本来并行转换它们:

from subprocess import Popen
import time
import os

os.chdir(os.path.dirname(__file__))

output_dir = './outputs'
source_file_format = './docs/example_{}.docx'

po_list = [Popen(
    f"/Applications/LibreOffice.app/Contents/MacOS/soffice --invisible --convert-to pdf --outdir {output_dir} {source_file_format.format(i)}",
    shell=True)
    for i in range(0, 7, 1)]

while po_list:
    time.sleep(0.01)
    for i, p in enumerate(po_list):
        status = p.poll()
        if status is None:
            continue
        elif status == 0:
            print('Succeed: [{}] {} -> {}'.format(p.returncode, p.stderr, p.args))
            po_list.remove(p)
        else:
            print('Failed: {} : {}'.format(p.args, p.poll()))
            po_list.remove(p)

But each time I run this script, only a part of docx files are converted successfully.但是每次运行这个脚本,只有一部分docx文件转换成功。 The rest conversion processes even not throw any error info. rest 转换过程甚至不抛出任何错误信息。

We were also stuck on the same issue for some time.我们也被困在同一个问题上一段时间了。

Multiple Instances of LibreOffice shares the same space using a UserInstallation directory and thus parallel conversion was creating a problem here (The intermittent processes seem to get mixed up). LibreOffice 的多个实例使用 UserInstallation 目录共享相同的空间,因此并行转换在这里产生了问题(间歇性进程似乎混淆了)。

Using a different directory for each instance of libre helped to solve this issue.为每个 libre 实例使用不同的目录有助于解决这个问题。 You may achieve this via UserInstallation env variable which can be passed as: "-env:UserInstallation=file:///d:/tmp/p0/"您可以通过 UserInstallation 环境变量来实现这一点,该变量可以传递为:“-env:UserInstallation=file:///d:/tmp/p0/”

You may automate this by appending your loop variable or any unique identifier in the directory.您可以通过在目录中附加循环变量或任何唯一标识符来自动执行此操作。

Reference: https://ask.libreoffice.org/en/question/42975/how-can-i-run-multiple-instances-of-sofficebin-at-a-time/参考: https://ask.libreoffice.org/en/question/42975/how-can-i-run-multiple-instances-of-sofficebin-at-a-time/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM