-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
96 lines (75 loc) · 2.77 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
from helium import *
from parentvue import student, navigation
from openpyxl import Workbook, load_workbook
from os import path
def xlsx_worksheet_name(sheetname):
# sheet name less than 35 characters
sheetname = sheetname[:35]
# sheet name cannot contain [ ] : * ? / \
for ch in ['[', ']', ':', '?', '/', '\\']:
if ch in sheetname:
sheetname = sheetname.replace(ch, "-")
# sheet name cannot begin or end with an apostrophe
sheetname = sheetname.strip('\'')
return sheetname
main_url = 'https://synergyweb.pusd11.net/'
# login_url = 'https://synergyweb.pusd11.net/PXP2_Login_Parent.aspx'
username = ''
password = ''
ws_columnname = 'Status'
# start browser and login
navigation.login(main_url, username, password)
# gather initial student data
students = student.get_students()
# gather class schedule and grades
for student in students:
# workbook
dest_filename = f'{student.name}.xlsx'
if path.exists(dest_filename):
# append to existing workbook (if exists)
wb = load_workbook(filename=dest_filename)
else:
# new workbook (remove default sheet)
wb = Workbook()
wb.remove(wb.active)
# gather schedule data
student.get_classlist()
# gather grade book data
student.get_grades()
# loop through classes
for key, rows in student.grades.items():
# worksheet for each class
sheetname = f'{xlsx_worksheet_name(key)}'
try:
# append to existing worksheet (if exists)
ws = wb[sheetname]
except KeyError:
# create new worksheet
ws = wb.create_sheet(sheetname)
# add header row
ws.append(rows[0])
ws.cell(row=1, column=ws.max_column + 1, value=ws_columnname)
# append data to worksheet
for row in rows:
# check for existing rows
for idx, sheetrow in enumerate(ws.iter_rows(values_only=True), start=1):
sheetrow = tuple(str(i or '') for i in sheetrow)
# check for duplicate rows (except last ws_columnname column)
if row == sheetrow[0:len(sheetrow)-1]:
# duplicate row
break
# check for matching rows
if row[0:4] == sheetrow[0:4]:
# matching row found, update existing values
try:
for col, val in enumerate(row, start=1):
ws.cell(row=idx, column=col).value = val
except Exception as e:
print(f"Exception: {e}")
break
else:
# new row
ws.append(row)
# save workbook
wb.save(filename=dest_filename)
kill_browser()