Skip to content

Commit e7bdc57

Browse files
author
Steve Canny
committed
shp: fix #94 picture prints at wrong size
Change the dimensions in both `wp:inline/wp:extent` and `pic:pic/pic:spPr/a:xfrm` when setting width or height of a picture.
1 parent 35db7e3 commit e7bdc57

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

docx/api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ def add_picture(self, image_path_or_stream, width=None, height=None):
8484
is often the case.
8585
"""
8686
run = self.add_paragraph().add_run()
87-
picture = run.add_picture(image_path_or_stream, width, height)
88-
return picture
87+
return run.add_picture(image_path_or_stream, width, height)
8988

9089
def add_section(self, start_type=WD_SECTION.NEW_PAGE):
9190
"""

docx/shape.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ def height(self):
3333

3434
@height.setter
3535
def height(self, cy):
36-
assert isinstance(cy, int)
37-
assert 0 < cy
3836
self._inline.extent.cy = cy
37+
self._inline.graphic.graphicData.pic.spPr.cy = cy
3938

4039
@classmethod
4140
def new_picture(cls, r, image_part, rId, shape_id):
@@ -83,6 +82,5 @@ def width(self):
8382

8483
@width.setter
8584
def width(self, cx):
86-
assert isinstance(cx, int)
87-
assert 0 < cx
8885
self._inline.extent.cx = cx
86+
self._inline.graphic.graphicData.pic.spPr.cx = cx

tests/test_shape.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ def dimensions_get_fixture(self):
6868
@pytest.fixture
6969
def dimensions_set_fixture(self):
7070
inline_cxml, new_cx, new_cy, expected_cxml = (
71-
'wp:inline/wp:extent{cx=333, cy=666}', 444, 888,
72-
'wp:inline/wp:extent{cx=444, cy=888}'
71+
'wp:inline/(wp:extent{cx=333,cy=666},a:graphic/a:graphicData/'
72+
'pic:pic/pic:spPr/a:xfrm/a:ext{cx=333,cy=666})',
73+
444, 888,
74+
'wp:inline/(wp:extent{cx=444,cy=888},a:graphic/a:graphicData/'
75+
'pic:pic/pic:spPr/a:xfrm/a:ext{cx=444,cy=888})'
7376
)
7477
inline_shape = InlineShape(element(inline_cxml))
7578
expected_xml = xml(expected_cxml)

0 commit comments

Comments
 (0)