Skip to content

Commit f85ab04

Browse files
author
Steve Canny
committed
shp: add InlineShapes.__getitem__()
1 parent 85be65c commit f85ab04

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

docx/parts.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ def __init__(self, body_elm):
118118
super(InlineShapes, self).__init__()
119119
self._body = body_elm
120120

121+
def __getitem__(self, idx):
122+
"""
123+
Provide indexed access, e.g. 'inline_shapes[idx]'
124+
"""
125+
try:
126+
inline = self._inline_lst[idx]
127+
except IndexError:
128+
msg = "inline shape index [%d] out of range" % idx
129+
raise IndexError(msg)
130+
return InlineShape(inline)
131+
121132
def __iter__(self):
122133
return (InlineShape(inline) for inline in self._inline_lst)
123134

features/shp-inline-shape-access.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Feature: Access inline shapes in document
88
Then I can access the inline shape collection of the document
99
And the length of the inline shape collection is 2
1010

11-
@wip
1211
Scenario: Access shape in inline shape collection
1312
Given an inline shape collection containing two shapes
1413
Then I can iterate over the inline shape collection

tests/test_parts.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,23 @@ def it_can_iterate_over_its_InlineShape_instances(
273273
actual_count += 1
274274
assert actual_count == inline_shape_count
275275

276+
def it_provides_indexed_access_to_inline_shapes(
277+
self, inline_shapes_fixture):
278+
inline_shapes, inline_shape_count = inline_shapes_fixture
279+
for idx in range(-inline_shape_count, inline_shape_count):
280+
inline_shape = inline_shapes[idx]
281+
assert isinstance(inline_shape, InlineShape)
282+
283+
def it_raises_on_indexed_access_out_of_range(
284+
self, inline_shapes_fixture):
285+
inline_shapes, inline_shape_count = inline_shapes_fixture
286+
with pytest.raises(IndexError):
287+
too_low = -1 - inline_shape_count
288+
inline_shapes[too_low]
289+
with pytest.raises(IndexError):
290+
too_high = inline_shape_count
291+
inline_shapes[too_high]
292+
276293
# fixtures -------------------------------------------------------
277294

278295
@pytest.fixture

0 commit comments

Comments
 (0)