-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathworddoc.py
259 lines (231 loc) · 6.61 KB
/
worddoc.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
#Copyright (C) 2017 Nihal Rao I, Sanjan S Poojari, Shishir Upadhya
'''
xml paths:
section timetable:
1st table:
t = root[0][6]
r1 = t[3]
u11 = r1[1]
u11_text = c11[1][1][1].text
u12 = r1[2] etc
r2 = t[4] etc
2nd table:
t = root[0][10]
regular subjects:
r1 = t[3]
c11 = r1[1]
c11_text = c11[1][1][1].text
lab subjects:
row = t[5]
l1: r1[1]
l111: r1[2]
l112: r1[3]
l121: t[6][2]
l122: t[6][3]
section:
root[0][3][16][1].text - replace 's00'
year:
root[0][3][3][1].text
room no: root[0][4][3][1].text
dept long: root[0][1][1][1].text
dept short: root[0][4][17][1].text - replace 'CSE'
personal timetable:
staff name: root[0][5][2][1].text
room no:
r1: root[0][8]- p
root[0][8][4][1].text
table: t = root[0][9]
r1 = t[3]
c11 = r1[2]
teaching workload:
w1: root[0][27][2][1].text - replace
w2: root[0][28][5][1].text
w3: root[0][29][3][1].text
dept- root[0][4][2][1].text - replace
designation- root[0][6][1][1].text - append
'''
import xml.etree.ElementTree as ET
import zipfile
import os
import copy
day_row_num = {
'monday': 0,
'tuesday': 1,
'wednesday': 2,
'thursday': 3,
'friday': 4,
'saturday': 5
}
dept_short = { 'Biotechnology': 'BT',
'Civil Engineering': 'CV',
'Computer Science & Engineering': 'CSE',
'Electronics & Communications Engineering': 'ECE',
'Electrical & Electronics Engineering': 'EE',
'Information Science & Engineering': 'ISE',
'Mechanical Engineering': 'ME',
'': '' }
def print_faculty_wordxml(tt, designation, faculty_subjects, subs, sections):
tree = ET.parse('template2.xml')
root = tree.getroot()
root[0][5][2][1].text = tt.name # name
root[0][4][2][1].text = root[0][4][2][1].text.replace('CSE', dept_short[tt.dept]) # branch
root[0][6][1][1].text += ' ' + designation
t = root[0][9] # table
w1 = root[0][27][2][1] # teaching workloads
w2 = root[0][28][5][1]
w3 = root[0][29][3][1]
reg_subs = []
for f_s in faculty_subjects:
sub, short_sub, sec = f_s.split(' - ')
sub = subs[short_sub]
if sub.lab == False:
reg_subs.append((short_sub, sec))
if len(reg_subs) > 0:
r = root[0][8]
sem, sec = reg_subs[0][1].split(' ')
r[4][1].text = '{} - {} - {}'.format(reg_subs[0][0], reg_subs[0][1], sections[sem][sec].roomno)
next_sub = 9
for short_sub, sec in reg_subs[1:]:
p = copy.deepcopy(r)
sem, s = sec.split(' ')
p[4][1].text = '{} - {} - {}'.format(short_sub, sec, sections[sem][s].roomno)
root[0].insert(next_sub, p)
theory_units = 0
lab_units = 0
for day in tt:
for timeslot in tt[day]:
sub = tt[day][timeslot]
if sub == '':
sub = ' '
else:
sub = tt[day][timeslot][1][3]
if subs[sub].lab == True:
lab_units += 1
else:
theory_units += 2
r = day_row_num[day] + 3
c = timeslot + 1
t[r][c][1][1][1].text = sub
w1.text = w1.text.replace('w1', str(theory_units + lab_units))
w2.text = str(theory_units)
w3.text = str(lab_units)
return ET.tostring(root)
def print_section_wordxml(tt, subjects_assigned, subs, year, faculty):
tree = ET.parse('template.xml')
root = tree.getroot()
t1 = root[0][6]
for day in tt:
for timeslot in tt[day]:
sub = tt[day][timeslot]
if sub == '':
sub = ' '
else:
sub = sub[3]
r = day_row_num[day] +3
c = timeslot +1
t1[r][c][1][1][1].text = sub
root[0][3][16][1].text = root[0][3][16][1].text.replace('s00', tt.name) # section
root[0][1][1][1].text = 'Department of ' + tt.dept # department - long
branch = root[0][4][17][1].text # department - short
branch = branch.replace('CSE', dept_short[tt.dept])
root[0][4][17][1].text = branch
root[0][3][3][1].text = year # year
root[0][4][3][1].text = tt.roomno # room number
regular_subs = []
labs = []
for fac in subjects_assigned:
sub_name, short_sub, fac = fac.split(' - ')
sub = subs[short_sub]
if sub.lab == True:
lab = sub_name.split('|')
fac = fac.split(', ')
for i, lname in enumerate(lab):
if lname.endswith(' Lab') == False:
lname = lname + ' Lab'
initials = ''.join(map(lambda x: x[0].upper(), fac[i].split(' ')))
labs.append((lname, initials))
else:
regular_subs.append((sub_name, sub.subcode, str(faculty[faculty.index(fac)])))
t2 = root[0][10]
r = t2[3]
labrow1 = t2[5]
labrow2 = t2[6]
r[1][1][1][1].text = regular_subs[0][1] # subject code
r[2][1][1][1].text = regular_subs[0][0] # sub expansion
r[3][1][1][1].text = regular_subs[0][2] # faculty
t2_nextrow = 4
for sub_name, subcode, faculty in regular_subs[1:]:
r = copy.deepcopy(t2[3])
r[1][1][1][1].text = subcode
r[2][1][1][1].text = sub_name
r[3][1][1][1].text = faculty
t2.insert(t2_nextrow, r)
t2_nextrow += 1
if len(labs) > 0:
r = labrow1
sec = tt.name.split(' ')[1]
r[1][1][1][1].text = labs[0][0]
r[2][1][1][1].text = sec + '1'
r[3][1][1][1].text = labs[0][1]
r = labrow2
r[2][1][1][1].text = sec + '2'
r[3][1][1][1].text = labs[0][1]
for labname, initials in labs[1:]:
r = copy.deepcopy(labrow1)
r[1][1][1][1].text = labname
r[2][1][1][1].text = sec + '1'
r[3][1][1][1].text = initials
t2.append(r)
r = copy.deepcopy(labrow2)
r[2][1][1][1].text = sec + '2'
r[3][1][1][1].text = initials
t2.append(r)
return ET.tostring(root)
def print_freeslots_wordxml(rooms, department):
'''
department: root[0][6][1][1].text
table: root[0][10]
2nd row: root[0][10][3]
2nd column: root[0][10][3][3]
2nd column text: root[0][10][3][3][1][2][1].text
'''
tree = ET.parse('template_freerooms.xml')
root = tree.getroot()
root[0][6][1][1].text = 'Department of ' + department
table = root[0][10]
for day in rooms:
r = day_row_num[day] + 3
row = table[r]
for timeslot in rooms[day]:
c = timeslot + 1
#print(r, c)
col = row[c][1][1][1]
freerooms = '\n'.join(rooms[day][timeslot])
col.text = freerooms
return ET.tostring(root)
def make_docx(tt, tt_type, filename, subjects_assigned = None, subs = None, dicts = None, year = None):
if tt_type == 'section':
xml_string = print_section_wordxml(tt, subjects_assigned, subs, year, dicts)
elif tt_type == 'faculty':
xml_string = print_faculty_wordxml(tt, year, subjects_assigned, subs, dicts)
else:
xml_string = print_freeslots_wordxml(tt, subjects_assigned)
with open(os.path.join('template', 'word', 'document.xml'), 'wb') as f:
f.write(xml_string)
file_paths = []
os.chdir('template')
for file in os.listdir():
if os.path.isdir(file):
for root, dirs, files in os.walk(file):
for file in files:
file_paths.append(os.path.join(root, file))
else:
file_paths.append(file)
try:
with zipfile.ZipFile(os.path.join('..', filename), 'w') as zf:
for file in file_paths:
zf.write(file)
except: # reraise the exception
raise
finally: # always change dir back
os.chdir('..')