-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.py
453 lines (249 loc) · 10.3 KB
/
extract.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from dotenv import load_dotenv
import pandas as pd
import requests
import openpyxl
import shutil
import sys
import os
load_dotenv()
#function to create folder
def createFolder(nameOfFolder):
if not os.path.exists(nameOfFolder):
os.mkdir(nameOfFolder)
folderName = nameOfFolder
return folderName
while True:
confirmAction = input("Folder name ({}) already exist, do you want to overwrite [y/n]: ".format(nameOfFolder))
if confirmAction == 'y' or confirmAction == 'Y':
shutil.rmtree(nameOfFolder)
os.mkdir(nameOfFolder)
folderName = nameOfFolder
break
elif confirmAction == 'n' or confirmAction == 'N':
newdir = input("Input new folder name: ")
folderName = createFolder(newdir)
break
else:
print('\nWrong value entered')
return folderName
# function to send message via telegram bot
def sendToTelegram(document, caption):
apiToken = os.getenv('botToken')
chatID = os.getenv('chatID')
payload = {'chat_id': chatID, 'caption': caption}
apiURL = f'https://api.telegram.org/bot{apiToken}/sendDocument'
try:
with open(document, 'rb') as file:
fileContent = file.read()
if not fileContent:
apiURL = f'https://api.telegram.org/bot{apiToken}/sendMessage'
if document == className + ' termination List.txt':
text = "No student in {} is due for termination".format(className)
else:
text = "No student in {} is due for probation".format(className)
payload = {'chat_id': chatID, 'text': text}
response = requests.post(apiURL, data=payload)
if response.status_code == 200:
print("Message sent successfully!")
else:
print(f"Failed to send message: {response.text}")
return
files = {'document': (os.path.basename(document), fileContent)}
response = requests.post(apiURL, data=payload, files=files)
if response.status_code == 200:
print("Document sent successfully!")
else:
print(f"Failed to send document: {response.text}")
except Exception as e:
print(e)
#specify result sheet
resultSheet = "emyety.xlsx"
# allow user to select programe name
print ("1. EMY \n2. ETY")
try:
programe = int(input("Kindly input which programe to generate result for: "))
if programe != 1 and programe != 2:
print('wrong value entered')
os.execv(sys.executable, ['python'] + sys.argv)
if programe == 1:
programe = 'EMY'
resultTemplate = 'emyTemplate.xlsx'
else:
programe = "ETY"
resultTemplate = 'etyTemplate.xlsx'
except ValueError:
print('Please select from the above option')
os.execv(sys.executable, ['python'] + sys.argv)
# allow user to select class number
while True:
try:
classNumber = int(input("Please input class number only, excluding 'C': "))
className = programe+"-C"+str(classNumber)
resultDb = openpyxl.load_workbook(resultSheet)
classResult = resultDb.sheetnames
if className not in classResult:
print("\n" + className + " does not exist in excel database\nKindly provide correct class details or update excel database\n")
os.execv(sys.executable, ['python'] + sys.argv)
while True:
confirmSelection = input("Do you want to generate result sheet for every student in "+ className + " (Y/N): ")
if confirmSelection == "y" or confirmSelection == "Y":
folder = className + " individual compiled result"
path = createFolder(folder)
break
elif confirmSelection == "n" or confirmSelection == "N":
os.execv(sys.executable, ['python'] + sys.argv)
else:
print ("wrong value entered")
break
except ValueError:
print("Please input a number\n")
# Input current number of courses
# selected class that code is working on
currentClass = resultDb[className]
# count number of courses
# Specify the row number to start the count
row_number_to_search = 3
courseCounted = 0
startColumn = 0
# get first column that course count begins
for cell in currentClass[row_number_to_search]:
cell_value = cell.value
if cell_value is not None:
startColumn = cell.column
break
# start count to know how many courses
for cell in currentClass.iter_cols(min_col=startColumn, min_row=row_number_to_search, max_row=row_number_to_search):
cell_value = cell[0].value
if cell_value != "AVERAGE":
courseCounted += 1
else:
totalCourse = courseCounted + 1 # including column for average(count start from index zero)
averageValue = courseCounted
break
# iteration to assign how many student are available while keeping max at 24
# max number of student in class
maxStudent = 24 # that is the maximum number of student in a class
startRow = 7 # from main db names majorly starts from column 7
cellCount = 0
startColumn = 1
numberOfStudent = 0
for cell in currentClass.iter_rows(min_col=startColumn, min_row=startRow):
cell_value = cell[0].value
if cell_value is not None and cellCount <= maxStudent:
numberOfStudent += 1
cellCount += 1
else:
break
#course index for db
courseRowDb = 5
courseColumnDb = 6
#course index for template
courseRowTemplate = 11
courseColumnTemplate = 3
# other variable
scoreList = []
currentCourseColumn = 11
trackFailure = 0
probation = []
termination = []
courseNameDb = []
courseNameTemplate = []
#load work sheet
template = openpyxl.load_workbook(resultTemplate)
templateWorksheet = template.worksheets[0]
data = pd.read_excel(resultSheet, sheet_name=className, skiprows=6, header=None, nrows=numberOfStudent) #skip rows is 6 because after that row student names which is the data we need starts counting
# extract all course name from db and template
for i in range(courseCounted):
dbCell = currentClass.cell(row=courseRowDb, column=courseColumnDb).value
courseNameDb.append(dbCell)
courseColumnDb += 1
for i in range(courseCounted):
templateCell = templateWorksheet.cell(row=courseRowTemplate, column=courseColumnTemplate).value
courseNameTemplate.append(templateCell)
courseRowTemplate += 1
# iterate through templateWorksheet and manipulate data
for _, row in data.iterrows():
lastName = row.iloc[1]
firstName = row.iloc[2]
middleName = row.iloc[3]
# Check if any of the names is NaN, if yes, set them to empty strings
if pd.isna(firstName):
firstName = ""
if pd.isna(middleName):
middleName = ""
# ommited last name(every individual would surely have a last name) to keep track of empty result that would be called nan
score = row.iloc[5:].values
score = pd.Series(score).fillna('NA').values
for scores in score[:totalCourse]:
scoreList.append(scores)
average = scoreList[averageValue]
# check for failures
for scores in scoreList[:averageValue]:
try:
if int(scores) < 60:
trackFailure += 1
except:
pass
# Create a dictionary to store the mapping of course names and their corresponding scores
course_score_mapping = dict(zip(courseNameDb, score[:totalCourse]))
# Iterate through the course names in the template
for coursesTemplate in courseNameTemplate:
# Check if the course name exists in the mapping
if coursesTemplate in course_score_mapping:
# Get the score from the mapping for the current course name
score_for_course = course_score_mapping[coursesTemplate]
else:
# If the course name is not found in the mapping, set the score to 'NA'
score_for_course = ' '
# Write the score to the corresponding cell in the template
templateWorksheet.cell(row=currentCourseColumn, column=4, value=score_for_course)
currentCourseColumn += 1
name = str(lastName) + " " + str(firstName) + " " + str(middleName)
templateWorksheet.cell(row=6, column=3, value=name)
templateWorksheet.cell(row=6, column=5, value=average)
templateWorksheet.cell(row=4, column=5, value=className)
# check number of failed courses
if trackFailure == 2:
probation.append(name)
if trackFailure > 2:
termination.append(name)
# clear variable memory
trackFailure = 0
scoreList = []
currentCourseColumn = 11
#save file
try:
template.save('{}/{}.xlsx'.format(path,name))
except:
pass
# loop to delete null report file created cause of incomplete class
for filename in os.listdir(path):
if "nan" in filename:
file_path = os.path.join(path, filename)
os.remove(file_path)
# create file to store probation and termination list
probationFile = open(className + " probation List.txt", "x")
terminationFile = open(className + " termination List.txt", "x")
# generate student on probation
for watchlist in probation:
probationFile.write(watchlist + '\n')
# generate student on termination
for watchlist in termination:
terminationFile.write(watchlist + '\n')
# Save and close files before sending
probationFile.close()
terminationFile.close()
# zip folder containing results
shutil.make_archive(className + " generated result", 'zip', path)
shutil.rmtree(path)
# send generated data to telegram
caption = "This is a TXT file containing list of student in {} due for probation".format(className)
sendToTelegram(className + " probation List.txt", caption)
caption = "This is a TXT file containing list of student in {} due for termination".format(className)
sendToTelegram(className + " termination List.txt", caption)
caption = "This is a zip file containing result for every student in {}".format(className)
sendToTelegram(className + " generated result.zip", caption)
#delete file after sending
os.remove(className + " generated result.zip")
os.remove(className + " termination List.txt")
os.remove(className + " probation List.txt")