/
fill_table.py
199 lines (185 loc) · 6.76 KB
/
fill_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
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
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
from xpinyin import Pinyin
def get_tr_width(tr):
result = 0
for td in tr.find_all('td'):
if td.get('colspan'):
colspan = int(td.get('colspan'))
else:
colspan = 1
result += colspan
return result
def get_title_pinyin_td(td):
py = Pinyin()
result = get_title_td(td)
result = py.get_init(result)
return result
def is_title_td(td):
if not td: return False
for child in td.contents:
if callable(child.find) and child != '\n' and child.find('签名') == -1:
return True
return False
def get_title_td(td):
for child in td.contents:
if callable(child.split) and child != '\n':
result = ''.join(child.replace('\n','').split('(')[0].split('(')[0].split()) #print(''.join(child.replace('\n','').split('(')[0].split()))
return result
def is_title_tr(tr):
num_title = 0
num_td = len(list(tr.find_all('td')))
for td in tr.find_all('td'):
if is_title_td(td):
num_title += 1
if num_title == num_td > 1:
return True
return False
def is_cupple_row_tr(tr):
num_title = 0
num_td = len(list(tr.find_all('td')))
flag = 1
for td in tr.find_all('td'):
if is_title_td(td):
if flag >1:
return True
flag = 1
if not is_title_td(td):
flag += 1
return False
def is_unfill_row_tr(tr, tlist):
flag = 1
if get_tr_width(tr) != tlist['width']:
return False
for td in tr.find_all('td'):
if is_title_td(td):
flag = 1
else:
flag += 1
if flag > 2:
return True
return False
def get_title(tr):
#tr is the first tr
t = {'height':0,'list':[],'width':0}
for td in tr.find_all('td'):
if td.get('colspan'):
colspan = int(td.get('colspan'))
else:
colspan = 1
if td.get('rowspan'):
rowspan = int(td.get('rowspan'))
else:
rowspan = 1
t['width'] += colspan
if (t['height'] < rowspan):
t['height'] = rowspan
for i in range(colspan):
t['list'].append({'title':get_title_pinyin_td(td),'idle': t['height']-rowspan})
for i in range(t['height']-1):
tr = tr.next_sibling.next_sibling
if is_title_tr(tr):
for td in tr.find_all('td'):
for item in t['list']:
if item['idle'] >= t['height']-1-i:
index = t['list'].index(item)
break
if td.get('colspan'):
colspan = int(td.get('colspan'))
else:
colspan = 1
if td.get('rowspan'):
rowspan = int(td.get('rowspan'))
else:
rowspan = 1
for j in range(colspan):
t['list'][j+index]['title'] += '_'+get_title_pinyin_td(td)
t['list'][j+index]['idle'] -=rowspan
#print(t)
return t
def fill_tr(tr, title, i):
width = 0
n = 0
for td in tr.find_all('td'):
if td.get('colspan'):
colspan = int(td.get('colspan'))
else:
colspan = 1
if not is_title_td(td):
if n ==0:
try:
if title['list'][width]['title'] == title['list'][width+colspan]['title']:
n += 1
else:
n = 0
except:
pass
else:
tp = td.find_previous_sibling('td')
if tp.get('colspan'):
pcolspan = int(tp.get('colspan'))
else:
pcolspan = 1
if title['list'][width]['title'] == title['list'][width-pcolspan]['title']:
n += 1
else:
n = 0
tag_id = title['list'][width]['title']
if tag_id[-1].isdigit():
tag_id += '_'
tag_id += str(i)
new_tag = BeautifulSoup().new_tag('input',id=tag_id)
if n > 0:
new_tag['id'] += '_'+str(n)
new_tag['value'] = new_tag['id']
if td.input:
td.input.replace_with(new_tag)
else:
td.append(new_tag)
width += colspan
def fill_cupple_tr(tr):
for td in tr.find_all('td'):
if is_title_td(td):
k = td.next_sibling.next_sibling
if k and (not is_title_td(k)):
if get_title_td(td).find('意见') != -1:
new_tag = BeautifulSoup().new_tag('textarea',id=get_title_pinyin_td(td))
new_tag['rows'] = 20
new_tag.clear()
else:
new_tag = BeautifulSoup().new_tag('input',id=get_title_pinyin_td(td))
new_tag['value'] = new_tag['id']
if k.input:
k.input.replace_with(new_tag)
else:
k.append(new_tag)
def fill_table(tb):
have_title = False
for tr in tb.find_all('tr'):
if is_title_tr(tr) and len(list(tr.find_all('td'))) > 2:
title = get_title(tr)
have_title = True
break
i = 1
for tr in tb.find_all('tr'):
if have_title and is_unfill_row_tr(tr,title):
fill_tr(tr, title, i)
i += 1
elif not (is_title_tr(tr)):
fill_cupple_tr(tr)
return have_title
def fill_file(filename,input_dir='input',output_dir='output'):
soup = BeautifulSoup(open(input_dir+'/'+filename,encoding='utf-8'),from_encoding='utf-8')
#print(soup)
result = True
for bodytb in soup.find_all(class_='bodyTb'):
try:
result = fill_table(bodytb)
#print(filename,"\tOK")
except (UnboundLocalError,AttributeError):
result = False
break
out = open(output_dir+'/'+filename,'w',encoding='utf-8')
out.write(soup.prettify())
out.close()
return result