-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcertificate_generator.py
More file actions
61 lines (42 loc) · 1.86 KB
/
certificate_generator.py
File metadata and controls
61 lines (42 loc) · 1.86 KB
1
2
3
4
5
6
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import sys
import os
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
import pandas as pd
script_path = sys.argv[0]
project_path, _ = os.path.split(script_path)
resources_path = os.path.join(project_path, 'resources')
certificates_path = os.path.join(project_path, 'certificates')
certificate_template_path = os.path.join(certificates_path, 'certificate_template-14-02-2023.pdf')
participant_list_path = os.path.join(certificates_path, 'Dados-Certificados-14-02-2023.xlsx')
df_participants = pd.read_excel(participant_list_path)
print("processing participant...")
for index, row in df_participants.iterrows():
f_name = row['First names'].replace(" ", "")
s_name = row['Second names'].replace(" ", "")
print(f_name, s_name)
output_filename = '{}_{}_certificate.pdf'.format(f_name, s_name)
output_file_path = os.path.join(certificates_path, output_filename)
existing_pdf = PdfFileReader(open(certificate_template_path, "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.pages[0]
page_width, page_height = existing_pdf.pages[0].mediaBox.upperRight
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=(page_width, page_height))# letter)
can.setFont("Helvetica-Bold", 30)
# TODO: choose the coordinates for inserting centered text here
x = int(can._pagesize[0] / 2) # half way horizontally
y = 300 # custom to 300 (by trial and error)
can.drawCentredString(x, y, "{} {}".format(f_name, s_name))
can.save()
# create a new PDF with Reportlab
new_pdf = PdfFileReader(packet)
packet.seek(0)
page.merge_page(new_pdf.pages[0])
output.add_page(page)
# write "output" to a file
output_stream = open(output_file_path, "wb")
output.write(output_stream)
output_stream.close()