logan_9997
logan_9997

Reputation: 619

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

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); enter image description here

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)

Upvotes: 0

Views: 49

Answers (0)

Related Questions