如何在Mac OS上使用Python将docx转换为pdf?

2024-10-01 15:46:12 发布

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

我查了好几个SO和其他网页,但没有找到任何有效的

我编写的脚本打开一个docx,更改一些单词,然后将其作为docx保存在某个文件夹中。然而,我希望它保存为pdf格式,但我不知道如何保存

这是我正在使用的代码的一个示例:

# Opening the original document
doc = Document('./myDocument.docx')

# Some code which changes the doc

# Saving the changed doc as a docx
doc.save('/my/folder/myChangedDocument.docx')

为了将其保存为pdf,我尝试了以下操作:

from docx2pdf import convert

# This after it was saved as a docx
    convert('/my/folder/myChangedDocument.docx', '/my/folder/myChangedDocument.pdf')

但是它说Word需要权限才能打开保存的文件,我必须选择该文件才能授予它权限。在那之后,它只是说:

0%|          | 0/1 [00:03<?, ?it/s]
{'input': '/my/folder/contractsomeVariable.docx', 'output': '/my/folder/contractsomeVariable.pdf', 'result': 'error', 'error': 'Error: An error has occurred.'}

当我保存文档名时,我试图简单地将.pdf而不是.docx放在文档名后面,但这也不起作用,因为模块docx无法做到这一点

那么,有人知道如何使用Python将docx保存为pdf吗


Tags: 文件the权限convertdocsopdfmy

热门问题