66
77from __future__ import absolute_import , print_function , unicode_literals
88
9+ import hashlib
10+
911from behave import given , then , when
1012
1113from docx import Document
1416from docx .oxml .ns import nsdecls , qn
1517from docx .text import Run
1618
17- from helpers import test_docx , test_text
19+ from helpers import test_docx , test_file , test_text
1820
1921
2022# given ===================================================
2325def given_a_run (context ):
2426 document = Document ()
2527 run = document .add_paragraph ().add_run ()
28+ context .document = document
2629 context .run = run
2730
2831
@@ -79,6 +82,21 @@ def given_a_run_having_style_char_style(context, char_style):
7982 context .run = document .paragraphs [0 ].runs [run_idx ]
8083
8184
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+
82100# when ====================================================
83101
84102@when ('I add a column break' )
@@ -99,6 +117,12 @@ def when_add_page_break(context):
99117 run .add_break (WD_BREAK .PAGE )
100118
101119
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+
102126@when ('I add a run specifying its text' )
103127def when_I_add_a_run_specifying_its_text (context ):
104128 context .run = context .paragraph .add_run (test_text )
@@ -184,6 +208,23 @@ def then_last_item_in_run_is_a_break(context):
184208 assert context .last_child .tag == expected_tag
185209
186210
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+
187228@then ('the run appears in {boolean_prop_name} unconditionally' )
188229def then_run_appears_in_boolean_prop_name (context , boolean_prop_name ):
189230 run = context .run
0 commit comments