aneesqumar commited on
Commit
99e5fdf
·
verified ·
1 Parent(s): 640f1cf

Update pdf_utils.py

Browse files
Files changed (1) hide show
  1. 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
- data = [df.columns.tolist()] + df.values.tolist()
 
8
 
9
- table = Table(data)
10
- style = TableStyle([
11
- ('BACKGROUND', (0,0), (-1,0), colors.lightblue),
12
- ('TEXTCOLOR', (0,0), (-1,0), colors.white),
13
- ('ALIGN', (0,0), (-1,-1), 'CENTER'),
14
- ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
15
- ('GRID', (0,0), (-1,-1), 1, colors.black),
16
- ('FONTSIZE', (0,0), (-1,-1), 10)
17
- ])
18
- table.setStyle(style)
19
- pdf.build([table])
 
 
 
 
 
 
 
 
 
 
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