Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions create_sample_exams.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
in the input/ folder, so you can test the system without needing real exam papers.
"""

from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.lib.enums import TA_CENTER, TA_LEFT
import os
try:
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.enums import TA_CENTER
REPORTLAB_AVAILABLE = True
except ImportError:
REPORTLAB_AVAILABLE = False

from pathlib import Path

# Import path utilities
Expand Down Expand Up @@ -72,7 +76,7 @@
def create_sample_pdf(filename: str, exam_data: dict):
"""Create a sample exam PDF with the given questions."""
filepath = str(get_input_path(filename)) # Convert Path to string

# Create PDF document
doc = SimpleDocTemplate(
filepath,
Expand All @@ -82,10 +86,10 @@ def create_sample_pdf(filename: str, exam_data: dict):
topMargin=72,
bottomMargin=18
)

# Container for the 'Flowable' objects
elements = []

# Define styles
styles = getSampleStyleSheet()
title_style = ParagraphStyle(
Expand All @@ -96,20 +100,20 @@ def create_sample_pdf(filename: str, exam_data: dict):
spaceAfter=30,
alignment=TA_CENTER
)

question_style = ParagraphStyle(
'Question',
parent=styles['BodyText'],
fontSize=11,
spaceAfter=12,
leftIndent=20
)

# Add title
title = Paragraph(exam_data["title"], title_style)
elements.append(title)
elements.append(Spacer(1, 0.2*inch))

# Add instructions
instructions = Paragraph(
"<b>Instructions:</b> Answer all questions. Show all work for calculation problems. "
Expand All @@ -118,51 +122,51 @@ def create_sample_pdf(filename: str, exam_data: dict):
)
elements.append(instructions)
elements.append(Spacer(1, 0.3*inch))

# Add questions
for question_text, bloom_level in exam_data["questions"]:
question = Paragraph(question_text, question_style)
elements.append(question)
elements.append(Spacer(1, 0.15*inch))

# Add footer
elements.append(Spacer(1, 0.5*inch))
footer = Paragraph(
f"<i>This is a sample exam generated for testing purposes. "
f"Bloom's levels included for demonstration.</i>",
"<i>This is a sample exam generated for testing purposes. "
"Bloom's levels included for demonstration.</i>",
styles['Italic']
)
elements.append(footer)

# Build PDF
doc.build(elements)
print(f"✓ Created {filename}")
print("✓ Created {filename}".format(filename=filename))


def main():
"""Generate all sample exam PDFs."""
print("Generating sample exam PDFs...\n")

# Ensure input directory exists
ensure_directories()


# Check if reportlab is available
try:
import reportlab
except ImportError:
if not REPORTLAB_AVAILABLE:
print("ERROR: reportlab is required to generate sample PDFs")
print("Install it with: pip install reportlab")
return 1


# Ensure input directory exists
ensure_directories()

# Generate each exam
for filename, exam_data in SAMPLE_EXAMS.items():
try:
create_sample_pdf(filename, exam_data)
except Exception as e:
print(f"✗ Failed to create {filename}: {e}")

print(f"\n✓ Successfully generated {len(SAMPLE_EXAMS)} sample exam PDFs")
print(f"✓ Files saved to: {get_input_path('')}")
print("✗ Failed to create {filename}: {error}".format(
filename=filename, error=e))

print("\n✓ Successfully generated {count} sample exam PDFs".format(
count=len(SAMPLE_EXAMS)))
print("✓ Files saved to: {path}".format(path=get_input_path('')))
print("\nYou can now run: python demo.py")
return 0

Expand Down
Loading
Loading