Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7df5323

Browse files
author
Steve Canny
committed
shp: refactor InlineShapes.add_picture()
Key change is adding *run* parameter to InlineShapes.add_picture() rather than adding a run to the document in the method, so picture can be added to an arbitrary run.
1 parent d77054a commit 7df5323

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

docx/api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def add_picture(self, image_path_or_stream, width=None, height=None):
8787
(dpi) value specified in the image file, defaulting to 72 dpi if no
8888
value is specified, as is often the case.
8989
"""
90-
picture = self.inline_shapes.add_picture(image_path_or_stream)
90+
run = self.add_paragraph().add_run()
91+
picture = self.inline_shapes.add_picture(image_path_or_stream, run)
9192

9293
# scale picture dimensions if width and/or height provided
9394
if width is not None or height is not None:

docx/parts/document.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,17 +190,18 @@ def __iter__(self):
190190
def __len__(self):
191191
return len(self._inline_lst)
192192

193-
def add_picture(self, image_descriptor):
193+
def add_picture(self, image_descriptor, run):
194194
"""
195-
Add the image identified by *image_descriptor* to the document at its
196-
native size. The picture is placed inline in a new paragraph at the
197-
end of the document. *image_descriptor* can be a path (a string) or a
198-
file-like object containing a binary image.
195+
Return an |InlineShape| instance containing the picture identified by
196+
*image_descriptor* and added to the end of *run*. The picture shape
197+
has the native size of the image. *image_descriptor* can be a path (a
198+
string) or a file-like object containing a binary image.
199199
"""
200200
image_part, rId = self.part.get_or_add_image_part(image_descriptor)
201201
shape_id = self.part.next_id
202-
r = self._body.add_p().add_r()
203-
return InlineShape.new_picture(r, image_part, rId, shape_id)
202+
r = run._r
203+
picture = InlineShape.new_picture(r, image_part, rId, shape_id)
204+
return picture
204205

205206
@property
206207
def _inline_lst(self):

features/steps/shape.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@ def given_inline_shape_known_to_be_shape_of_type(context, shp_of_type):
5858
@when('I add an inline picture from a file-like object')
5959
def when_add_inline_picture_from_file_like_object(context):
6060
document = context.document
61+
run = document.add_paragraph().add_run()
6162
with open(test_file_path('monty-truth.png'), 'rb') as f:
62-
context.inline_shape = document.inline_shapes.add_picture(f)
63+
context.inline_shape = document.inline_shapes.add_picture(f, run)
6364

6465

6566
@when('I add an inline picture to the document')
6667
def when_add_inline_picture_to_document(context):
6768
document = context.document
69+
run = document.add_paragraph().add_run()
6870
context.inline_shape = (document.inline_shapes.add_picture(
69-
test_file_path('monty-truth.png')
71+
test_file_path('monty-truth.png'), run
7072
))
7173

7274

tests/parts/test_document.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from docx.section import Section
1919
from docx.shape import InlineShape
2020
from docx.table import Table
21-
from docx.text import Paragraph
21+
from docx.text import Paragraph, Run
2222

2323
from ..oxml.parts.unitdata.document import a_body, a_document
2424
from ..oxml.unitdata.table import (
@@ -448,10 +448,10 @@ def it_can_add_an_inline_picture_to_the_document(
448448
self, add_picture_fixture):
449449
# fixture ----------------------
450450
(inline_shapes, image_descriptor_, document_, InlineShape_,
451-
r_, image_part_, rId_, shape_id_, new_picture_shape_
451+
run, r_, image_part_, rId_, shape_id_, new_picture_shape_
452452
) = add_picture_fixture
453453
# exercise ---------------------
454-
picture_shape = inline_shapes.add_picture(image_descriptor_)
454+
picture_shape = inline_shapes.add_picture(image_descriptor_, run)
455455
# verify -----------------------
456456
document_.get_or_add_image_part.assert_called_once_with(
457457
image_descriptor_
@@ -474,9 +474,10 @@ def add_picture_fixture(
474474
r_, image_part_, rId_, shape_id_, new_picture_shape_):
475475
inline_shapes = InlineShapes(body_, None)
476476
property_mock(request, InlineShapes, 'part', return_value=document_)
477+
run = Run(r_, None)
477478
return (
478-
inline_shapes, image_descriptor_, document_, InlineShape_, r_,
479-
image_part_, rId_, shape_id_, new_picture_shape_
479+
inline_shapes, image_descriptor_, document_, InlineShape_, run,
480+
r_, image_part_, rId_, shape_id_, new_picture_shape_
480481
)
481482

482483
@pytest.fixture

tests/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from docx.text import Paragraph, Run
2323

2424
from .unitutil.mock import (
25-
instance_mock, class_mock, method_mock, property_mock, var_mock
25+
ANY, instance_mock, class_mock, method_mock, property_mock, var_mock
2626
)
2727

2828

@@ -93,7 +93,7 @@ def it_can_add_a_picture(self, add_picture_fixture):
9393
(document, image_path, width, height, inline_shapes_, expected_width,
9494
expected_height, picture_) = add_picture_fixture
9595
picture = document.add_picture(image_path, width, height)
96-
inline_shapes_.add_picture.assert_called_once_with(image_path)
96+
inline_shapes_.add_picture.assert_called_once_with(image_path, ANY)
9797
assert picture.width == expected_width
9898
assert picture.height == expected_height
9999
assert picture is picture_

tests/unitutil/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from unittest.mock import create_autospec, Mock, patch, PropertyMock
1515
else:
1616
import mock # noqa
17-
from mock import call, MagicMock # noqa
17+
from mock import ANY, call, MagicMock # noqa
1818
from mock import create_autospec, Mock, patch, PropertyMock
1919

2020

0 commit comments

Comments
 (0)