-
Notifications
You must be signed in to change notification settings - Fork 2
/
edit.py
260 lines (218 loc) · 9.66 KB
/
edit.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import smtplib
import ssl
import csv
import xlrd
import os
import sys
from PIL import Image, ImageDraw, ImageFont
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
class Certificates:
def __init__(self):
self.totals = []
self.emails = []
self.names = []
self.email_loaction = 0
self.name_location = 0
self.path = ''
self.text_x = 0
self.text_y = 0
self.size = 30
self.sample = False
self.satisfied = 0
self.counts = 1
self.counts_1 = 0
self.server = ''
self.port = 587
self.font_path = 'C:/Windows/Fonts/Arial/ariblk.ttf'
self.attachment = ''
self.part = ''
self.text = ''
self.msg = ''
def _draw(self, certificate_file, name):
try:
completePath = os.path.join(self.path, name)
img = Image.open(certificate_file, mode='r')
image_width = img.width
image_height = img.height
draw = ImageDraw.Draw(img)
font = ImageFont.truetype(self.font_path, size=self.size)
text_width, _ = draw.textsize(name, font=font)
draw.text(
(
(image_width - (text_width - self.text_x)) / 2,
self.text_y
),
name,
fill='rgb(0, 0, 0)',
font=font)
img.save("{}.png".format(completePath))
print(f'{name} --------> {self.counts}')
self.counts += 1
except FileNotFoundError:
print('\nPlease check your filename!!!')
sys.exit(0)
def _createPath(self):
try:
if self.path == '':
self.path = os.path.join(os.getcwd(), 'Certificates')
os.mkdir(self.path)
except FileExistsError:
return
def _send_mails(self, username, password, subject, body):
if self.emails == []:
print('\nNo mail Ids are provided to send the mails')
return
else:
totals = len(self.emails)
try:
self.server = smtplib.SMTP('smtp.gmail.com', self.port)
self.server.starttls()
self.server.login(username, password)
addImages = (int(input(
'\nDo you want to attach Certificates to the mails (1 for yes | 0 for no) : ')))
for i in range(len(self.emails)):
self.msg = MIMEMultipart()
self.msg['From'] = username
self.msg['To'] = self.emails[i]
self.msg['Subject'] = subject
self.msg.attach(MIMEText(body, 'plain'))
if(addImages == 1):
filename = os.path.join(
self.path, self.names[i]+'.png')
self.attachment = open(filename, 'rb')
self.part = MIMEBase('application', 'octet-stream')
self.part.set_payload((self.attachment).read())
encoders.encode_base64(self.part)
self.part.add_header('Content-Disposition',
"self.attachment; filename= "+filename)
self.msg.attach(self.part)
self.text = self.msg.as_string()
self.server.sendmail(username, self.emails[i], self.text)
self.counts_1 += 1
print(
f'{self.counts_1} / {totals} --------- {self.emails[i]}')
self.server.quit()
except smtplib.SMTPAuthenticationError:
print('Please Check your username and Password, \n\n And make sure you have turned on the allow less secure apps for your account.')
def renderCertificate(self, certificate_file):
if len(self.names) > 0 and ('.png' in certificate_file or '.jpeg' in certificate_file):
self._createPath()
while(not self.sample):
print('\n**************************')
try:
self.counts = 0
self.text_x = int(input(
"\nEnter the x position (default = 0 | to continue with default press Enter) : "))
except ValueError:
self.text_x = 0
try:
self.text_y = int(
input('\nPlease enter a Text y position : (300-500) - please experiment it : '))
except ValueError:
self.text_y = 300
try:
self.size = int(input(
'\nPlease provide a text size (default value is 30), press enter to Continue : '))
except:
self.size = 30
self._draw(certificate_file, self.names[0])
print(
'\nPlease preview your certificate sample in the images folder in the root directory of the program')
print('\n**************************')
try:
self.satisfied = int(input(
'\n Press (1 to proceed to all files) else (0 to re render the certificate) [ 1 - proceed || 0 - re-render ] : '))
if(self.satisfied):
self.sample = True
except ValueError:
self.satisfied = 0
for name in self.names:
self._draw(certificate_file, name)
return
def read_file(self, filename, getEmails=True, getNames=True, encoding_f='utf-8'):
try:
workbook = xlrd.open_workbook(filename)
sheet = workbook.sheet_by_index(0)
for i in range(sheet.nrows):
sam_lst = [str(sheet.cell_value(i,j)) for j in range(sheet.ncols)]
self.totals.append(sam_lst)
for i in self.totals[0]:
x = ''.join(i.split())
x = x.lower()
if x == 'emailaddress' or x == 'email' or x == 'emailid':
self.email_loaction = self.totals[0].index(i)
elif x == 'name' or x == 'fullname' or x == 'full-name':
self.name_location = self.totals[0].index(i)
self.totals.pop(0)
if getNames and getEmails:
for i in self.totals:
if i[self.name_location] != '':
self.names.append(i[self.name_location])
self.emails.append(i[self.email_loaction])
elif getEmails:
for i in self.totals:
self.emails.append(i[self.email_loaction])
else:
for i in self.totals:
self.names.append(i[self.name_location])
print('\n******************')
print('\nFile Read Successful')
return True
except xlrd.biffh.XLRDError:
try:
with open(filename, encoding=encoding_f) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for i in csv_reader:
self.totals.append(i)
for i in self.totals[0]:
x = ''.join(i.split())
x = x.lower()
if x == 'emailaddress' or x == 'email' or x == 'emailid':
self.email_loaction = self.totals[0].index(i)
elif x == 'name' or x == 'fullname' or x == 'full-name':
self.name_location = self.totals[0].index(i)
self.totals.pop(0)
if getNames and getEmails:
for i in self.totals:
if i[self.name_location] != '':
self.names.append(i[self.name_location])
self.emails.append(i[self.email_loaction])
elif getEmails:
for i in self.totals:
self.emails.append(i[self.email_loaction])
else:
for i in self.totals:
self.names.append(i[self.name_location])
csv_file.close()
print('\n******************')
print('\nFile Read Successful')
return True
except UnicodeDecodeError:
print(
'\nPlease try passing this parameter to the read_file\n\n self.read_file(filename, encoding_f=\'latin-1\')\n\n If that doesnt work , please choose encoders from the csv docs of pypi and pass them in...')
return False
except:
print(
'\nPlease check your filename, make sure it is in the root folder of the program !!')
return False
except:
print(
'\nPlease check your filename, make sure it is in the root folder of the program !!')
return False
class Mailer(Certificates):
def __init__(self):
self.username = ''
self.password = ''
self.subject = ''
self.body = ''
Certificates.__init__(self)
def send_mail(self):
if(self.username and self.password and self.subject and self.body):
self._send_mails(self.username, self.password, self.subject, self.body)
print('\nCompleted Sending all Mails !!')
return
print('\nSome Error has occured')
return