-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmain_certificate.py
101 lines (73 loc) · 3.22 KB
/
main_certificate.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
import os
import re
# os.system("pip install -r requirements.txt")
import csv
from certificate import *
from docx import Document
from docx2pdf import convert
from openpyxl import Workbook, load_workbook
mailerpath = "Data/Mail.xlsm"
htmltemplatepath = "Data/mailtemplate.html"
# create output folder if not exist
try:
os.makedirs("Output/Doc")
os.makedirs("Output/PDF")
except OSError:
pass
def updatemailer(row, workbook, sheet, email, filepath, sub, body, status, cc=""):
sheet.cell(row=row, column=1).value = email
sheet.cell(row=row, column=2).value = cc
sheet.cell(row=row, column=3).value = sub
sheet.cell(row=row, column=4).value = body
sheet.cell(row=row, column=5).value = filepath
sheet.cell(row=row, column=6).value = status
workbook.save(filename=mailerpath)
def getworkbook(filename):
wb = load_workbook(filename=filename, read_only=False, keep_vba=True)
sheet = wb.active
return wb, sheet
def gethtmltemplate(htmltemplatepath=htmltemplatepath):
return open(htmltemplatepath, "r").read()
def getmail(name, event, ambassador):
sub = f"[MLSA] Certificate of Participation for {name}"
html = gethtmltemplate(htmltemplatepath)
body = html.format(name=name, event=event, ambassador=ambassador)
return sub, body
def get_participants(f):
data = [] # create empty list
with open(f, mode="r", encoding='iso-8859-1') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
data.append(row) # append all results
return data
def create_docx_files(filename, list_participate):
wb, sheet = getworkbook(mailerpath)
event = input("Enter the event name: ")
ambassador = input("Enter Ambassador Name: ")
for index, participate in enumerate(list_participate):
# use original file everytime
doc = Document(filename)
participate['Name'] = participate.pop(next(key for key in participate if re.search(r'\bName\b', key)))
participate['Email'] = participate.pop(next(key for key in participate if re.search(r'\bEmail\b', key)))
name = participate["Name"]
email = participate["Email"]
replace_participant_name(doc, name)
replace_event_name(doc, event)
replace_ambassador_name(doc, ambassador)
doc.save('Output/Doc/{}.docx'.format(name))
doc.save('Output/Doc/{}.docx'.format(name))
# ! if your program working slowly, comment this two line and open other 2 line.
print("Output/{}.pdf Creating".format(name))
convert('Output/Doc/{}.docx'.format(name), 'Output/Pdf/{}.pdf'.format(name))
filepath = os.path.abspath('Output/Pdf/{}.pdf'.format(name))
sub, body = getmail(name, event, ambassador)
updatemailer(row=index + 2, workbook=wb, sheet=sheet, email=email, filepath=filepath, sub=sub, body=body,
status="Send")
# get certificate temple path
certificate_file = "Data/Event Certificate Template.docx"
# get participants path
participate_file = "Data/" + ("ParticipantList.csv" if (input("Test Mode (Y/N): ").lower())[0] == "n" else "temp.csv")
# get participants
list_participate = get_participants(participate_file);
# process data
create_docx_files(certificate_file, list_participate)