Skip to content

Commit dde7784

Browse files
author
Steve Canny
committed
img: add ImagePart.filename
1 parent 16a588f commit dde7784

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

docx/parts/image.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ def filename(self):
235235
case a default extension is applied based on the detected MIME type
236236
of the image.
237237
"""
238-
raise NotImplementedError
238+
if self._image is not None:
239+
return self._image.filename
240+
return 'image%s' % self.partname.ext
239241

240242
@classmethod
241243
def from_image(cls, image, partname):

tests/parts/test_image.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ def it_knows_its_default_dimensions_in_EMU(self, dimensions_fixture):
112112
assert image_part.default_cx == cx
113113
assert image_part.default_cy == cy
114114

115+
def it_knows_its_filename(self, filename_fixture):
116+
image_part, expected_filename = filename_fixture
117+
assert image_part.filename == expected_filename
118+
115119
# fixtures -------------------------------------------------------
116120

117121
@pytest.fixture
@@ -137,6 +141,18 @@ def dimensions_fixture(self, request):
137141

138142
return image_part, expected_cx, expected_cy
139143

144+
@pytest.fixture(params=['loaded', 'new'])
145+
def filename_fixture(self, request, image_):
146+
partname = PackURI('/word/media/image666.png')
147+
if request.param == 'loaded':
148+
image_part = ImagePart(partname, None, None, None)
149+
expected_filename = 'image.png'
150+
elif request.param == 'new':
151+
image_.filename = 'foobar.PXG'
152+
image_part = ImagePart(partname, None, None, image_)
153+
expected_filename = image_.filename
154+
return image_part, expected_filename
155+
140156
@pytest.fixture
141157
def from_image_fixture(self, image_, partname_, ImagePart__init__):
142158
return image_, partname_, ImagePart__init__

0 commit comments

Comments
 (0)