[Tkinter] local variable 'doc' referenced before assignment - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] local variable 'doc' referenced before assignment (/thread-21489.html) |
local variable 'doc' referenced before assignment - Kriti - Oct-01-2019 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:
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!! RE: local variable 'doc' referenced before assignment - Larz60+ - Oct-02-2019 try adding immediately after line 25 doc = Noneif 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. RE: local variable 'doc' referenced before assignment - Kriti - Oct-02-2019 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' RE: local variable 'doc' referenced before assignment - Larz60+ - Oct-02-2019 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}") |