Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 374b505

Browse files
author
Steve Canny
committed
img: add Image.ext
Also refactored other ext usage to leave off the leading dot.
1 parent fcba2bf commit 374b505

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

docx/image/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def ext(self):
7777
a load filename it is used. Otherwise a canonical extension is
7878
assigned based on the content type.
7979
"""
80-
return os.path.splitext(self._filename)[1]
80+
return os.path.splitext(self._filename)[1][1:]
8181

8282
@property
8383
def filename(self):

docx/image/image.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ def content_type(self):
5353
"""
5454
return self._image_header.content_type
5555

56+
@lazyproperty
57+
def ext(self):
58+
"""
59+
The file extension for the image. If an actual one is available from
60+
a load filename it is used. Otherwise a canonical extension is
61+
assigned based on the content type. Does not contain the leading
62+
period, e.g. 'jpg', not '.jpg'.
63+
"""
64+
return os.path.splitext(self._filename)[1][1:]
65+
5666
@property
5767
def px_width(self):
5868
"""

docx/package.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ def _get_by_sha1(self, sha1):
101101
def _next_image_partname(self, ext):
102102
"""
103103
The next available image partname, starting from
104-
``/word/media/image1{ext}`` where unused numbers are reused. The
104+
``/word/media/image1.{ext}`` where unused numbers are reused. The
105105
partname is unique by number, without regard to the extension. *ext*
106-
must include the leading period.
106+
does not include the leading period.
107107
"""
108108
def image_partname(n):
109-
return PackURI('/word/media/image%d%s' % (n, ext))
109+
return PackURI('/word/media/image%d.%s' % (n, ext))
110110
used_numbers = [image_part.partname.idx for image_part in self]
111111
for n in range(1, len(self)+1):
112112
if not n in used_numbers:

tests/image/test_image.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ def it_knows_the_image_dimensions(self, dimensions_fixture):
6464
assert image.px_width == px_width
6565
assert image.px_height == px_height
6666

67+
def it_knows_the_image_filename_extension(self):
68+
image = Image(None, 'foobar.png', None)
69+
assert image.ext == 'png'
70+
6771
def it_knows_the_horz_and_vert_dpi_of_the_image(self, dpi_fixture):
6872
image_header_, horz_dpi, vert_dpi = dpi_fixture
6973
image = Image(None, None, image_header_)
@@ -240,13 +244,13 @@ def it_can_construct_from_an_image_stream(self):
240244
def it_knows_the_extension_of_a_file_based_image(self):
241245
image_file_path = test_file('monty-truth.png')
242246
image = Image_OLD.from_file(image_file_path)
243-
assert image.ext == '.png'
247+
assert image.ext == 'png'
244248

245249
def it_knows_the_extension_of_a_stream_based_image(self):
246250
image_file_path = test_file('monty-truth.png')
247251
with open(image_file_path, 'rb') as image_file_stream:
248252
image = Image_OLD.from_file(image_file_stream)
249-
assert image.ext == '.png'
253+
assert image.ext == 'png'
250254

251255
def it_correctly_characterizes_a_few_known_images(
252256
self, known_image_fixture):
@@ -268,16 +272,16 @@ def it_correctly_characterizes_a_few_known_images(
268272
@pytest.fixture(params=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
269273
def known_image_fixture(self, request):
270274
cases = (
271-
('python.bmp', ('.bmp', CT.BMP, 211, 71, 72, 72)),
272-
('sonic.gif', ('.gif', CT.GIF, 290, 360, 72, 72)),
273-
('python-icon.jpeg', ('.jpg', CT.JPEG, 204, 204, 72, 72)),
274-
('300-dpi.jpg', ('.jpg', CT.JPEG, 1504, 1936, 300, 300)),
275-
('monty-truth.png', ('.png', CT.PNG, 150, 214, 72, 72)),
276-
('150-dpi.png', ('.png', CT.PNG, 901, 1350, 150, 150)),
277-
('300-dpi.png', ('.png', CT.PNG, 860, 579, 300, 300)),
278-
('72-dpi.tiff', ('.tiff', CT.TIFF, 48, 48, 72, 72)),
279-
('300-dpi.TIF', ('.tiff', CT.TIFF, 2464, 3248, 300, 300)),
280-
('CVS_LOGO.WMF', ('.wmf', CT.X_WMF, 149, 59, 72, 72)),
275+
('python.bmp', ('bmp', CT.BMP, 211, 71, 72, 72)),
276+
('sonic.gif', ('gif', CT.GIF, 290, 360, 72, 72)),
277+
('python-icon.jpeg', ('jpg', CT.JPEG, 204, 204, 72, 72)),
278+
('300-dpi.jpg', ('jpg', CT.JPEG, 1504, 1936, 300, 300)),
279+
('monty-truth.png', ('png', CT.PNG, 150, 214, 72, 72)),
280+
('150-dpi.png', ('png', CT.PNG, 901, 1350, 150, 150)),
281+
('300-dpi.png', ('png', CT.PNG, 860, 579, 300, 300)),
282+
('72-dpi.tiff', ('tiff', CT.TIFF, 48, 48, 72, 72)),
283+
('300-dpi.TIF', ('tiff', CT.TIFF, 2464, 3248, 300, 300)),
284+
('CVS_LOGO.WMF', ('wmf', CT.X_WMF, 149, 59, 72, 72)),
281285
)
282286
image_filename, characteristics = cases[request.param]
283287
return image_filename, characteristics

tests/test_package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def next_partname_fixture(self, request):
129129
for n in existing_partname_numbers:
130130
image_part_ = self._image_part_with_partname_(request, n)
131131
image_parts.append(image_part_)
132-
ext = '.png'
132+
ext = 'png'
133133
expected_image_partname = self._image_partname(
134134
expected_partname_number
135135
)

0 commit comments

Comments
 (0)