2

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?

Jem
  • 557
  • 9
  • 28
  • Sounds like it could be a permissions issue. Try changing the permissions of the file before the call to `convert`? i.e. by using `os.chmod` from the stdlibrary."https://stackoverflow.com/questions/16249440/changing-file-permission-in-python" I woud set to giving all permissions `os.chmod(path, 0777)` just to check whether it is a permissions issue or not. – Calimocho Jun 29 '20 at 11:21
  • 2
    Thanks for your comment. I did what you suggested. It didn't ask for the permission anymore but the other 2 things (the 0% thing and the error) remained the same. Do you perhaps know anything else which could make it work? – Jem Jun 29 '20 at 15:39
  • 1
    Well, if we've ruled out a permissions error, it is either that there is something wrong with the file, or with the convert function. Are you able to verify whether the saved document is otherwise well-behaved? i.e. can you save it to a pdf manually using word? If you can then the problem is likely with the convert function. In which case I would suggest inspecting the source code/documentation of docx2pdf or raising an issue on their repo, there was a commit 13 days ago so it seems like it is actively maintained https://github.com/AlJohri/docx2pdf – Calimocho Jun 29 '20 at 15:45
  • 1
    I just tried converting the file manually and that worked. I guess I'll let them know by raising an issue on their repo then. Thanks for your recommendations! – Jem Jun 29 '20 at 15:48

1 Answers1

0

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()
  • I tried doing some things like this but I don't want to have to select the file manually every time again. I want it to automatically do it because the file is already specified without using the `askopenfile`. So I don't want to have the `file = askopenfile` in my script. – Jem Jul 04 '20 at 11:44