-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetSchedulePic.py
executable file
·111 lines (97 loc) · 3.45 KB
/
GetSchedulePic.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
import os
import xlrd
import pdfscheduler
from datetime import datetime
# import pdf2image
from reportlab.lib.colors import black
import fitz
import web_api as wapi
def convertTime(time):
miltime = ""
if time[-2:] == "am":
if time[:2] == "12":
miltime = str('00' + time[2:8])
else:
miltime = time[:-2]
else:
if time[:2] == "12":
miltime = time[:-2]
else:
miltime = str(
int(time.split(':')[0]) + 12) + ":" + time.split(":")[1][0:-2]
return miltime
def convertDays(days):
newDays = []
for i in range(0, len(days)):
if (days[i] == "M"):
newDays.append("Monday")
elif (days[i] == "T"):
newDays.append("Tuesday")
elif (days[i] == "W"):
newDays.append("Wednesday")
elif (days[i] == "R"):
newDays.append("Thursday")
elif (days[i] == "F"):
newDays.append("Friday")
return newDays
def GetSchedulePic(semesterNew, userID, planID, planName):
path = "./Users/" + userID + "/" + planID + "/" + semesterNew + " Info.xls"
try:
book = xlrd.open_workbook(path)
sheet = book.sheet_by_name(planName)
except:
return False
schedu = pdfscheduler.Schedule(
["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"])
colors = [[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 0], [0, 1, 1], [1, 0, 1], [1, 0.5, 0.5], [1, 0.5, 0], [0.5, 1, 0], [0.5, 0.5, 1], [
0.5, 0, 1], [0.5, 1, 0.5], [0, 1, 0.5], [0, 0.5, 1], [1, 0.5, 1], [1, 0, 0.5], [0, 0.5, 0], [0.5, 0, 0], [0, 0, 0.5], [0.5, 0.5, 0.5]]
for i in range(1, sheet.nrows):
times = sheet.cell_value(i, 5).split(",")
for time in times:
out = time.split()
out2 = out[2].split('-')
schedu.add_event(pdfscheduler.Event(datetime.strptime(out[1] + out2[0], '%I:%M%p').time(), datetime.strptime(out2[1] + out[3], '%I:%M%p').time(), [sheet.cell_value(
i, 9), sheet.cell_value(i, 3) + " " + convertTime(out[1] + out2[0]) + "-" + convertTime(out2[1] + out[3]), sheet.cell_value(i, 0)], colors[i], convertDays(out[0])))
path = "./static/Users/" + userID + "/" + planID + "/"
outfile_pdf = "./static/Users/" + userID + "/" + planID + "/schedule.pdf"
outfile_img = "./static/Users/" + userID + "/" + planID + "/schedule.png"
wapi.checkFolder(path)
c = pdfscheduler.Canvas(outfile_pdf, (800, 600))
# c.setFillColorRGB(1, 1, 1) # set background color to white
# c.setStrokeColorRGB(1, 1, 1) # set border color to white
c.setFont("Helvetica", 20)
pwidth = 800
pheight = 600
inch = 20
schedu.render(
c,
x=inch,
y=pheight - inch,
width=pwidth - 2 * inch,
height=pheight - 2 * inch,
font_size=14,
show_times=True
)
try:
os.remove(outfile_pdf)
os.remove(outfile_img)
except:
pass
c.setFillColor(black)
c.save()
doc = fitz.open(outfile_pdf)
for page in doc:
pix = page.get_pixmap(alpha=True)
pix.save(outfile_img)
return True
if __name__ == "__main__":
# semesterNew = "2022-SPRG" # Fall:"FALL", Summer:"SUMM", Spring:"SPRG"
# username = "Sam2"
# planName = "Plan 1"
# GetSchedulePic(semesterNew, username, planName)
userID = '1'
planID = '1'
planName = 'Plan 1'
semester = '2023-FALL'
GetSchedulePic(semester, userID, planID, planName)
# pass