forked from python-openxml/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.py
More file actions
103 lines (70 loc) · 2.73 KB
/
text.py
File metadata and controls
103 lines (70 loc) · 2.73 KB
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
# encoding: utf-8
"""
Step implementations for text-related features
"""
from __future__ import absolute_import, print_function, unicode_literals
from behave import given, then, when
from docx import Document
from docx.enum.text import WD_BREAK
from docx.oxml.shared import qn
# given ===================================================
@given('a run')
def given_a_run(context):
p = Document().add_paragraph()
context.run = p.add_run()
@given('a run having {bool_prop_name} set on')
def given_a_run_having_bool_prop_set_on(context, bool_prop_name):
run = Document().add_paragraph().add_run()
setattr(run, bool_prop_name, True)
context.run = run
# when ====================================================
@when('I add a column break')
def when_add_column_break(context):
run = context.run
run.add_break(WD_BREAK.COLUMN)
@when('I add a line break')
def when_add_line_break(context):
run = context.run
run.add_break()
@when('I add a page break')
def when_add_page_break(context):
run = context.run
run.add_break(WD_BREAK.PAGE)
@when('I assign {value_str} to its {bool_prop_name} property')
def when_assign_true_to_bool_run_prop(context, value_str, bool_prop_name):
value = {'True': True, 'False': False, 'None': None}[value_str]
run = context.run
setattr(run, bool_prop_name, value)
# then =====================================================
@then('it is a column break')
def then_type_is_column_break(context):
attrib = context.last_child.attrib
assert attrib == {qn('w:type'): 'column'}
@then('it is a line break')
def then_type_is_line_break(context):
attrib = context.last_child.attrib
assert attrib == {}
@then('it is a page break')
def then_type_is_page_break(context):
attrib = context.last_child.attrib
assert attrib == {qn('w:type'): 'page'}
@then('the last item in the run is a break')
def then_last_item_in_run_is_a_break(context):
run = context.run
context.last_child = run._r[-1]
expected_tag = (
'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}br'
)
assert context.last_child.tag == expected_tag
@then('the run appears in {boolean_prop_name} unconditionally')
def then_run_appears_in_boolean_prop_name(context, boolean_prop_name):
run = context.run
assert getattr(run, boolean_prop_name) is True
@then('the run appears with its inherited {boolean_prop_name} setting')
def then_run_inherits_bool_prop_value(context, boolean_prop_name):
run = context.run
assert getattr(run, boolean_prop_name) is None
@then('the run appears without {boolean_prop_name} unconditionally')
def then_run_appears_without_bool_prop(context, boolean_prop_name):
run = context.run
assert getattr(run, boolean_prop_name) is False