-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
148 lines (107 loc) · 4.21 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
142
143
144
145
146
147
148
import pandas as pd
import os
import smtplib
from getpass import getpass
from email.message import EmailMessage
# Importing the PIL library
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# read assignments file
assignments = pd.read_csv('responses/Assignments.csv')
assignments.dropna(subset=['Grade'], inplace=True)
assignments = assignments[assignments['Grade'] >= 60]
assignments['Email Address'] = assignments['Email Address'].str.lower()
# read quiz responses file
quizzes = pd.read_csv('responses/Quizzes.csv')
# merge both the DataFrames on common emails
final = pd.merge(assignments, quizzes, on='Email Address', how='inner')
# list of columns to keep
cols = ['Email Address', 'Name', 'Quiz-1 Score', 'Quiz-2 Score', 'Quiz-3 Score', 'Quiz-4 Score', 'Grade']
final = final[cols]
final.rename({'Grade': 'Assignment'}, axis=1, inplace=True)
# process assignment marks column
final['Assignment'] = final['Assignment'].apply(lambda x: str(int(x)) + ' / 100')
# store in csv file
final.to_csv('responses/final.csv')
# credentials of sender and address of receiver
sender_address = '[email protected]'
sender_password = getpass('Enter password: ')
pwd = os.getcwd()
# change coordinates to your liking (where the text should be placed)
bottom_right_corner = (2350, 1300)
top_left_corner = (900, 1000)
# open CSV file in read mode
df = pd.read_csv("responses/final.csv")
for index, row in df.iterrows():
name = row['Name']
# format name properly
name = name.strip().title()
# Open the image
img = Image.open('certs/PTCC Certificate.png')
# Call draw Method to add 2D graphics in an image
I1 = ImageDraw.Draw(img)
# Adjusting font size based on String length
font_size = bottom_right_corner[1] - top_left_corner[1]
font_size = font_size - int(len(name) * 2.4)
# Custom font style and font size
myFont = ImageFont.truetype('certs/DancingScript.ttf', font_size)
# Find size of the font in pixels
x, y = I1.textsize(name, font=myFont)
X = bottom_right_corner[0] - top_left_corner[0]
Y = bottom_right_corner[1] - top_left_corner[1]
x = (X - x) / 2 + top_left_corner[0]
y = (Y - y) / 2 + bottom_right_corner[1]
# Add Text to an image
I1.text((x, y), name, font=myFont, fill=(193, 175, 83))
# Save the edited image
im = img.convert('RGB')
im.save(pwd + '/certs/pdfs/' + name + '.pdf', "PDF", resolution=100.0)
# im.save(pwd + '/certs/pngs/' + name + '.png')
print("Created: " + name + "-cert.pdf")
# use other fields from each row
email = row['Email Address']
quiz_1 = row['Quiz-1 Score']
quiz_2 = row['Quiz-2 Score']
quiz_3 = row['Quiz-3 Score']
quiz_4 = row['Quiz-4 Score']
assignment = row['Assignment']
receiver_address = email
# adding a subject, from address and to addess
message = EmailMessage()
message['Subject'] = 'Python Training Certificate of Completion'
# message['Subject'] = 'Sample Email'
message['From'] = sender_address
message['To'] = receiver_address
# body of the mail
body = f'''Hello {name},
Thank You for participating in the Python Training Sessions conducted and organised by Coding Club JNTUHCEH.
Here is a summary of your scores:
Quiz - 1: {quiz_1}
Quiz - 2: {quiz_2}
Quiz - 3: {quiz_3}
Quiz - 4: {quiz_4}
Final assignment: {assignment}
As a reward of your efforts, here is your certificate!
We wish you all the best!!!
Thank you {name}
- Coding Club JNTUHCEH
'''
message.set_content(body)
# setting the path
path = f'certs/pdfs/{name}.pdf'
# opening the file in 'rb' mode
# and reading in the binary data
pdf = open(path, 'rb')
file_data = pdf.read()
file_name = os.path.basename(pdf.name)
pdf.close()
# add the binary data as an attachment
message.add_attachment(file_data, maintype='application', subtype='pdf', filename=file_name)
# send the email using the SMTP Library
session = smtplib.SMTP('smtp.gmail.com', 587) # use gmail with port
session.starttls() # enable security
session.login(sender_address, sender_password) # login with mail_id and password
session.send_message(message)
session.quit()
print(f'Mail Sent to {receiver_address}')