From 32f3d9fae7ad0a4ad7cc9c74d81a6fd670912fbe Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 9 Aug 2026 14:45:32 +0100 Subject: Check image content type for backslashes --- mammoth/cli.py | 20 ++++++++++---------- mammoth/images.py | 9 +++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) (limited to 'mammoth') diff --git a/mammoth/cli.py b/mammoth/cli.py index b759412..08b67c6 100644 --- a/mammoth/cli.py +++ b/mammoth/cli.py @@ -10,13 +10,13 @@ from . import writers def main(): args = _parse_args() - + if args.style_map is None: style_map = None else: with open(args.style_map) as style_map_fileobj: style_map = style_map_fileobj.read() - + with open(args.path, "rb") as docx_fileobj: if args.output_dir is None: convert_image = None @@ -25,7 +25,7 @@ def main(): convert_image = mammoth.images.img_element(ImageWriter(args.output_dir)) output_filename = "{0}.html".format(os.path.basename(args.path).rpartition(".")[0]) output_path = os.path.join(args.output_dir, output_filename) - + result = mammoth.convert( docx_fileobj, style_map=style_map, @@ -35,7 +35,7 @@ def main(): for message in result.messages: sys.stderr.write(message.message) sys.stderr.write("\n") - + _write_output(output_path, result.value) @@ -43,16 +43,16 @@ class ImageWriter(object): def __init__(self, output_dir): self._output_dir = output_dir self._image_number = 1 - + def __call__(self, element): - extension = element.content_type.partition("/")[2] + extension = mammoth.images.image_filename_extension(element) image_filename = "{0}.{1}".format(self._image_number, extension) with open(os.path.join(self._output_dir, image_filename), "wb") as image_dest: with element.open() as image_source: shutil.copyfileobj(image_source, image_dest) - + self._image_number += 1 - + return {"src": image_filename} @@ -76,7 +76,7 @@ def _parse_args(): "path", metavar="docx-path", help="Path to the .docx file to convert.") - + output_group = parser.add_mutually_exclusive_group() output_group.add_argument( "output", @@ -86,7 +86,7 @@ def _parse_args(): output_group.add_argument( "--output-dir", help="Output directory for generated HTML and images. Images will be stored in separate files. Mutually exclusive with output-path.") - + parser.add_argument( "--output-format", required=False, diff --git a/mammoth/images.py b/mammoth/images.py index 21b9288..1623a73 100644 --- a/mammoth/images.py +++ b/mammoth/images.py @@ -1,4 +1,5 @@ import base64 +import re from . import html @@ -26,3 +27,11 @@ def data_uri(image): return { "src": "data:{0};base64,{1}".format(image.content_type, encoded_src) } + + +def image_filename_extension(image): + result = re.split(r"/|\\", image.content_type) + if len(result) < 2: + return None + else: + return result[1] -- cgit v1.2.3