Python Forum
[Tkinter] local variable 'doc' referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] local variable 'doc' referenced before assignment
#1
Hi All,
I am trying to write the python GUI Code which creates the PDF based on user input and the excel sheet.It takes the user input create the doc template to convert the docx file to pdf. I am trying to create GUI for this using Tkinter. Before I added the GUI code Everything working as desired(Was creating PDF based on DOC Template) after I have added the GUI code to this, It's giving me the following error:

Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\kt.TASK\AppData\Local\Continuum\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "<ipython-input-1-39a50c29bfea>", line 38, in do_it doc.render(context) UnboundLocalError: local variable 'doc' referenced before assignment

Here is my full code:

from Tkinter import *
from docxtpl import DocxTemplate
import os
import comtypes.client
import time
import sys
import openpyxl

# when ever you see this think as balnk window. This is type of constructor
root = Tk()
root.title("New App")
root.geometry("640x640+0+0")

heading=Label(root,text="Welcome",font=("arial",40,"bold"),fg="steelblue").pack()

certificate=Label(root,text="Which Certificate you want to create?" ,font=("arial",10,"bold"),fg="Black" ).place(x=10, y=200)
name= StringVar()
entry_box=Entry(root, textvariable=name , width=25, bg="lightgreen").place(x=280,y=200)

certi_date=Label(root,text= 'Enter the certificate date?',font=("arial", 10,"bold"), fg="Black").place(x=10,y=300)
certi_date1=StringVar()
certi_date_box=Entry(root,textvariable=certi_date1,width=25,bg="lightgreen").place(x=280,y=300)

   
def do_it():
    wb =openpyxl.load_workbook('Test.xlsx')
    sheet = wb['Sheet1']
    Certi_no=sheet.max_row+1
    if certificate=='Audit_ESS':
        doc = DocxTemplate("Audit_ESS.docx")
    
    elif certificate=='IFRS_ESS':
        doc=DocxTemplate("IFRS_ESS.docx")

    for i in range(1,Certi_no):
        user=sheet.cell(row=i,column=1).value
        context = { 'desired_date' : certi_date,'user' :user }
        doc.render(context)
        doc.save("generated_doc.docx")

        docxfilepath = os.path.join('C:\\Users\\kt.TASK',"generated_doc.docx")
        PDFfilepath=os.path.join('C:\\Users\\kt.TASK',user)
        wdFormatPDF = 17
        word = comtypes.client.CreateObject('Word.Application')
    #word.Visible = True
        time.sleep(3)
        doc = word.Documents.Open(docxfilepath)
        doc.SaveAs(PDFfilepath, FileFormat=wdFormatPDF)
        doc.Close()
        word.Quit()
    

# button on window 1
work=Button(root,text="Create", width=30, height=5, bg="lightblue",command=do_it).place(x=250,y=500)


# make sure windows constantly display 
root.mainloop()
Not sure what to do. Please help!!
Reply


Messages In This Thread
local variable 'doc' referenced before assignment - by Kriti - Oct-01-2019, 10:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,429 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  [PyQt] Create exe file including referenced image (*.png) files mart79 0 1,630 Jul-21-2020, 09:49 AM
Last Post: mart79

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020