从lin命令将DOCX文件转换为PDF

2024-06-16 10:47:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写一个python脚本,想把DOCX转换成PDF格式。有什么办法吗?以下是我当前的代码:

printer_path = 'C:\\Program Files\\Nitro\\Pro\\12\\NitroPDF.exe'
doc_path = 'Test.docx'

subprocess.call([printer_path,  doc_source_path])

Nitro PDF将打开并开始转换文件,但不会完成。感谢您的任何意见。在

编辑1:对于subprocess.call为了工作,我必须使两个输入都是绝对路径,例如doc_path='C:\Documents\测试.docx'


Tags: path代码脚本docpdf格式filescall
2条回答

在Windows或macOS上,您可以使用docx2pdfpython包来转换docx而不存在任何格式问题。它需要安装microsoftword,在Windows上使用COM API,在macOS上使用AppleScript(JXA)。在

from docx2pdf import convert

convert("input.docx")
convert("input.docx", "output.pdf")
convert("my_docx_folder/")

免责声明:我写了docx2pdf。https://github.com/AlJohri/docx2pdf

如果安装了Microsoft Word,则以下操作应该有效:

subprocess.call('docto -f "C:\Dir with Spaces\FilesToConvert\" -O "C:\DirToOutput" -T wdFormatPDF  -OX .pdf', shell=True)

相关问题 更多 >