Skip to content

Commit ef56632

Browse files
committed
Normalize None image filename before escaping
Treat image.filename == None (e.g., BytesIO/file-like descriptors) as an empty string before calling xml_escape so XML attribute generation matches python-docx behavior. Added a clarifying comment and ensure the escaped filename is stored in the cache to avoid None-related issues when rendering.
1 parent 82fd69c commit ef56632

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

docxtpl/inline_image.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,10 @@ def _insert_image(self):
125125
image_part, image = self.tpl._get_or_add_image_part(image_descriptor)
126126
rId = part.relate_to(image_part, RT.IMAGE)
127127
cx, cy = image.scaled_dimensions(self.width, self.height)
128-
# Escape for use inside XML attribute (quotes must be escaped)
129-
filename = xml_escape(image.filename, {'"': """})
128+
# Escape for use inside XML attribute (quotes must be escaped).
129+
# image.filename is None for file-like descriptors (BytesIO);
130+
# normalize to empty string to match python-docx's behavior.
131+
filename = xml_escape(image.filename or "", {'"': """})
130132
cache[cache_key] = (rId, int(cx), int(cy), filename)
131133

132134
# Always assign a fresh shape_id per insertion so that drawing IDs

0 commit comments

Comments
 (0)