-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
141 lines (107 loc) · 4.02 KB
/
main.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
# Imports
import os
import pdb
from re import T
import sys
import pandas as pd
import yaml
from datetime import datetime
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.lib.enums import TA_CENTER
from reportlab.lib.pagesizes import A4, landscape
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import Paragraph
def readYAML(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
return yaml.load(f, Loader=yaml.FullLoader)
def cleanFolder(folder):
if os.path.exists(folder):
for file_name in os.listdir(folder):
os.remove(os.path.join(folder, file_name))
def date2Text(date):
date = date.split('/')
month = {
'01': 'Janeiro',
'02': 'Fevereiro',
'03': 'Março',
'04': 'Abril',
'05': 'Maio',
'06': 'Junho',
'07': 'Julho',
'08': 'Agosto',
'09': 'Setembro',
'10': 'Outubro',
'11': 'Novembro',
'12': 'Dezembro'
}.get(date[1])
return (date[0] + " de " + month + " de " + date[2])
def setMetadata(c, config):
c.setAuthor('Centro Acadêmico Ada Lovelace - CAAda')
c.setSubject(f'Certificado do evento \"{config["event_name"]}\" realizado em {date2Text(config["date"])}')
c.setCreator('Generated by CAAda')
c.setKeywords(f'CAAda, Certificado, {config["event_name"]}')
c.setProducer('Certificate Generator by CAAda')
def createPDF(filename, directory='.'):
if not os.path.exists(directory):
os.makedirs(directory)
return canvas.Canvas(os.path.join(directory, filename), pagesize=landscape(A4))
def drawBackground(canvas, background):
cW, cH = canvas._pagesize
canvas.drawInlineImage(background, 0, 0, width=cW, height=cH)
def drawParagraph(canvas, text, margin, x, y, style):
cW, cH = canvas._pagesize
message = Paragraph(text, style)
message.wrapOn(canvas, cW - margin, cH)
message.drawOn(canvas, x, y)
def drawLocalDate(canvas):
cW, cH = canvas._pagesize
canvas.setFillColor('white')
canvas.setFont('Helvetica', 14)
canvas.drawCentredString(
cW/2, cH*0.39, f'Bauru, {date2Text(datetime.now().strftime("%d/%m/%Y"))}')
def certGen(df, config):
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='centered', alignment=TA_CENTER,
fontSize=20, textColor='white', leading=24))
for _index, row in df.iterrows():
this = sys.modules[__name__]
# Create dynamic variables from attributes with values from the row
for attribute in config['attributes']:
if attribute == 'name':
setattr(this, attribute, row[attribute.upper()].title())
else:
setattr(this, attribute, row[attribute.upper()])
# Create PDF
filename = os.path.join(f'{name}.pdf')
c = createPDF(filename, config['directory'])
c.setTitle(name)
setMetadata(c, config)
_cW, cH = c._pagesize
drawBackground(c, config['background'])
# Format text
content = config['text'].replace('\n', '<br/>').format(
**globals(), event_name=config['event_name'], date=date2Text(config['date']))
drawParagraph(c, content, 100, 50, cH*0.45, styles['centered'])
drawLocalDate(c)
c.showPage()
c.save()
print(f'Certificado gerado para "{name}"')
if __name__ == '__main__':
config_file = readYAML('config.yaml')
input_name = config_file['input_name']
# Read config file
config = {
'event_name': config_file['event_name'],
'directory': os.path.join(os.getcwd(), config_file['output_directory_name']),
'attributes': config_file['attributes'],
'text': config_file['certificate_text'],
'background': config_file['background_image'],
'date': config_file['date']
}
# Read input file
df = pd.read_csv(input_name, sep=';')
# Clean folder
cleanFolder(config['directory'])
# Generate certificates
certGen(df, config)