|
6 | 6 |
|
7 | 7 | from __future__ import absolute_import, print_function, unicode_literals |
8 | 8 |
|
9 | | -from behave import given, then |
| 9 | +import hashlib |
| 10 | + |
| 11 | +from behave import given, then, when |
10 | 12 |
|
11 | 13 | from docx import Document |
12 | 14 | from docx.enum.shape import WD_INLINE_SHAPE |
13 | 15 | from docx.parts import InlineShape, InlineShapes |
14 | 16 |
|
15 | | -from .helpers import test_docx |
| 17 | +from .helpers import test_docx, test_file_path |
16 | 18 |
|
17 | 19 |
|
18 | 20 | # given =================================================== |
@@ -44,6 +46,16 @@ def given_inline_shape_known_to_be_shape_of_type(context, shp_of_type): |
44 | 46 | context.inline_shape = document.inline_shapes[inline_shape_idx] |
45 | 47 |
|
46 | 48 |
|
| 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 | + |
47 | 59 | # then ===================================================== |
48 | 60 |
|
49 | 61 | @then('I can access each inline shape by index') |
@@ -86,6 +98,21 @@ def then_inline_shape_type_is_shape_type(context, shape_type): |
86 | 98 | assert inline_shape.type == expected_value |
87 | 99 |
|
88 | 100 |
|
| 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 | + |
89 | 116 | @then('the length of the inline shape collection is 5') |
90 | 117 | def then_len_of_inline_shape_collection_is_5(context): |
91 | 118 | inline_shapes = context.document.inline_shapes |
|
0 commit comments