Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] local variable 'doc' referenced before assignment
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:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\kt.TASK\AppData\Local\Continuum\anaconda3\lib\tkinter\", 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")


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)

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

    for i in range(1,Certi_no):
        context = { 'desired_date' : certi_date,'user' :user }

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

# 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 
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.
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.
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'
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}")

Top Page

Forum Jump:

Users browsing this thread: 1 Guest(s)