Skip to content

Commit 6ac7ced

Browse files
author
Steve Canny
committed
acpt: add run-add-picture.feature
1 parent ff0bd91 commit 6ac7ced

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

features/run-add-picture.feature

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Feature: Add picture to a run
2+
In order to place an inline picture at an arbitrary place in a document
3+
As a developer using python-docx
4+
I need a way to add a picture to a run
5+
6+
@wip
7+
Scenario: Add a picture to a body paragraph run
8+
Given a run
9+
When I add a picture to the run
10+
Then the picture appears at the end of the run
11+
And the document contains the inline picture
12+
13+
14+
@wip
15+
Scenario Outline: Add a picture to a run in a table cell
16+
Given a run inside a table cell retrieved from <cell-source>
17+
When I add a picture to the run
18+
Then the picture appears at the end of the run
19+
And the document contains the inline picture
20+
21+
Examples: Table cell sources
22+
| cell-source |
23+
| Table.cell |
24+
| Table.row.cells |
25+
| Table.column.cells |

features/steps/text.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

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

1113
from docx import Document
@@ -14,7 +16,7 @@
1416
from docx.oxml.ns import nsdecls, qn
1517
from 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 ===================================================
@@ -23,6 +25,7 @@
2325
def 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')
103127
def 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')
188229
def then_run_appears_in_boolean_prop_name(context, boolean_prop_name):
189230
run = context.run

0 commit comments

Comments
 (0)