aneesqumar commited on
Commit
0f1afca
·
verified ·
1 Parent(s): bbf6fad

Update pdf_utils.py

Browse files
Files changed (1) hide show
  1. pdf_utils.py +17 -25
pdf_utils.py CHANGED
@@ -1,28 +1,20 @@
1
- from reportlab.lib.pagesizes import A4
2
- from reportlab.pdfgen import canvas
3
- import pandas as pd
4
 
5
- def export_pdf(df: pd.DataFrame, filename="timetable.pdf"):
6
- c = canvas.Canvas(filename, pagesize=A4)
7
- width, height = A4
8
 
9
- x = 40
10
- y = height - 40
11
-
12
- c.setFont("Helvetica-Bold", 14)
13
- c.drawString(x, y, "Weekly Time Table")
14
- y -= 30
15
-
16
- c.setFont("Helvetica", 10)
17
-
18
- for _, row in df.iterrows():
19
- line = f"{row['Day']} | {row['Time']} | {row['Course']} | {row['Teacher']} | {row['Room']}"
20
- c.drawString(x, y, line)
21
- y -= 18
22
-
23
- if y < 40:
24
- c.showPage()
25
- y = height - 40
26
-
27
- c.save()
28
  return filename
 
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