我需要将 Word 文档转换为 pdf,并保留用于使用 python 脚本生成 Word 文档的相同模板 注意:我的机器上没有安装Word应用程序,并且我没有安装它的权限 有没有使用python库的解决方案,应该保留模板
您可以使用 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 这也可以帮助你。
希望这有帮助。