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:
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!!