6
6
7
7
from __future__ import absolute_import , print_function , unicode_literals
8
8
9
+ import hashlib
10
+
9
11
from behave import given , then , when
10
12
11
13
from docx import Document
14
16
from docx .oxml .ns import nsdecls , qn
15
17
from docx .text import Run
16
18
17
- from helpers import test_docx , test_text
19
+ from helpers import test_docx , test_file , test_text
18
20
19
21
20
22
# given ===================================================
23
25
def given_a_run (context ):
24
26
document = Document ()
25
27
run = document .add_paragraph ().add_run ()
28
+ context .document = document
26
29
context .run = run
27
30
28
31
@@ -79,6 +82,21 @@ def given_a_run_having_style_char_style(context, char_style):
79
82
context .run = document .paragraphs [0 ].runs [run_idx ]
80
83
81
84
85
+ @given ('a run inside a table cell retrieved from {cell_source}' )
86
+ def given_a_run_inside_a_table_cell_from_source (context , cell_source ):
87
+ document = Document ()
88
+ table = document .add_table (rows = 2 , cols = 2 )
89
+ if cell_source == 'Table.cell' :
90
+ cell = table .cell (0 , 0 )
91
+ elif cell_source == 'Table.row.cells' :
92
+ cell = table .rows [0 ].cells [1 ]
93
+ elif cell_source == 'Table.column.cells' :
94
+ cell = table .columns [1 ].cells [0 ]
95
+ run = cell .paragraphs [0 ].add_run ()
96
+ context .document = document
97
+ context .run = run
98
+
99
+
82
100
# when ====================================================
83
101
84
102
@when ('I add a column break' )
@@ -99,6 +117,12 @@ def when_add_page_break(context):
99
117
run .add_break (WD_BREAK .PAGE )
100
118
101
119
120
+ @when ('I add a picture to the run' )
121
+ def when_I_add_a_picture_to_the_run (context ):
122
+ run = context .run
123
+ run .add_picture (test_file ('monty-truth.png' ))
124
+
125
+
102
126
@when ('I add a run specifying its text' )
103
127
def when_I_add_a_run_specifying_its_text (context ):
104
128
context .run = context .paragraph .add_run (test_text )
@@ -184,6 +208,23 @@ def then_last_item_in_run_is_a_break(context):
184
208
assert context .last_child .tag == expected_tag
185
209
186
210
211
+ @then ('the picture appears at the end of the run' )
212
+ def then_the_picture_appears_at_the_end_of_the_run (context ):
213
+ run = context .run
214
+ r = run ._r
215
+ blip_rId = r .xpath (
216
+ './w:drawing/wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/'
217
+ 'a:blip/@r:embed'
218
+ )[0 ]
219
+ image_part = run .part .related_parts [blip_rId ]
220
+ image_sha1 = hashlib .sha1 (image_part .blob ).hexdigest ()
221
+ expected_sha1 = '79769f1e202add2e963158b532e36c2c0f76a70c'
222
+ assert image_sha1 == expected_sha1 , (
223
+ "image SHA1 doesn't match, expected %s, got %s" %
224
+ (expected_sha1 , image_sha1 )
225
+ )
226
+
227
+
187
228
@then ('the run appears in {boolean_prop_name} unconditionally' )
188
229
def then_run_appears_in_boolean_prop_name (context , boolean_prop_name ):
189
230
run = context .run
0 commit comments