To create a Python exam questions PDF, you can use a combination of Python libraries, such as reportlab
and PyPDF2
, to generate and manipulate PDF files. Here’s an example of how to create a basic exam questions PDF:
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from PyPDF2 import PdfFileMerger
# define the exam questions
questions = [
"What is the capital of France?",
"What is the tallest mountain in the world?",
"Who wrote the book 'To Kill a Mockingbird'?",
"What is the largest organ in the human body?",
"What is the boiling point of water in Celsius?",
]
# create a new PDF document
pdf = canvas.Canvas("exam_questions.pdf", pagesize=letter)
# set the font and font size for the questions
pdf.setFont("Helvetica", 12)
# iterate over the questions and add them to the PDF
for i, question in enumerate(questions):
y = 750 - i*50
pdf.drawString(50, y, f"{i+1}. {question}")
# save the PDF document
pdf.save()
# merge the PDF document with a cover page
pdf_cover = canvas.Canvas("exam_cover.pdf", pagesize=letter)
pdf_cover.setFont("Helvetica", 16)
pdf_cover.drawString(50, 750, "Exam Questions")
pdf_cover.save()
pdf_merger = PdfFileMerger()
pdf_merger.append("exam_cover.pdf")
pdf_merger.append("exam_questions.pdf")
pdf_merger.write("exam.pdf")
pdf_merger.close()
In this example, we first define the exam questions as a list of strings.
We create a new PDF document using the canvas.Canvas()
function from the reportlab
library, and set the page size to letter.
We set the font and font size for the questions using the setFont()
method of the PDF canvas.
We iterate over the questions using a for
loop and add each question to the PDF using the drawString()
method of the PDF canvas. We use the enumerate()
function to add the question number to each question.
We save the PDF document using the save()
method of the PDF canvas.
We create a cover page for the exam using another PDF document, and merge it with the exam questions PDF using the PdfFileMerger
class from the PyPDF2
library.
We save the merged PDF as exam.pdf
.
Note that this is just a basic example of how to create a Python exam questions PDF. You can customize the content and formatting of the PDF to suit your specific needs, such as adding images, tables, or multiple choice questions.
+ There are no comments
Add yours