问题
I'm using Python 3.8 and docx2pdf 0.1.7. I've been trying for ages to get something something in my script which will convert a docx to a pdf. I've tried all sorts of stuff but nothing has worked for me thus far.
There's a module called docx2pdf which should convert the file I just created but it doesn't seem to work and I can't figure out why that's the case. I tried running it in my script but I also tried running it as a subprocess but neither worked. Documentation of the module is here.
I think this is a pretty unknown module as I couldn't find any answers on the internet so I'm hoping there is someone who knows how to tackle this problem.
This is the code I'm working with:
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()
But first, it gives me this:
0%| | 0/1 [00:02<?, ?it/s]
Then it opens MS Word on my macbook and tells me it needs a permit / rights to open the docx. I then have to select the document, which gives it the permit to open it. After that, it opens the docx but nothing happens.
After that, it gives me this:
{'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.' is Dutch for: an error has occurred.
Does anyone know why this is happening or what I could do to make it work so that it converts the docx to a pdf?
回答1:
Try this code and if any error occurred then post a screenshot of the error ...... we will try to solve
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()
来源:https://stackoverflow.com/questions/62636726/why-is-the-docx2pdf-module-not-converting-docx-to-pdf-in-my-python-script-mac-os