Skip to content

Commit 253b179

Browse files
author
Steve Canny
committed
doc: add Document.add_picture()
1 parent 08703b6 commit 253b179

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

docx/document.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ def add_paragraph(self, text='', style=None):
5959
"""
6060
return self._body.add_paragraph(text, style)
6161

62+
def add_picture(self, image_path_or_stream, width=None, height=None):
63+
"""
64+
Return a new picture shape added in its own paragraph at the end of
65+
the document. The picture contains the image at
66+
*image_path_or_stream*, scaled based on *width* and *height*. If
67+
neither width nor height is specified, the picture appears at its
68+
native size. If only one is specified, it is used to compute
69+
a scaling factor that is then applied to the unspecified dimension,
70+
preserving the aspect ratio of the image. The native size of the
71+
picture is calculated using the dots-per-inch (dpi) value specified
72+
in the image file, defaulting to 72 dpi if no value is specified, as
73+
is often the case.
74+
"""
75+
run = self.add_paragraph().add_run()
76+
return run.add_picture(image_path_or_stream, width, height)
77+
6278
@property
6379
def part(self):
6480
"""

tests/test_document.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from docx.document import _Body, Document
1414
from docx.enum.text import WD_BREAK
1515
from docx.parts.document import DocumentPart
16+
from docx.shape import InlineShape
1617
from docx.text.paragraph import Paragraph
1718
from docx.text.run import Run
1819

@@ -51,6 +52,12 @@ def it_can_add_a_paragraph(self, add_paragraph_fixture):
5152
document._body.add_paragraph.assert_called_once_with(text, style)
5253
assert paragraph is paragraph_
5354

55+
def it_can_add_a_picture(self, add_picture_fixture):
56+
document, path, width, height, run_, picture_ = add_picture_fixture
57+
picture = document.add_picture(path, width, height)
58+
run_.add_picture.assert_called_once_with(path, width, height)
59+
assert picture is picture_
60+
5461
def it_provides_access_to_the_document_part(self, part_fixture):
5562
document, part_ = part_fixture
5663
assert document.part is part_
@@ -94,6 +101,14 @@ def add_paragraph_fixture(self, request, body_prop_, paragraph_):
94101
body_prop_.return_value.add_paragraph.return_value = paragraph_
95102
return document, text, style, paragraph_
96103

104+
@pytest.fixture
105+
def add_picture_fixture(self, request, add_paragraph_, run_, picture_):
106+
document = Document(None, None)
107+
path, width, height = 'foobar.png', 100, 200
108+
add_paragraph_.return_value.add_run.return_value = run_
109+
run_.add_picture.return_value = picture_
110+
return document, path, width, height, run_, picture_
111+
97112
@pytest.fixture
98113
def body_fixture(self, _Body_, body_):
99114
document_elm = element('w:document/w:body')
@@ -132,6 +147,10 @@ def document_part_(self, request):
132147
def paragraph_(self, request):
133148
return instance_mock(request, Paragraph)
134149

150+
@pytest.fixture
151+
def picture_(self, request):
152+
return instance_mock(request, InlineShape)
153+
135154
@pytest.fixture
136155
def run_(self, request):
137156
return instance_mock(request, Run)

0 commit comments

Comments
 (0)