python - Headless LibreOffice 在 Windows 上导出为 PDF 非常慢(比 Linux 慢 6 倍)

标签 python pdf docx libreoffice

我经常需要使用 LibreOffice 将许多(> 1000).docx 文档导出为 PDF。这是一个示例文档:test.docx 。以下代码可以工作,但在 Windows 上速度相当慢(每个 PDF 文档平均 3.3 秒):

import subprocess, docx, time   # first do: pip install python-docx 
for i in range(10):
    doc = docx.Document('test.docx')
    for paragraph in doc.paragraphs:
        paragraph.text = paragraph.text.replace('{{num}}', str(i))
    doc.save('test%i.docx' % i)   # these 4 previous lines are super fast - a few ms
    t0 = time.time()
    subprocess.call(r'C:\Program Files\LibreOffice\program\soffice.exe --headless --convert-to pdf test%i.docx --outdir . --nocrashreport --nodefault --nofirststartwizard --nolockcheck --nologo --norestore"' % i)
    print('PDF generated in %.1f sec' % (time.time()-t0))

    # for linux:
    # (0.54 seconds on average, so it's 6 times better than on Windows!)
    # subprocess.call(['/usr/bin/soffice', '--headless', '--convert-to', 'pdf', '--outdir', '/home/user', 'test%i.docx' % i])  

如何在 Windows 上加快 PDF 导出速度?

我怀疑在“启动 LibreOffice/Writer,(执行工作),关闭 LibreOffice”上浪费了很多时间 “启动 LibreOffice/Writer,(执行工作),关闭 LibreOffice” “ “启动 LibreOffice/Writer,(完成工作),关闭 LibreOffice”

注释:

  • 作为比较:此处:https://bugs.documentfoundation.org/show_bug.cgi?id=92274据说导出时间为 90ms 或 810ms。

  • soffice.exe 替换为 swriter.exe:同样的问题:平均 3.3 秒

    subprocess.call(r'C:\Program Files\LibreOffice\program\swriter.exe --headless --convert-to pdf test%i.docx --outdir ."' % i)
    

最佳答案

事实上,所有时间都浪费在启动/退出 LibreOffice 上。我们可以在一次调用 soffice.exe 中传递许多 docx 文档:

import subprocess, docx
for i in range(1000):
    doc = docx.Document('test.docx')
    for paragraph in doc.paragraphs:
        paragraph.text = paragraph.text.replace('{{num}}', str(i))
    doc.save('test%i.docx' % i)

# all PDFs in one pass:
subprocess.call(['C:\Program Files\LibreOffice\program\swriter.exe', 
    '--headless', '--convert-to', 'pdf', '--outdir', '.'] + ['test%i.docx' % i for i in range(1000)])

总共 107 秒,因此每个 PDF 平均需要约 107 毫秒,要好得多!

注释:

  • 它不适用于 10,000 个文档,因为命令行参数的长度将超过 32k 个字符,如 explained here

  • 我想知道是否可以通过更具交互性的方式来使用 LibreOffice headless:

    • 启动 Writer headless ,保持启动
    • 向此进程发送诸如open test1.docx之类的操作
    • 发送操作导出为 pdf,并关闭 docx
    • 发送打开test2.docx,然后导出等
    • ...
    • 退出 Writer headless

       

    这适用于 MS Office 的 COM(组件对象模型):.doc to pdf using python但我想知道 LibreOffice 是否存在类似的东西。答案似乎是否定的:Does LibreOffice/OpenOffice Support the COM Model

关于python - Headless LibreOffice 在 Windows 上导出为 PDF 非常慢(比 Linux 慢 6 倍),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61447666/

相关文章:

python - networkx 中的定向加权平衡树导入和最短路径

Python:从 .msg 文件保存附件

c# - 使用 OpenXML 和 RTF 将瑞典语和中文符号发送到 Docx

php - 将多个 csv 文件组合在一起,并在连接期间添加一列

python - pandas 自动从具有列名称的系列列表创建数据框

python - 通过Applescript在Python中打开pwdir文件夹中的文件

php - 获取 PDF 文档的页数

python - 结构错误 : unpack requires a string argument of length 16

python-3.x - Python-docx add_picture size 不会打印如屏幕上所示

python - Docx 缺少属性