-1

I am trying to make a little converter from pdf to word in python, so I have two functions one for converting the file and one to save the file. I wonder how to define a variable in both functions: here what I wrote, docx_file is giving me error not defined in the convert function.

def convert():
    try:
        pdf = fileEntry.get()
        cv = Converter(pdf)
        cv.convert(docx_file, start=0, end=None)
        cv.close()
        if docx_file is None:
            return
           
    except FileNotFoundError:
        fileEntry.delete(0,END)
        fileEntry.config(fg="red")
        fileEntry.insert(0,"Please select a pdf file first")
    except:
        pass
    
def save2word():

    docx_file = asksaveasfile(mode='w',defaultextension=".docx", filetypes=[("word file","*.docx"), ("text file","*.txt")])
    docx_file.write()
    docx_file.close()
    
    if docx_file is None:
        return
    
    print("saved")
    fileEntry.delete(0,END)
    fileEntry.insert(0,"pdf Extracted and Saved...")
quamrana
  • 37,849
  • 12
  • 53
  • 71
MaxCode
  • 1
  • 1

1 Answers1

2

You could try declaring a blank variable

docx_file = ""    
def convert():
    try:
        pdf = fileEntry.get()
        cv = Converter(pdf)
        cv.convert(docx_file, start=0, end=None)
        cv.close()
        if docx_file is None:
            return

or set the variable to global

def convert():
    try:
        pdf = fileEntry.get()
        cv = Converter(pdf)
        cv.convert(docx_file, start=0, end=None)
        cv.close()
        global docx_file
        if docx_file is None:
            return
Ae3erdion
  • 51
  • 7