Bottom Page

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!!
Larz60+ wrote Oct-02-2019, 01:47 AM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#2
try adding immediately after line 25
doc = None
if any of the assigning code fails to execute, doc will not be defined.
None may cause an error if this happens, but at least you will know why.
Quote
#3
Thanks, @Larz60+
I am new here.

I have already tried this but it's saying in error that AttributeError: 'NoneType' object has no attribute 'render'
Quote
#4
As I stated:
Quote:None may cause an error if this happens, but at least you will know why.
And that's exactly what is happening here.
The new error is telling you that doc has never been assigned a value, so none of the conditionals are being called:
In other words, certificate is not equal to 'Audit_ESS' or 'IFRS_ESS'
add this statement after line 28:
print(f"certificate: {certificate}")
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)