Skip to content

Commit 4bccc6f

Browse files
author
Steve Canny
committed
acpt: add shp-add-inline-picture.feature
1 parent 8102bd4 commit 4bccc6f

3 files changed

Lines changed: 47 additions & 2 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Feature: Add inline picture
2+
In order to generate a document containing an image
3+
As a python-docx developer
4+
I need the ability to add an inline picture to a document
5+
6+
@wip
7+
Scenario: Add inline picture to document
8+
Given a document
9+
When I add an inline picture to the document
10+
Then its inline shape type is WD_INLINE_SHAPE.PICTURE
11+
And the document contains the inline picture

features/steps/helpers.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ def test_docx(name):
2424
Return the absolute path to test .docx file with root name *name*.
2525
"""
2626
return absjoin(thisdir, 'test_files', '%s.docx' % name)
27+
28+
29+
def test_file_path(name):
30+
"""
31+
Return the absolute path to file with *name* in test_files directory
32+
"""
33+
return absjoin(thisdir, 'test_files', '%s' % name)

features/steps/shape.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

9-
from behave import given, then
9+
import hashlib
10+
11+
from behave import given, then, when
1012

1113
from docx import Document
1214
from docx.enum.shape import WD_INLINE_SHAPE
1315
from docx.parts import InlineShape, InlineShapes
1416

15-
from .helpers import test_docx
17+
from .helpers import test_docx, test_file_path
1618

1719

1820
# given ===================================================
@@ -44,6 +46,16 @@ def given_inline_shape_known_to_be_shape_of_type(context, shp_of_type):
4446
context.inline_shape = document.inline_shapes[inline_shape_idx]
4547

4648

49+
# when =====================================================
50+
51+
@when('I add an inline picture to the document')
52+
def when_add_inline_picture_to_document(context):
53+
document = context.document
54+
context.inline_shape = (
55+
document.add_picture(test_file_path('monty-truth.png'))
56+
)
57+
58+
4759
# then =====================================================
4860

4961
@then('I can access each inline shape by index')
@@ -86,6 +98,21 @@ def then_inline_shape_type_is_shape_type(context, shape_type):
8698
assert inline_shape.type == expected_value
8799

88100

101+
@then('the document contains the inline picture')
102+
def then_the_document_contains_the_inline_picture(context):
103+
document = context.document
104+
picture_shape = document.inline_shapes[0]
105+
blip = picture_shape._inline.graphic.graphicData.pic.blipFill.blip
106+
rId = blip.embed
107+
image_part = document.related_parts[rId]
108+
image_sha1 = hashlib.sha1(image_part.blob).hexdigest()
109+
expected_sha1 = '92abcF0eab86674as0e029342309023423'
110+
assert image_sha1 == expected_sha1, (
111+
"image SHA1 doesn't match, expected %s, got %s" %
112+
(expected_sha1, image_sha1)
113+
)
114+
115+
89116
@then('the length of the inline shape collection is 5')
90117
def then_len_of_inline_shape_collection_is_5(context):
91118
inline_shapes = context.document.inline_shapes

0 commit comments

Comments
 (0)