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
|
from mammoth.docx.xmlparser import element as xml_element
from mammoth.docx.styles_xml import read_styles_xml_element
from ..testing import assert_equal
def test_paragraph_style_is_null_if_no_style_with_that_id_exists():
element = xml_element("w:styles")
styles = read_styles_xml_element(element)
assert_equal(None, styles.find_paragraph_style_by_id("Heading1"))
def test_paragraph_style_can_be_found_by_id():
element = xml_element("w:styles", {}, [
_paragraph_style_element("Heading1", "Heading 1"),
])
styles = read_styles_xml_element(element)
assert_equal(
"Heading1",
styles.find_paragraph_style_by_id("Heading1").style_id
)
def test_character_style_can_be_found_by_id():
element = xml_element("w:styles", {}, [
_character_style_element("Heading1Char", "Heading 1 Char"),
])
styles = read_styles_xml_element(element)
assert_equal(
"Heading1Char",
styles.find_character_style_by_id("Heading1Char").style_id
)
def test_table_style_can_be_found_by_id():
element = xml_element("w:styles", {}, [
_table_style_element("TableNormal", "Normal Table"),
])
styles = read_styles_xml_element(element)
assert_equal(
"TableNormal",
styles.find_table_style_by_id("TableNormal").style_id
)
def test_paragraph_and_character_styles_are_distinct():
element = xml_element("w:styles", {}, [
_paragraph_style_element("Heading1", "Heading 1"),
_character_style_element("Heading1Char", "Heading 1 Char"),
])
styles = read_styles_xml_element(element)
assert_equal(None, styles.find_character_style_by_id("Heading1"))
assert_equal(None, styles.find_paragraph_style_by_id("Heading1Char"))
def test_styles_include_names():
element = xml_element("w:styles", {}, [
_paragraph_style_element("Heading1", "Heading 1"),
])
styles = read_styles_xml_element(element)
assert_equal(
"Heading 1",
styles.find_paragraph_style_by_id("Heading1").name
)
def test_style_name_is_none_if_name_element_does_not_exist():
element = xml_element("w:styles", {}, [
_style_without_name_element("paragraph", "Heading1"),
_style_without_name_element("character", "Heading1Char")
])
styles = read_styles_xml_element(element)
assert_equal(None, styles.find_paragraph_style_by_id("Heading1").name)
assert_equal(None, styles.find_character_style_by_id("Heading1Char").name)
def test_numbering_style_is_none_if_no_style_with_that_id_exists():
element = xml_element("w:styles", {}, [])
styles = read_styles_xml_element(element)
assert_equal(None, styles.find_numbering_style_by_id("List1"))
def test_numbering_style_has_none_num_id_if_style_has_no_paragraph_properties():
element = xml_element("w:styles", {}, [
xml_element("w:style", {"w:type": "numbering", "w:styleId": "List1"}),
])
styles = read_styles_xml_element(element)
assert_equal(None, styles.find_numbering_style_by_id("List1").num_id)
def test_numbering_style_has_num_id_read_from_paragraph_properties():
element = xml_element("w:styles", {}, [
xml_element("w:style", {"w:type": "numbering", "w:styleId": "List1"}, [
xml_element("w:pPr", {}, [
xml_element("w:numPr", {}, [
xml_element("w:numId", {"w:val": "42"})
]),
]),
]),
])
styles = read_styles_xml_element(element)
assert_equal("42", styles.find_numbering_style_by_id("List1").num_id)
def _paragraph_style_element(style_id, name):
return _style_element("paragraph", style_id, name)
def _character_style_element(style_id, name):
return _style_element("character", style_id, name)
def _table_style_element(style_id, name):
return _style_element("table", style_id, name)
def _style_element(element_type, style_id, name):
children = [xml_element("w:name", {"w:val": name}, [])]
return _style_element_with_children(element_type, style_id, children)
def _style_without_name_element(element_type, style_id):
return _style_element_with_children(element_type, style_id, [])
def _style_element_with_children(element_type, style_id, children):
attributes = {"w:type": element_type, "w:styleId": style_id}
return xml_element("w:style", attributes, children)
|