我经常需要使用 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/