import tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import asksaveasfile
from reportlab.pdfgen import canvas
my_w = tk.Tk()
my_w.geometry("400x300") # Size of the window
my_w.title('www.plus2net.com')
my_font1=('times', 18, 'bold')
l1 = tk.Label(my_w,text='Save File',width=30,font=my_font1)
l1.grid(row=1,column=1)
b1 = tk.Button(my_w, text='Save',
width=20,command = lambda:save_file())
b1.grid(row=2,column=1)
def save_file():
file = filedialog.asksaveasfilename(
filetypes=[("pdf file", ".pdf")],
defaultextension=".pdf")
save_pdf(file) # create and save the pdf in given path
def save_pdf(my_path): # save the pdf file in given path
c = canvas.Canvas(my_path,bottomup=0)
c.drawString(200,200,"Hello World") # write text in page
c.showPage() # saves current page
c.save() # stores the file and close the canvas
my_w.destroy() #Close the window
my_w.mainloop() # Keep the window open
e1 = tk.Entry(my_w, width=3, bg='lightyellow')
This widget allows the user to input the student ID directly into the GUI. c.drawString(200, 200, "Hello World " + e1.get())
The entered student ID is retrieved using e1.get() and is combined with the text "Hello World". This text is then printed on the PDF at the specified coordinates. import tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import asksaveasfile
from reportlab.pdfgen import canvas
my_w = tk.Tk()
my_w.geometry("400x300") # Size of the window
my_w.title('www.plus2net.com')
my_font1=('times', 18, 'bold')
l1 = tk.Label(my_w,text='Save File',width=30,font=my_font1)
l1.grid(row=1,column=1,columnspan=2)
e1=tk.Entry(my_w,width=3,bg='lightyellow')
e1.grid(row=2,column=1)
b1 = tk.Button(my_w, text='Save',
width=20,command = lambda:save_file())
b1.grid(row=2,column=2)
def save_file():
file = filedialog.asksaveasfilename(
filetypes=[("pdf file", ".pdf")],
defaultextension=".pdf")
save_pdf(file)
def save_pdf(my_path):
c = canvas.Canvas(my_path,bottomup=0)
c.drawString(200,200,"Hello World " + e1.get()) # write text in page
c.showPage() # saves current page
c.save() # stores the file and close the canvas
my_w.destroy() #Close the window
my_w.mainloop() # Keep the window open
import tkinter as tk
from tkinter import filedialog
from tkinter.filedialog import asksaveasfile
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from sqlalchemy import create_engine,text
my_conn = create_engine("sqlite:///E:\\testing\\sqlite\\my_db.db")
my_conn=my_conn.connect()
my_w = tk.Tk()
my_w.geometry("400x300") # Size of the window
my_w.title('www.plus2net.com')
my_font1=('times', 18, 'bold')
l1 = tk.Label(my_w,text='Save File',width=30,font=my_font1)
l1.grid(row=1,column=1,columnspan=2)
e1=tk.Entry(my_w,width=6,bg='lightyellow',font=16)
e1.grid(row=2,column=1)
b1 = tk.Button(my_w, text='Save',
width=20,command = lambda:save_file())
b1.grid(row=2,column=2)
def save_file():
file = filedialog.asksaveasfilename(
filetypes=[("pdf file", ".pdf")],
defaultextension=".pdf")
save_pdf(file)
def save_pdf(my_path):
c = canvas.Canvas(my_path,bottomup=0)
r_set=my_conn.execute(text("SELECT * from student where id= "+ e1.get()))
data_row=r_set.fetchone()
#print(data_row[1])
c.drawRightString(2*inch,2*inch,'Name:')
c.drawRightString(4*inch,2*inch,data_row[1])
c.drawRightString(2*inch,2.3*inch,'Class:')
c.drawRightString(4*inch,2.3*inch,data_row[2])
c.showPage() # saves current page
c.save() # stores the file and close the canvas
my_w.destroy() #Close the window
my_w.mainloop() # Keep the window open
Here, we're demonstrating how to extract two columns from the student table and write them into a PDF file. This approach can be extended to include additional columns by joining multiple tables to retrieve comprehensive details. The same method can be adapted to create detailed applications or student forms for saving or printing, making it a versatile solution for various data-driven tasks.Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.