-
Notifications
You must be signed in to change notification settings - Fork 704
Expand file tree
/
Copy pathtext.py
More file actions
272 lines (199 loc) · 8.88 KB
/
text.py
File metadata and controls
272 lines (199 loc) · 8.88 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"""Gherkin step implementations for text-related features."""
from __future__ import annotations
from behave import given, then, when
from helpers import test_pptx
from pptx import Presentation
from pptx.enum.text import MSO_NUMBERED_BULLET_STYLE, PP_ALIGN
from pptx.util import BulletStyle, Emu
# given ===================================================
@given("a _Paragraph object as paragraph")
def given_a_Paragraph_object_as_paragraph(context):
prs = Presentation(test_pptx("txt-text"))
context.paragraph = prs.slides[0].shapes[0].text_frame.paragraphs[0]
@given("a _Paragraph object containing {value} as paragraph")
def given_a_Paragraph_object_containing_value_as_paragraph(context, value):
prs = Presentation(test_pptx("txt-text"))
paragraph_idx = {"abc": 0, "a\vb\vc": 1}[eval(value)]
context.paragraph = prs.slides[0].shapes[1].text_frame.paragraphs[paragraph_idx]
@given("a paragraph having line spacing of {setting}")
def given_a_paragraph_having_line_spacing_of_setting(context, setting):
paragraph_idx = {"no explicit setting": 0, "1.5 lines": 1, "20 pt": 2}[setting]
prs = Presentation(test_pptx("txt-paragraph-spacing"))
text_frame = prs.slides[2].shapes[0].text_frame
context.paragraph = text_frame.paragraphs[paragraph_idx]
@given("a paragraph having space {before_after} of {setting}")
def given_a_paragraph_having_space_before_after_of_setting(context, before_after, setting):
slide_idx = {"before": 0, "after": 1}[before_after]
paragraph_idx = {"no explicit setting": 0, "6 pt": 1}[setting]
prs = Presentation(test_pptx("txt-paragraph-spacing"))
text_frame = prs.slides[slide_idx].shapes[0].text_frame
context.paragraph = text_frame.paragraphs[paragraph_idx]
@given("a _Run object as run")
def given_a_Run_object_as_run(context):
prs = Presentation(test_pptx("txt-text"))
context.run = prs.slides[0].shapes[0].text_frame.paragraphs[0].runs[0]
@given("a _Run object containing text as run")
def given_a_Run_object_containing_text_as_run(context):
prs = Presentation(test_pptx("txt-text"))
context.run = prs.slides[0].shapes[0].text_frame.paragraphs[0].runs[0]
@given("a text run")
def given_a_text_run(context):
prs = Presentation(test_pptx("txt-text"))
context.run = prs.slides[0].shapes[0].text_frame.paragraphs[0].runs[0]
@given("a text run in a table cell")
def given_a_text_run_in_a_table_cell(context):
prs = Presentation(test_pptx("txt-text"))
cell = prs.slides[1].shapes[0].table.cell(0, 0)
context.run = cell.text_frame.paragraphs[0].runs[0]
@given("a text run having a hyperlink")
def given_a_text_run_having_a_hyperlink(context):
prs = Presentation(test_pptx("txt-text"))
context.run = prs.slides[0].shapes[0].text_frame.paragraphs[1].runs[0]
# when ====================================================
@when("I assign a typeface name to the font")
def when_assign_typeface_name_to_font(context):
context.font.name = "Verdana"
@when("I assign None to hyperlink.address")
def when_assign_None_to_hyperlink_address(context):
context.run.hyperlink.address = None
@when("I assign paragraph.alignment = PP_ALIGN.CENTER")
def when_I_assign_paragraph_alignment_eq_center(context):
context.paragraph.alignment = PP_ALIGN.CENTER
@when("I assign paragraph.level = 1")
def when_I_assign_paragraph_leve_eq_1(context):
context.paragraph.level = 1
@when("I assign paragraph.text = {value}")
def when_I_assign_paragraph_text_eq_value(context, value):
context.paragraph.text = eval(value)
@when("I assign run.text = {value}")
def when_I_assign_run_text_eq_value(context, value):
context.run.text = eval(value)
@when("I assign {value_str} to paragraph.line_spacing")
def when_I_assign_value_to_paragraph_line_spacing(context, value_str):
value = {
"1.5": 1.5,
"2.0": 2.0,
"254000": Emu(254000),
"304800": Emu(304800),
"None": None,
}[value_str]
paragraph = context.paragraph
paragraph.line_spacing = value
@when("I assign {value_str} to paragraph.space_{before_after}")
def when_I_assign_value_to_paragraph_space_before_after(context, value_str, before_after):
value = {"76200": 76200, "38100": 38100, "None": None}[value_str]
attr_name = {"before": "space_before", "after": "space_after"}[before_after]
paragraph = context.paragraph
setattr(paragraph, attr_name, value)
@when("I set the hyperlink address")
def when_set_hyperlink_address(context):
context.run_text = "python-pptx @ GitHub"
context.address = "https://github.com/scanny/python-pptx"
run = context.run
run.text = context.run_text
hlink = run.hyperlink
hlink.address = context.address
@when("I assign paragraph.bullet = {value_str}")
def when_I_assign_value_to_paragraph_bullet(context, value_str):
value = {
"x": BulletStyle.custom("x"),
"Default": BulletStyle.DEFAULT,
"No bullet": BulletStyle.NO_BULLET,
"hindiAlphaPeriod": BulletStyle.numbered(MSO_NUMBERED_BULLET_STYLE.HINDI_ALPHA_PERIOD),
}[value_str]
paragraph = context.paragraph
paragraph.bullet = value
# then ====================================================
@then("paragraph.alignment == PP_ALIGN.CENTER")
def then_paragraph_alignment_eq_center(context):
actual, expected = context.paragraph.alignment, PP_ALIGN.CENTER
assert actual == expected, "paragraph.alignment == %s" % actual
@then("paragraph.level == 1")
def then_paragraph_level_eq_1(context):
actual, expected = context.paragraph.level, 1
assert actual == expected, "paragraph.level == %s" % actual
@then("paragraph.line_spacing is {value_str}")
def then_paragraph_line_spacing_is_value(context, value_str):
value = {
"None": None,
"1.0": 1.0,
"1.5": 1.5,
"2.0": 2.0,
"254000": 254000,
"304800": 304800,
}[value_str]
paragraph = context.paragraph
assert paragraph.line_spacing == value
@then("paragraph.line_spacing.pt {result}")
def then_paragraph_line_spacing_pt_result(context, result):
value, exception = {
"raises AttributeError": (None, AttributeError),
"is 20.0": (20.0, None),
"is 24.0": (24.0, None),
}[result]
line_spacing = context.paragraph.line_spacing
if value is not None:
assert line_spacing.pt == value
if exception is not None:
try:
line_spacing.pt
raise AssertionError("did not raise")
except exception:
pass
@then("paragraph.space_{before_after} is {value_str}")
def then_paragraph_space_before_is_value(context, before_after, value_str):
attr_name = {"before": "space_before", "after": "space_after"}[before_after]
value = None if value_str == "None" else int(value_str)
paragraph = context.paragraph
assert getattr(paragraph, attr_name) == value
@then("paragraph.space_{before_after}.pt {result}")
def then_paragraph_space_before_pt_is_value(context, before_after, result):
attr_name = {"before": "space_before", "after": "space_after"}[before_after]
value, exception = {
"raises AttributeError": (None, AttributeError),
"== 6.0": (6.0, None),
}[result]
space_before_after = getattr(context.paragraph, attr_name)
if value is not None:
assert space_before_after.pt == value
if exception is not None:
try:
space_before_after.pt
raise AssertionError("did not raise")
except exception:
pass
@then("paragraph.text == {value}")
def then_paragraph_text_eq_value(context, value):
actual, expected = context.paragraph.text, eval(value)
assert actual == expected, 'paragraph.text == "%s"' % actual
@then("paragraph.text matches the assigned string")
def then_paragraph_text_matches_the_assigned_string(context):
paragraph = context.paragraph
assert paragraph.text == " Boo Far \n Faz Foo "
@then("run.text == {value}")
def then_run_text_is_value(context, value):
actual, expected = context.run.text, eval(value)
assert actual == expected, "run.text == %s" % (actual,)
@then("run.text is a hyperlink")
def then_run_text_is_a_hyperlink(context):
run = context.run
hlink = run.hyperlink
assert run.text == context.run_text
assert hlink.address == context.address
@then("run.text is not a hyperlink")
def then_run_text_is_not_a_hyperlink(context):
hlink = context.run.hyperlink
assert hlink.address is None
@then("the font name matches the typeface I set")
def then_font_name_matches_typeface_I_set(context):
assert context.font.name == "Verdana"
@then("paragraph.bullet == {value}")
def then_paragraph_bullet_is_value(context, value):
expected = {
"x": BulletStyle.custom("x"),
"Default": BulletStyle.DEFAULT,
"No bullet": BulletStyle.NO_BULLET,
"hindiAlphaPeriod": BulletStyle.numbered(MSO_NUMBERED_BULLET_STYLE.HINDI_ALPHA_PERIOD),
}[value]
actual = context.paragraph.bullet
assert actual == expected, 'paragraph.bullet == "%s"' % actual