Драйверы торгового оборудования v.9
html_to_tables.py
1 # -*- coding: utf-8 -*-
2 
3 import xml.dom.minidom as md
4 import codecs
5 import xml.etree.ElementTree as ET
6 
7 doc = md.parse('tables.html')
8 tables = doc.getElementsByTagName('table')
9 cvt = tables[:2]
10 cvtr = [[], []]
11 
12 for T in xrange(0, len(cvt)):
13  values = []
14  trs = cvt[T].getElementsByTagName('tr')
15  for tr in trs:
16  row = []
17  tds = tr.getElementsByTagName('td')
18  for td in tds:
19  text = []
20  for p in td.childNodes:
21  if p.nodeType == p.TEXT_NODE:
22  text.append('')
23  else:
24  tree = ET.fromstring(p.toxml().encode('utf-8'))
25  text.append(''.join(tree.itertext()))
26  row.append('<br>'.join(text))
27  values.append(row)
28 
29  for row in values:
30  static = row[:2]
31  models = row[2:]
32 
33  i = 0
34  for model in models:
35  if len(cvtr[T]) <= i:
36  cvtr[T].append([])
37  i += 1
38  if not model:
39  continue
40 
41  cvtr[T][i - 1].append([static[0], static[1], model])
42 
43 name = ''
44 for table in cvtr[1]:
45  if not table:
46  continue
47  text = u"/*!\n"
48  name = table[0][2]
49  table[0][2] = u'Возможные значения'
50  text += u"\page fptrValues{} {}\n".format(table[1][2], name)
51  text += u"<table>\n"
52 
53  text += u"\t<tr>\n"
54  for col in table[0]:
55  text += u"\t\t<th>" + col + u"</th>\n"
56  text += u"\t</tr>\n"
57 
58  for row in table[2:]:
59  text += u"\t<tr>\n"
60  for col in row:
61  text += u"\t\t<td>" + col + u"</td>\n"
62  text += u"\t</tr>\n"
63 
64  text += u"</table>\n\n"
65 
66  name = name.replace('/', '')
67  with codecs.open(name + u'.dox', 'w', encoding='utf-8') as f:
68  f.write(text)
69  f.flush()
70 
71 for table in cvtr[0]:
72  if not table:
73  continue
74  name = table[0][2]
75  table[0][2] = u'Возможные значения'
76  text = u"\page fptrCaptions{} {}\n".format(table[1][2], name)
77  text += u"<table>\n"
78 
79  text += u"\t<tr>\n"
80  for col in table[0]:
81  text += u"\t\t<th>" + col + u"</th>\n"
82  text += u"\t</tr>\n"
83 
84  for row in table[2:]:
85  text += u"\t<tr>\n"
86  for col in row:
87  text += u"\t\t<td>" + col + u"</td>\n"
88  text += u"\t</tr>\n"
89 
90  text += u"</table>\n"
91 
92  text += u"*/"
93 
94  name = name.replace('/', '')
95  with codecs.open(name + u'.dox', 'a+', encoding='utf-8') as f:
96  f.write(text)
97  f.flush()