Spaces:
Sleeping
Sleeping
Update pdf_utils.py
Browse files- pdf_utils.py +30 -29
pdf_utils.py
CHANGED
|
@@ -1,32 +1,33 @@
|
|
| 1 |
-
from reportlab.lib.pagesizes import
|
| 2 |
-
from reportlab.
|
| 3 |
from reportlab.lib import colors
|
| 4 |
-
from reportlab.lib.styles import getSampleStyleSheet
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
return filename
|
|
|
|
| 1 |
+
from reportlab.lib.pagesizes import letter, landscape
|
| 2 |
+
from reportlab.pdfgen import canvas
|
| 3 |
from reportlab.lib import colors
|
|
|
|
| 4 |
|
| 5 |
+
def export_timetable_pdf(timetable_dict, filename="timetable.pdf"):
|
| 6 |
+
c = canvas.Canvas(filename, pagesize=landscape(letter))
|
| 7 |
+
width, height = landscape(letter)
|
| 8 |
+
y_start = height - 50
|
| 9 |
+
for cls, df in timetable_dict.items():
|
| 10 |
+
c.setFont("Helvetica-Bold", 16)
|
| 11 |
+
c.drawString(50, y_start, cls)
|
| 12 |
+
y_start -= 30
|
| 13 |
+
c.setFont("Helvetica", 12)
|
| 14 |
+
row_height = 50
|
| 15 |
+
col_width = (width - 100)/len(df.columns)
|
| 16 |
+
# Draw header
|
| 17 |
+
for j, day in enumerate(df.columns):
|
| 18 |
+
c.setFillColor(colors.lightgrey)
|
| 19 |
+
c.rect(50 + j*col_width, y_start, col_width, row_height, fill=True)
|
| 20 |
+
c.setFillColor(colors.black)
|
| 21 |
+
c.drawString(50 + j*col_width + 5, y_start + row_height/2 - 6, day)
|
| 22 |
+
# Draw rows
|
| 23 |
+
for i, slot in enumerate(df.index):
|
| 24 |
+
for j, day in enumerate(df.columns):
|
| 25 |
+
x = 50 + j*col_width
|
| 26 |
+
y = y_start - (i+1)*row_height
|
| 27 |
+
c.rect(x, y, col_width, row_height)
|
| 28 |
+
text = df.loc[slot, day] if df.loc[slot, day] is not None else ""
|
| 29 |
+
c.drawString(x + 5, y + row_height/2 - 6, f"{slot}\n{text}")
|
| 30 |
+
y_start = y - 70 # space before next class
|
| 31 |
+
c.showPage()
|
| 32 |
+
c.save()
|
| 33 |
return filename
|