-
Notifications
You must be signed in to change notification settings - Fork 5
/
auto.py
148 lines (98 loc) · 4.72 KB
/
auto.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
import email
from itertools import count
from PIL import Image, ImageDraw, ImageFont
import os, shutil, xlrd
import smtplib
import mimetypes
from email.message import EmailMessage
# Global Variables
spreadsheet_file = 'test.xlsx'
font = ImageFont.truetype('fonts/Poppins-Medium.ttf', 60) # Setting the font to Poppins Medium and font size to 40
names = []
emails = []
# Creates a new 'generated' folder if not already present
def folder_check():
if os.path.isdir("generated_certificates"):
shutil.rmtree("generated_certificates")
os.mkdir("generated_certificates")
else:
os.mkdir("generated_certificates")
def findNameCol(sheet):
for i in range(sheet.ncols):
if "NAME" in sheet.cell_value(0, i).upper(): # Programmatically finds column heading 'name' in the spreadsheet, without explicitly asking the user
name_col = i
return name_col
def findEmailCol(sheet):
for j in range(sheet.ncols):
if "EMAIL" in sheet.cell_value(0, j).upper(): # Programmatically finds column heading 'name' in the spreadsheet, without explicitly asking the user
email_col = j
return email_col
# Gets names from the xlsx file
def getNames():
workbook = xlrd.open_workbook(spreadsheet_file)
sheet = workbook.sheet_by_index(0) # Getting first sheet of the xlsx workbook
for i in range(sheet.nrows):
names.append(sheet.cell_value(i, findNameCol(sheet))) # Extracts names from the column and inserts it into the array
del names[0] # Deleting the column heading
return names
def getEmails():
workbook = xlrd.open_workbook(spreadsheet_file)
sheet = workbook.sheet_by_index(0) # Getting first sheet of the xlsx workbook
for j in range(sheet.nrows):
emails.append(sheet.cell_value(j, findEmailCol(sheet))) # Extracts names from the column and inserts it into the array
del emails[0] # Deleting the column heading
return emails
def generate(names,emails):
def sendEmail():
for counter in range(len(emails)):
name = names[counter]
email = emails[counter]
message = EmailMessage()
sender = "[email protected]"
recipient = email
message['From'] = sender
message['To'] = recipient
message['Subject'] = name + ", your certificate is here"
body = "Hi " + name + ",\n\nSorry there was a clerical mistake in the certificate attached with the last mail."+"""\nContent
Please find the file attached with this email.
Do revert back to this mail incase of any errors/trouble.
Have a Good Day!
Regards,
Amal Shibu
CEO, IEDC JEC"""
message.set_content(body)
fname=r'generated_certificates/'+name + ".png"
# print(fname)
mime_type, _ = mimetypes.guess_type(fname)
mime_type, mime_subtype = mime_type.split('/')
with open(fname, 'rb') as file:
message.add_attachment(file.read(),
maintype=mime_type,
subtype=mime_subtype,
filename=name+".png")
mail_server = smtplib.SMTP_SSL('smtp.gmail.com')
mail_server.set_debuglevel(0)
mail_server.login("mailid", 'password')
mail_server.send_message(message)
print("Recipient Name : "+name)
print("Recipient Mail Id : "+recipient)
print("Msg :\n"+body)
print("\n\n")
print("-------------------------------------")
print("\n\n")
mail_server.quit()
folder_check()
def gencertificate():
for name in names:
print("Generating " + name + ".png")
img = Image.open("templates/ctf.png") # Loading the certificate template
draw = ImageDraw.Draw(img)
w, h = draw.textsize(name, font=font)
name_x, name_y = (1990-w)/2, (((350-h)/2)+430) # Setting the co-ordinates to where the names should be entered
draw.text((name_x, name_y), name, font=font, fill="black")
img.save(r'generated_certificates/' + name + ".png") # Saving the images to the generated_certificates directory
print("\n\n")
gencertificate() #Generate and save certiciates according to the name
sendEmail() #Sends email to the given mail id
if __name__ == "__main__":
generate(getNames(),getEmails())