且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将 Word 文档转换为 PDF - Python

更新时间:2023-02-12 21:54:53

如果 OpenOffice 在系统上可用,您可以使用它.

You can use OpenOffice if it is available on the system.

import subprocess
import shutil

input_filename = 'input.doc'
output_filename = 'output.pdf'

p = subprocess.Popen(['unoconv', '--stdout', input_filename], stdout=subprocess.PIPE)
with open(output_filename, 'w') as output:
   shutil.copyfileobj(p.stdout, output)

如果您想直接使用 UNO/OpenOffice COM 的 Python 绑定执行此操作,也可以查看 unoconv 的源代码.

You can also look at unoconv's source code if you want to do it directly with the Python bindings for UNO/OpenOffice COM.