-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_cert.py
152 lines (125 loc) · 4.03 KB
/
gen_cert.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# import the necessary libraries</pre>
# pip3 install openpyxl
import os
import openpyxl
from PIL import Image, ImageDraw, ImageFont
import img2pdf
def gen_cert_img(details_path,
template_path,
output_path,
font_style,
output_path_pdf,
pdf_para_rel):
# loading the details.xlsx workbook
# and grabbing the active sheet
obj = openpyxl.load_workbook(details_path)
sheet = obj.active
# excel sheet
for i in range(1, sheet.max_row+1):
# grab the name of the participant
name = sheet.cell(row=i, column=1).value
print("Generating certificate:" + name)
# grab the id course
course_id = sheet.cell(row=i, column=2).value
# grab the course name
course_name = sheet.cell(row=i, column=3).value
# grab the number of hours
number_hours = str(sheet.cell(row=i, column=4).value)
# grab the date
date_cert = str(sheet.cell(row=i, column=5).value)
# style of the font
font_name = ImageFont.truetype(font_style, 55)
font_course = ImageFont.truetype(font_style, 40)
font_numb_hour = ImageFont.truetype(font_style, 35)
font_date = ImageFont.truetype(font_style, 30)
# certificate template
img = Image.open(template_path, mode='r')
image_width = img.width
image_height = img.height
draw = ImageDraw.Draw(img)
"""
draw the name on the certificate
"""
text_width = draw.textlength(name, font_name)
# x,y
draw.text(
(
(image_width - text_width) / 2,
486
),
name,
font=font_name,
fill=(0, 0, 0)
)
"""
draw the course name on the certificate
"""
text_width = draw.textlength(course_name, font_course)
# x,y
draw.text(
(
(image_width - text_width) / 2,
720
),
course_name,
font=font_course,
fill=(0, 0, 0)
)
"""
draw the number of hours on the certificate
"""
text_width = draw.textlength(number_hours, font_numb_hour)
# x,y
draw.text(
(
1180,
858
),
number_hours,
font=font_numb_hour,
fill=(0, 0, 0)
)
"""
draw the date on the certificate
"""
text_width = draw.textlength(date_cert, font_date)
# x,y
draw.text(
(
920,
1000
),
date_cert,
font=font_date,
fill=(0, 0, 0)
)
# save the certificate as PNG
img_pathname = output_path + name + "_" + str(course_id) + ".png"
img.save(img_pathname)
pdf_pathname = output_path_pdf + name + "_" + str(course_id) + ".pdf"
with open(pdf_pathname, "wb") as f:
f.write(img2pdf.convert(img_pathname))
# Pdf para Relatório (1 file somente)
imgs = []
for fname in os.listdir(output_path):
if not fname.endswith(".png"):
continue
path = os.path.join(output_path, fname)
if os.path.isdir(path):
continue
imgs.append(path)
with open(pdf_para_rel + "pdf-relatorio" + str(course_id) + ".pdf","wb") as f:
f.write(img2pdf.convert(imgs))
if __name__ == "__main__":
# style of the font
font_style = "Playwrite_SK/PlaywriteSK-VariableFont_wght.ttf"
# template of the certificate
template_path = 'modelo_certificado_data.png'
# Excel file containing names of the participants
details_path = 'list_alunos.xlsx'
# Output Paths
output_path = 'certificate_results_png/'
output_path_pdf = 'certificate_results_pdf/'
pdf_para_rel = 'pdf_para_rel/'
gen_cert_img(details_path, template_path,
output_path, font_style, output_path_pdf, pdf_para_rel)