[Python] PNG image is partially corrupt when loading into word document with wand.image.Image

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a Python script that takes multiple one-page SVG files, converting them to a PNG file, and then combining them into one word document. But when inspecting the word document the images are partially corrupt with black ink covering more than half of the image.

    The image inside the word document looks like this (shouldn't be covered in black ink); [​IMG]

    Python code

    from docx import Document
    from wand.image import Image
    from io import BytesIO

    def create_word_document(input_files: list[str], output_docx: str):
    doc = Document()
    for svg_file in input_files:
    with Image(filename=svg_file, format='svg') as img:
    width, height = 600, 800
    img.resize(width, height)

    with img.convert('png') as converted:
    png_bytes = converted.make_blob('png')

    doc.add_picture(BytesIO(png_bytes))
    doc.save(output_docx)

    Continue reading...

Compartilhe esta Página