-
Notifications
You must be signed in to change notification settings - Fork 3
/
abst_table.py
75 lines (67 loc) · 2.32 KB
/
abst_table.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
import csv
from table import Table
from jinja2 import Template
class AbstTable(Table):
def __init__(self, source_file):
with open(source_file, newline='') as f:
self.timetable_sheet = list(csv.reader(f))
self.row_structure = [
[9], # 氏名
[12, 13], # タイトル, 発表区分
[14] # 発表詳細
]
self.index_name = {
9: 'name',
12: 'title',
13: 'type',
14: 'detail'
}
self.row_looks = [
Template('{{ name }}'),
Template('{{ title }}<br>[{{ type }}]'),
Template('{{ detail }}')
]
self.row_tag = [
# header
[
Template('<th class="name">{{ c }}</th>'),
Template('<th class="title">{{ c }}</th>'),
Template('<th class="detail right_edge">{{ c }}</th>')
],
# body
[
Template('<td class="name">{{ c }}</td>'),
Template('<td class="title">{{ c }}</td>'),
Template('<td class="detail right_edge">{{ c }}</td>')
]
]
def gen_rows(self, content):
rows = []
for row in content:
if row[self.row_structure[0][0]] == '':
continue
tmp = []
for col, idxs in enumerate(self.row_structure):
context = {}
for i in idxs:
context[self.index_name[i]] = row[i]
tmp.append(self.row_looks[col].render(context))
rows.append(tmp)
return rows
def gen_tables(self):
[headings, contents] = self.divide_sheet(self.timetable_sheet)
tables = []
for content in contents:
rows = self.gen_rows(content)
rows.insert(0, ['氏名', 'タイトル', '概要'])
table = []
for i, row in enumerate(rows):
tmp_row = []
for j, cell in enumerate(row):
if i <= 1:
tmp_row.append(self.row_tag[i][j].render(c=cell))
else:
tmp_row.append(self.row_tag[1][j].render(c=cell))
table.append(tmp_row)
tables.append(table)
return [headings, tables]