Spaces:
Sleeping
Sleeping
Update pdf_utils.py
Browse files- pdf_utils.py +25 -13
pdf_utils.py
CHANGED
|
@@ -1,20 +1,32 @@
|
|
| 1 |
from reportlab.lib.pagesizes import A4, landscape
|
| 2 |
-
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
|
| 3 |
from reportlab.lib import colors
|
|
|
|
| 4 |
|
| 5 |
def export_pdf(df, filename="timetable.pdf"):
|
| 6 |
pdf = SimpleDocTemplate(filename, pagesize=landscape(A4))
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
return filename
|
|
|
|
| 1 |
from reportlab.lib.pagesizes import A4, landscape
|
| 2 |
+
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
|
| 3 |
from reportlab.lib import colors
|
| 4 |
+
from reportlab.lib.styles import getSampleStyleSheet
|
| 5 |
|
| 6 |
def export_pdf(df, filename="timetable.pdf"):
|
| 7 |
pdf = SimpleDocTemplate(filename, pagesize=landscape(A4))
|
| 8 |
+
elements = []
|
| 9 |
+
styles = getSampleStyleSheet()
|
| 10 |
|
| 11 |
+
current_class = None
|
| 12 |
+
for idx, row in df.iterrows():
|
| 13 |
+
if row['Class/Program'] != current_class:
|
| 14 |
+
current_class = row['Class/Program']
|
| 15 |
+
elements.append(Spacer(1,12))
|
| 16 |
+
elements.append(Paragraph(f"<b>Class/Program: {current_class}</b>", styles['Heading2']))
|
| 17 |
+
class_df = df[df['Class/Program']==current_class]
|
| 18 |
+
data = [class_df.columns.tolist()] + class_df.values.tolist()
|
| 19 |
+
table = Table(data)
|
| 20 |
+
style = TableStyle([
|
| 21 |
+
('BACKGROUND', (0,0), (-1,0), colors.lightblue),
|
| 22 |
+
('TEXTCOLOR', (0,0), (-1,0), colors.white),
|
| 23 |
+
('ALIGN', (0,0), (-1,-1), 'CENTER'),
|
| 24 |
+
('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
|
| 25 |
+
('GRID', (0,0), (-1,-1), 1, colors.black),
|
| 26 |
+
('FONTSIZE', (0,0), (-1,-1), 9)
|
| 27 |
+
])
|
| 28 |
+
table.setStyle(style)
|
| 29 |
+
elements.append(table)
|
| 30 |
+
|
| 31 |
+
pdf.build(elements)
|
| 32 |
return filename
|