Skip to content

Commit 5d7cf81

Browse files
author
Steve Canny
committed
opc: make _ContentTypesItem case-ins on ext
* A partname with a capitalized extension (like .PNG) shall match an existing Default element having the same extension in lower-case. * Extension value shall always be lowercase, regardless of the case of the partname extension. e.g. '/part/name.JPG' causes '<Default Extension="jpg" …>' to be written.
1 parent 80653bd commit 5d7cf81

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

docx/opc/pkgwriter.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .oxml import CT_Types, serialize_part_xml
1212
from .packuri import CONTENT_TYPES_URI, PACKAGE_URI
1313
from .phys_pkg import PhysPkgWriter
14+
from .shared import CaseInsensitiveDict
1415
from .spec import default_content_types
1516

1617

@@ -75,7 +76,9 @@ def xml_for(parts):
7576
appropriate content type and suitable for storage as
7677
``[Content_Types].xml`` in an OPC package.
7778
"""
78-
defaults = dict((('.rels', CT.OPC_RELATIONSHIPS), ('.xml', CT.XML)))
79+
defaults = CaseInsensitiveDict()
80+
defaults['.rels'] = CT.OPC_RELATIONSHIPS
81+
defaults['.xml'] = CT.XML
7982
overrides = dict()
8083
for part in parts:
8184
_ContentTypesItem._add_content_type(
@@ -90,7 +93,7 @@ def _add_content_type(defaults, overrides, partname, content_type):
9093
using a default or override as appropriate.
9194
"""
9295
ext = partname.ext
93-
if (ext, content_type) in default_content_types:
96+
if (ext.lower(), content_type) in default_content_types:
9497
defaults[ext] = content_type
9598
else:
9699
overrides[partname] = content_type

tests/opc/test_pkgwriter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def _mock_part(self, request, name, partname_str, content_type):
126126
)
127127

128128
@pytest.fixture(params=[
129-
# ('Default', '/ppt/MEDIA/image.PNG', CT.PNG),
129+
('Default', '/ppt/MEDIA/image.PNG', CT.PNG),
130130
('Default', '/ppt/media/image.xml', CT.XML),
131131
('Default', '/ppt/media/image.rels', CT.OPC_RELATIONSHIPS),
132132
('Default', '/ppt/media/image.jpeg', CT.JPEG),

0 commit comments

Comments
 (0)