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
|
import collections
class Styles(object):
def __init__(self, paragraph_styles, character_styles, table_styles):
self._paragraph_styles = paragraph_styles
self._character_styles = character_styles
self._table_styles = table_styles
def find_paragraph_style_by_id(self, style_id):
return self._paragraph_styles.get(style_id)
def find_character_style_by_id(self, style_id):
return self._character_styles.get(style_id)
def find_table_style_by_id(self, style_id):
return self._table_styles.get(style_id)
Styles.EMPTY = Styles(
paragraph_styles={},
character_styles={},
table_styles={},
)
def read_styles_xml_element(element):
paragraph_styles = {}
character_styles = {}
table_styles = {}
styles = {
"paragraph": paragraph_styles,
"character": character_styles,
"table": table_styles,
}
for style_element in element.find_children("w:style"):
style = _read_style_element(style_element)
element_type = style_element.attributes["w:type"]
style_set = styles.get(element_type)
if style_set is not None:
style_set[style.style_id] = style
return Styles(
paragraph_styles=paragraph_styles,
character_styles=character_styles,
table_styles=table_styles,
)
def _read_style_element(element):
style_id = element.attributes["w:styleId"]
name = element.find_child_or_null("w:name").attributes.get("w:val")
return Style(style_id=style_id, name=name)
Style = collections.namedtuple("Style", ["style_id", "name"])
|