Spaces:
Sleeping
Sleeping
Update pdf_utils.py
Browse files- pdf_utils.py +45 -31
pdf_utils.py
CHANGED
|
@@ -1,33 +1,47 @@
|
|
| 1 |
-
from reportlab.lib.pagesizes import
|
| 2 |
-
from reportlab.pdfgen import canvas
|
| 3 |
from reportlab.lib import colors
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
for
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from reportlab.lib.pagesizes import A4
|
|
|
|
| 2 |
from reportlab.lib import colors
|
| 3 |
+
from reportlab.lib.styles import ParagraphStyle
|
| 4 |
+
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
|
| 5 |
|
| 6 |
+
TIME_SLOTS = ["9:00-10:30", "10:30-12:00", "12:00-1:30", "1:30-3:00"]
|
| 7 |
+
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
|
| 8 |
+
|
| 9 |
+
def export_timetable_pdf(timetable_dict, output_file="timetable.pdf"):
|
| 10 |
+
doc = SimpleDocTemplate(output_file, pagesize=A4)
|
| 11 |
+
elements = []
|
| 12 |
+
|
| 13 |
+
# Multi-line style
|
| 14 |
+
style = ParagraphStyle(name="Normal", fontSize=9, leading=12)
|
| 15 |
+
|
| 16 |
+
for cls, timetable in timetable_dict.items():
|
| 17 |
+
# Class/Program Name on top
|
| 18 |
+
elements.append(Paragraph(f"<b>{cls}</b>", ParagraphStyle(name="Heading", fontSize=14)))
|
| 19 |
+
elements.append(Paragraph("<br/>", style))
|
| 20 |
+
|
| 21 |
+
# Table header
|
| 22 |
+
data = [["Day"] + TIME_SLOTS]
|
| 23 |
+
|
| 24 |
+
for day in DAYS:
|
| 25 |
+
row = [day]
|
| 26 |
+
for slot in TIME_SLOTS:
|
| 27 |
+
info = timetable[day][slot]
|
| 28 |
+
if info:
|
| 29 |
+
text = f"{info['Course']}\n{info['Teacher']}\n{info['Room']}"
|
| 30 |
+
row.append(Paragraph(text, style))
|
| 31 |
+
else:
|
| 32 |
+
row.append("")
|
| 33 |
+
data.append(row)
|
| 34 |
+
|
| 35 |
+
table = Table(data, repeatRows=1, colWidths=[60]+[100]*len(TIME_SLOTS))
|
| 36 |
+
table.setStyle(TableStyle([
|
| 37 |
+
('GRID', (0,0), (-1,-1), 1, colors.black),
|
| 38 |
+
('BACKGROUND', (0,0), (-1,0), colors.lightblue),
|
| 39 |
+
('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
|
| 40 |
+
('ALIGN', (0,0), (-1,0), 'CENTER'),
|
| 41 |
+
('ALIGN', (0,1), (-1,-1), 'CENTER'),
|
| 42 |
+
]))
|
| 43 |
+
elements.append(table)
|
| 44 |
+
elements.append(Paragraph("<br/><br/>", style))
|
| 45 |
+
|
| 46 |
+
doc.build(elements)
|
| 47 |
+
return output_file
|