0% found this document useful (0 votes)
17 views5 pages

Question Paper Generator.ipynb

The document contains a Python script for a Test Generator System that uses SQLite for database management and FPDF for PDF generation. It allows users to add questions to a database and generate test papers based on selected difficulty levels, which are then emailed to the user. The script includes a graphical user interface built with Tkinter for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

Question Paper Generator.ipynb

The document contains a Python script for a Test Generator System that uses SQLite for database management and FPDF for PDF generation. It allows users to add questions to a database and generate test papers based on selected difficulty levels, which are then emailed to the user. The script includes a graphical user interface built with Tkinter for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

{

"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import sqlite3\n",
"from tkinter import *\n",
"import webbrowser\n",
"import smtplib\n",
"import os\n",
"from email.mime.multipart import MIMEMultipart\n",
"from email.mime.text import MIMEText\n",
"from email.mime.base import MIMEBase\n",
"from email import encoders\n",
"import fpdf\n",
"import random\n",
"pdf = fpdf.FPDF(format='letter')\n",
"from PIL import ImageTk, Image\n",
"conn=sqlite3.connect('paper.db')\n",
"c=conn.cursor()\n",
"\n",
"# to view all the table deatials in the database\n",
"c.execute(\"SELECT name FROM sqlite_master where type='table';\")\n",
"print(c.fetchall())\n",
"\n",
"#c.execute(\"\"\"CREATE TABLE EASY6(ID INT PRIMARY KEY NOT NULL, QS CHAR(1000)
NOT NULL)\"\"\")\n",
"#c.execute(\"INSERT INTO testpaper VALUES ('Difference between A* and
AO*',5,37)\")\n",
"#c.execute(\"SELECT * FROM testpaper\")\n",
"#print(c.fetchall())\n",
"#conn.commit()\n",
"#conn.close()\n",
"\n",
"def pdfgen(list1,list2,list3):\n",
" pdf.add_page()\n",
" pdf.set_font(\"Arial\", size=15)\n",
" pdf.cell(200,15,\"Question paper NCU 2018\", ln=1, align=\"C\")\n",
" pdf.set_font(\"Arial\",'i', size=14)\n",
" pdf.cell(200,15,\"Generated using an automated paper generation system\",
ln=1, align=\"C\")\n",
" pdf.set_font(\"Times\", size=10)\n",
" pdf.cell(167,15,\"Max Marks : 100\", align=\"left\")\n",
" pdf.cell(100,15,\"Time : 3 Hours\",ln=1, align=\"right\")\n",
" pdf.set_font(\"Arial\",'b', size=13)\n",
" pdf.cell(134,15,\"Section A\", align=\"left\")\n",
" pdf.set_font(\"Arial\",'i', size=11)\n",
" pdf.cell(100,15,\"Max marks for this section are 4\",ln=1,
align=\"left\")\n",
" pdf.set_font(\"Times\", size=10)\n",
" for i in range(5):\n",
" pdf.cell(170,6,\"Q\"+str(i+1)+\": \"+list1[i][0],ln=1,align=\"left\")\
n",
" pdf.set_font(\"Arial\",'b', size=13)\n",
" pdf.cell(134,15,\"Section B\", align=\"left\")\n",
" pdf.set_font(\"Times\",'i', size=11)\n",
" pdf.cell(100,15,\"Max marks for this section are 6\",ln=1,
align=\"left\")\n",
" pdf.set_font(\"Arial\", size=10)\n",
" for i in range(5):\n",
" pdf.cell(170,6,\"Q\"+str(i+1)+\": \"+list2[i][0],ln=1,align=\"left\")\
n",
" pdf.set_font(\"Arial\",'b', size=13)\n",
" pdf.cell(133,15,\"Section C\", align=\"left\")\n",
" pdf.set_font(\"Times\",'i', size=11)\n",
" pdf.cell(100,15,\"Max marks for this section are 10\",ln=1,
align=\"left\")\n",
" pdf.set_font(\"Arial\", size=10)\n",
" for i in range(5):\n",
" pdf.cell(170,6,\"Q\"+str(i+1)+\": \"+list3[i][0],ln=1,align=\"left\")\
n",
" pdf.output(\"test.pdf\")\n",
" fromaddr = \"[email protected]\"\n",
" toaddr = \"[email protected]\"\n",
"\n",
" msg = MIMEMultipart()\n",
"\n",
" msg['From'] = fromaddr\n",
" msg['To'] = toaddr\n",
" msg['Subject'] = \"Generated test paper\"\n",
" body = \"Here is your generated test sir\"\n",
" msg.attach(MIMEText(body, 'plain'))\n",
"\n",
" filename = \"test.pdf\"\n",
" attachment = open(\"D:\\\\blah\\\\test.pdf\", \"rb\")\n",
"\n",
" part = MIMEBase('application', 'octet-stream')\n",
" part.set_payload((attachment).read())\n",
" encoders.encode_base64(part)\n",
" part.add_header('Content-Disposition', \"attachment; filename= %s\" %
filename)\n",
"\n",
" msg.attach(part)\n",
" conn=smtplib.SMTP('smtp.gmail.com',587)\n",
" conn.ehlo()\n",
" conn.starttls()\n",
" conn.login('[email protected]',os.environ[\"pass\"])\n",
" text = msg.as_string()\n",
" conn.sendmail(fromaddr, toaddr, text)\n",
" conn.quit()\n",
"\n",
"def addqs(qs, marks, diff):\n",
" c.execute(\"SELECT MAX(ID) FROM \"+diff+marks)\n",
" data = c.fetchone()\n",
" if data[0] is None:\n",
" i = 1\n",
" else:\n",
" i = data[0] + 1\n",
" c.execute(\"INSERT INTO \"+diff+marks+\" VALUES(\"+str(i)
+\",\\\"\"+qs+\"\\\");\")\n",
" print(qs)\n",
" conn.commit()\n",
" \n",
"\n",
"def ques_select(diff3):\n",
" c.execute(\"SELECT MAX(ID) FROM \"+diff3+\"4\")\n",
" data1=c.fetchone()\n",
" c.execute(\"SELECT MAX(ID) FROM \"+diff3+\"6\")\n",
" data2=c.fetchone()\n",
" c.execute(\"SELECT MAX(ID) FROM \"+diff3+\"10\")\n",
" data3=c.fetchone()\n",
" \n",
" if data1[0]<5 or data2[0]<5 or data3[0]<5:\n",
" print(\"Not sufficient elements in Tables\")\n",
" exit()\n",
" else:\n",
" rand1 = random_num_gen(data1[0])\n",
" rand2 = random_num_gen(data2[0])\n",
" rand3 = random_num_gen(data3[0])\n",
" obj1 = []\n",
" obj2 = []\n",
" obj3 = []\n",
" for i in range(5):\n",
" c.execute(\"SELECT QS FROM \"+diff3+\"4 WHERE \"+\"ID
= \"+str(rand1[i]))\n",
" obj1.append(list(c.fetchone()))\n",
" c.execute(\"SELECT QS FROM \"+diff3+\"6 WHERE \"+\"ID
= \"+str(rand2[i]))\n",
" obj2.append(list(c.fetchone()))\n",
" c.execute(\"SELECT QS FROM \"+diff3+\"10 WHERE \"+\"ID
= \"+str(rand3[i]))\n",
" obj3.append(list(c.fetchone()))\n",
" pdfgen(obj1,obj2,obj3)\n",
" \n",
"def random_num_gen(n):\n",
" rlist = random.sample(range(n),5)\n",
" rlist = [x+1 for x in rlist]\n",
" return rlist\n",
"\n",
"def genwin():\n",
" main=Toplevel()\n",
" main.geometry(\"600x400+8+400\")\n",
" main.title(\"Generate Test\")\n",
" canvas=Canvas(main,width=470,height=80,relief='raised',borderwidth=3)\n",
" canvas.grid(row=0,column=1,padx=50,pady=20)\n",
" canvas.create_text(250,50,fill=\"blue\",font=\"Times 13 italic bold\",\n",
" text=\"Generated test will be e-mailed to
you!!!\")\n",
" \n",
" def click2():\n",
" diff3=variable.get()\n",
" ques_select(diff3)\n",
" \n",
" frame=Frame(main)\n",
" frame.grid(row=1,column=1,padx=50,pady=40)\n",
" frame2=Frame(frame)\n",
" frame2.grid(row=0,column=1,padx=50,pady=40)\n",
" button1=Button(frame,text=\"Generate test\",font=\"Times 10 italic
bold\")\n",
" gg=Label(frame,text=\"Please select the complexity level\")\n",
" gg.grid(row=0,column=0)\n",
" OPTIONS = ['Easy','Medium','Hard']\n",
" variable = StringVar()\n",
" variable.set(OPTIONS[0])\n",
"\n",
" w = OptionMenu(frame2, variable, *OPTIONS)\n",
" w.grid(row=0,column=1)\n",
" button1.config( height =2, width = 13,bg=\"yellow\", command=click2)\n",
" button1.grid(row=1,column=0,padx=10,pady=30)\n",
"\n",
"def addwin():\n",
" main=Toplevel()\n",
" main.geometry(\"600x400+8+400\")\n",
" main.title(\"Add Question\")\n",
" canvas=Canvas(main,width=470,height=80,relief='raised',borderwidth=3)\n",
" canvas.grid(row=0,column=1,padx=80,pady=20)\n",
" canvas.create_text(250,50,fill=\"green\",font=\"Times 15 italic bold\",\
n",
" text=\"Please Add Relevant Questions\")\n",
" def click1():\n",
" ques=namez.get()\n",
" diff=variable1.get()\n",
" marks=variable2.get()\n",
" c.execute(\"create table if not exists \"+diff+marks+\" (ID INT
PRIMARY KEY NOT NULL, QS CHAR(1000) NOT NULL)\")\n",
" addqs(ques, marks, diff)\n",
" frame=Frame(main)\n",
" lbl1 = Label(frame, text = \"Enter question\",background='#ECECEC')\n",
" lbl1.grid(row=0,column=0,pady=0) \n",
" namez=Entry(frame)\n",
" namez.grid(row=0,column=1,pady=0)\n",
" lbl2=Label(frame,text=\"Enter Question complexity\")\n",
" lbl2.grid(row=1,column=0)\n",
" OPTIONS = ['Easy','Medium','Hard']\n",
" variable1 = StringVar()\n",
" variable1.set(OPTIONS[0])\n",
" namez2 = OptionMenu(frame, variable1, *OPTIONS)\n",
" namez2.grid(row=1,column=1)\n",
" \n",
" lbl3=Label(frame,text=\"Enter The Marks\")\n",
" lbl3.grid(row=2,column=0)\n",
" OPTIONS = [4,6,10]\n",
" variable2 = StringVar()\n",
" variable2.set(OPTIONS[0])\n",
" namez3 = OptionMenu(frame, variable2, *OPTIONS)\n",
" namez3.grid(row=2,column=1)\n",
" frame.grid(row=1,column=1,padx=50,pady=40)\n",
" button1=Button(frame,text=\"Add Question To Database\",font=\"Times 10
italic bold\",command=click1)\n",
" button1.config( height =2, width = 30,bg=\"yellow\")\n",
" button1.grid(row=3,column=1,padx=0)\n",
"\n",
"def mainwin():\n",
" main = Tk()\n",
" main.title(\"Test Generator System\")\n",
" img = ImageTk.PhotoImage(Image.open(\"a.gif\"))\n",
" main.geometry(\"800x600+8+400\")\n",
"\n",
" canvas=Canvas(main,width=470,height=80,relief='raised',borderwidth=3)\n",
" canvas.grid(row=0,column=4,padx=80,pady=20)\n",
" canvas.create_text(250,50,fill=\"green\",font=\"Times 15 italic bold\",\
n",
" text=\"Best Test Generator Ever! Easy to use
too!!\")\n",
"\n",
" text=Label(main,text=\"Welcome to Test Generator, if you have new
questions please add them\",font=\"Times 15 italic bold\")\n",
" text.configure(bg=\"green\",foreground=\"yellow\")\n",
" text.grid(row=1,column=4,padx=80,pady=40)\n",
" panel = Label(main, image = img,height=220,width=600)\n",
" panel.grid(row=3,column=4,padx=80,pady=20)\n",
" frame=Frame(main)\n",
" frame.grid(row=4,column=4,padx=50,pady=20)\n",
" button1=Button(frame,text=\"Add Question\",font=\"Times 10 italic
bold\",command=addwin)\n",
" button1.config( height =3, width = 15,bg=\"green\")\n",
" button1.grid(row=0,column=0,padx=50)\n",
" button2=Button(frame,text=\"Generate test\",font=\"Times 10 italic
bold\",command=genwin)\n",
" button2.config( height =3, width = 15,bg=\"yellow\")\n",
" button2.grid(row=0,column=1)\n",
" main.mainloop()\n",
"mainwin()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

You might also like