Skip to content

Commit 3272199

Browse files
author
Steve Canny
committed
img: add __init__() and load() overrides to ImagePart
1 parent 0be0080 commit 3272199

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

docx/parts/document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def inline_shapes(self):
6060
@classmethod
6161
def load(cls, partname, content_type, blob, package):
6262
document_elm = oxml_fromstring(blob)
63-
document = cls(partname, content_type, document_elm, package)
64-
return document
63+
document_part = cls(partname, content_type, document_elm, package)
64+
return document_part
6565

6666
@property
6767
def next_id(self):

docx/parts/image.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ class ImagePart(Part):
196196
An image part. Corresponds to the target part of a relationship with type
197197
RELATIONSHIP_TYPE.IMAGE.
198198
"""
199+
def __init__(self, partname, content_type, blob, image=None):
200+
super(ImagePart, self).__init__(partname, content_type, blob)
201+
self._image = image
202+
199203
@property
200204
def filename(self):
201205
"""
@@ -225,6 +229,15 @@ def height(self):
225229
"""
226230
raise NotImplementedError
227231

232+
@classmethod
233+
def load(cls, partname, content_type, blob, package):
234+
"""
235+
Called by ``docx.opc.package.PartFactory`` to load an image part from
236+
a package being opened by ``Document(...)`` call.
237+
"""
238+
image_part = cls(partname, content_type, blob)
239+
return image_part
240+
228241
@property
229242
def sha1(self):
230243
"""

tests/parts/test_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def it_is_used_by_PartFactory_to_construct_image_part(self, load_fixture):
9999
)
100100
assert part is image_part_
101101

102-
def it_can_construct_from_image_instance(self, from_image_fixture):
102+
def it_can_construct_from_an_Image_instance(self, from_image_fixture):
103103
image_, partname_, ImagePart__init__ = from_image_fixture
104104
image_part = ImagePart.from_image(image_, partname_)
105105
ImagePart__init__.assert_called_once_with(

0 commit comments

Comments
 (0)