如何使用 python 将 Word 文档转换为 pdf 并保留相同的模板

问题描述 投票:0回答:1

我需要将 Word 文档转换为 pdf,并保留用于使用 python 脚本生成 Word 文档的相同模板 注意:我的机器上没有安装Word应用程序,并且我没有安装它的权限 有没有使用python库的解决方案,应该保留模板

python-3.x pdf ms-word
1个回答
0
投票

您可以使用 python-docx 和 pdf2docx 库将 Word 文档转换为 PDF,同时保留模板。请记住,该库可能需要在您的计算机上安装单词。下一行是示例:

from docx2pdf import convert
# Convert a single file - here maybe need to set file path
convert("input.docx", "output.pdf")
# Convert all files in a directory
convert("my_docx_folder/")

或者,您可以使用 spire.doc 库,它不需要安装 Word。例如,您可以使用这个:

from spire.doc import Document, FileFormat
# Create a Document object
document = Document()
# Load the .docx file
document.LoadFromFile("sample.docx")
# Save the document as a PDF
document.SaveToFile("output.pdf", FileFormat.PDF)

https://dev.to/codingco/how-to-convert-word-doc-docx-to-pdf-in-python-308h 这也可以帮助你。

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.