Skip to content

Commit 031c8b5

Browse files
author
Steve Canny
committed
api: add _Document.add_inline_picture()
Along the way: * rename _pkg to _package in api._Document * remove no-op return from api._Document.save()
1 parent 8e9210c commit 031c8b5

File tree

4 files changed

+39
-4
lines changed

4 files changed

+39
-4
lines changed

docx/api.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,18 @@ class _Document(object):
3737
"""
3838
API class representing a Word document.
3939
"""
40-
def __init__(self, pkg, document_part):
40+
def __init__(self, package, document_part):
4141
super(_Document, self).__init__()
4242
self._document = document_part
43-
self._pkg = pkg
43+
self._package = package
44+
45+
def add_inline_picture(self, image_path_or_stream):
46+
"""
47+
Add the image at *image_path_or_stream* to the document at its native
48+
size. The picture is placed inline in a new paragraph at the end of
49+
the document.
50+
"""
51+
return self.inline_shapes.add_picture(image_path_or_stream)
4452

4553
@property
4654
def body(self):
@@ -61,4 +69,4 @@ def save(self, file_):
6169
Save this document to *file_*, where *file_* can be either a path to
6270
a file (a string) or a file-like object.
6371
"""
64-
return self._pkg.save(file_)
72+
self._package.save(file_)

docx/parts.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ def __iter__(self):
151151
def __len__(self):
152152
return len(self._inline_lst)
153153

154+
def add_picture(self, image_path_or_stream):
155+
"""
156+
Add the image at *image_path_or_stream* to the document at its native
157+
size. The picture is placed inline in a new paragraph at the end of
158+
the document.
159+
"""
160+
154161
@property
155162
def _inline_lst(self):
156163
body = self._body

features/steps/shape.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def given_inline_shape_known_to_be_shape_of_type(context, shp_of_type):
5252
def when_add_inline_picture_to_document(context):
5353
document = context.document
5454
context.inline_shape = (
55-
document.add_picture(test_file_path('monty-truth.png'))
55+
document.add_inline_picture(test_file_path('monty-truth.png'))
5656
)
5757

5858

tests/test_api.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from docx.api import Document, _Document
1111
from docx.opc.constants import CONTENT_TYPE as CT
1212
from docx.opc.package import OpcPackage
13+
from docx.parts import InlineShapes
1314

1415
from .unitutil import class_mock, instance_mock, var_mock
1516

@@ -82,13 +83,32 @@ def it_provides_access_to_the_document_inline_shapes(self, document):
8283
body = document.inline_shapes
8384
assert body is document._document.inline_shapes
8485

86+
def it_can_add_an_inline_picture(self, add_picture_fixture):
87+
document, inline_shapes, image_path_or_stream_, picture_shape_ = (
88+
add_picture_fixture
89+
)
90+
picture_shape = document.add_inline_picture(image_path_or_stream_)
91+
inline_shapes.add_picture.assert_called_once_with(
92+
image_path_or_stream_
93+
)
94+
assert picture_shape is picture_shape_
95+
8596
def it_can_save_the_package(self, save_fixture):
8697
document, package_, file_ = save_fixture
8798
document.save(file_)
8899
package_.save.assert_called_once_with(file_)
89100

90101
# fixtures -------------------------------------------------------
91102

103+
@pytest.fixture
104+
def add_picture_fixture(self, request, document_part_):
105+
document = _Document(None, document_part_)
106+
inline_shapes = instance_mock(request, InlineShapes)
107+
document_part_.inline_shapes = inline_shapes
108+
image_path_ = instance_mock(request, str)
109+
picture_shape_ = inline_shapes.add_picture.return_value
110+
return document, inline_shapes, image_path_, picture_shape_
111+
92112
@pytest.fixture
93113
def document(self, request, package_, document_part_):
94114
return _Document(package_, document_part_)

0 commit comments

Comments
 (0)