为什么 docx2pdf 模块在我的 Python 脚本 Mac OS 中没有将 docx 转换为 pdf?



我正在使用Python 3.8和docx2pdf 0.1.7。多年来,我一直在尝试在我的脚本中获取一些东西,将 docx 转换为 pdf。我已经尝试了各种各样的东西,但到目前为止没有任何效果。

有一个名为docx2pdf的模块应该转换我刚刚创建的文件,但它似乎不起作用,我无法弄清楚为什么会这样。我尝试在我的脚本中运行它,但我也尝试将其作为子进程运行,但都没有奏效。该模块的文档在这里。

我认为这是一个非常未知的模块,因为我在互联网上找不到任何答案,所以我希望有人知道如何解决这个问题。

这是我正在使用的代码:

from docx import Document
from docx.shared import Pt
from tkinter import *
from docx2pdf import convert
root = Tk()
# Then some irrelevant code for this question
def updater()
doc = Document('./Contract.docx')
# Then some code which updates the doc according to the tkinter Entry input
# Save it according to some of the input from the GUI
doc.save('/Users/Jem/Documents/Huurovereenkomsten/Specifiek/{}/contract{}.docx'.format(nospaceadres,
                                 naamhuurder.get()))
# It all works fine until here
convert('/Users/Jem/Documents/Huurovereenkomsten/Specifiek/{}/contract{}.docx'.format(nospaceadres,
                                 naamhuurder.get())) # This should convert it to a pdf with the same name in the same folder
# Some Tkinter GUI code which is also irrelevant for this question
root.mainloop()

但首先,它给了我这个:

0%|          | 0/1 [00:02<?, ?it/s]

然后它在我的Macbook上打开MS Word,并告诉我它需要许可证/权限才能打开docx。然后,我必须选择文件,该文件允许它打开它。之后,它会打开 docx,但没有任何反应。

之后,它给了我这个:

{'input': '/Users/Jem/Documents/Huurovereenkomsten/Specifiek/slotlaan73/contractabc.docx', 'output': '/Users/Jem/Documents/Huurovereenkomsten/Specifiek/slotlaan73/contractabc.pdf', 'result': 'error', 'error': 'Error: Er heeft zich een fout voorgedaan.'}

'Er heeft zich een fout voorgedaan." 是荷兰语的意思:发生了错误。

有谁知道为什么会发生这种情况,或者我可以做些什么来让它工作以便它将 docx 转换为 pdf?

尝试此代码,如果发生任何错误,请发布错误的屏幕截图......我们将努力解决

import tkinter as to 
import tkinter.ttk as ttk
from  tkinter.filedialog import askopenfile
from tkinter.messagebox  import showinfo
from docx2pdf import convert
win = tk.Tk()
win.title("Word To PDF Converter")
def openfile():
file = askopenfile(filetypes = [('Word Files','*.docx')])
print(file)
convert(file.name)
showinfo("Done","File Successfully Converted")
label = tk.Label(win,text='Choose File: ')
label.grid(row=0,column=0,padx=5,pady=5)
button = ttk.Button(win,text='Select',width=30,command=openfile)
button.grid(row=0,column=1,padx=5,pady=5)
win.mainloop()

最新更新