from reportlab.lib.pagesizes import A4
from reportlab.lib import colors
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
TIME_SLOTS = ["9:00-10:30", "10:30-12:00", "12:00-1:30", "1:30-3:00"]
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
def export_timetable_pdf(timetable_dict, output_file="timetable.pdf"):
doc = SimpleDocTemplate(output_file, pagesize=A4)
elements = []
# Multi-line style
style = ParagraphStyle(name="Normal", fontSize=9, leading=12)
for cls, timetable in timetable_dict.items():
# Class/Program Name on top
elements.append(Paragraph(f"{cls}", ParagraphStyle(name="Heading", fontSize=14)))
elements.append(Paragraph("
", style))
# Table header
data = [["Day"] + TIME_SLOTS]
for day in DAYS:
row = [day]
for slot in TIME_SLOTS:
info = timetable[day][slot]
if info:
text = f"{info['Course']}\n{info['Teacher']}\n{info['Room']}"
row.append(Paragraph(text, style))
else:
row.append("")
data.append(row)
table = Table(data, repeatRows=1, colWidths=[60]+[100]*len(TIME_SLOTS))
table.setStyle(TableStyle([
('GRID', (0,0), (-1,-1), 1, colors.black),
('BACKGROUND', (0,0), (-1,0), colors.lightblue),
('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
('ALIGN', (0,0), (-1,0), 'CENTER'),
('ALIGN', (0,1), (-1,-1), 'CENTER'),
]))
elements.append(table)
elements.append(Paragraph("
", style))
doc.build(elements)
return output_file