【问题标题】:Converting DOCX file to PDF from command line从命令行将 DOCX 文件转换为 PDF
【发布时间】:2019-07-14 16:25:34
【问题描述】:

我正在编写一个 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\Test.docx'

【问题讨论】:

  • 如果您手动而不是编程,这是否可以正常工作?
  • 你看到this question了吗?
  • @jwalton3141 这个答案正是我想要的。谢谢!

标签: python pdf pdf-generation docx


【解决方案1】:

如果您安装了 Microsoft Word,以下应该可以工作:

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

【讨论】:

【解决方案2】:

您可以使用docx2pdf python 包在 Windows 或 macOS 上以零格式问题转换 docx。它需要安装 Microsoft Word,并在 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

【讨论】:

  • docx2pdf 未针对 linux 实现,因为它需要安装 Microsoft Word
  • @PHZ.fi-Pharazon 没错!虽然我听到一些报道说它似乎与“WPS Office”(github.com/AlJohri/docx2pdf/pull/10)兼容但我自己没有尝试过。
猜你喜欢
  • 2021-04-23
  • 1970-01-01
  • 2010-12-02
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 2020-08-14
  • 2021-10-03
  • 1970-01-01
相关资源
最近更新 更多