pqzx / html2docx

Convert html to docx
MIT License
69 stars 49 forks source link

handling base64 image #67

Open BowenFu opened 5 months ago

BowenFu commented 5 months ago

https://github.com/pqzx/html2docx/blob/9337c3950bee62a4fea5b722e7ba19c163df4d9f/htmldocx/h2d.py#L312C30-L312C41

Seems this is not handling base64 image properly. Maybe something like this can fix it.

from io import BytesIO
from docx import Document

# Your base64 image string
base64_img = 'your_base64_string_here'

# Decode the base64 string to binary
img_data = base64.b64decode(base64_img)

# Use an in-memory bytes buffer
img_stream = BytesIO(img_data)

# Create or open a document
doc = Document()

# Add the image to the document using the in-memory stream
doc.add_picture(img_stream)

# Save the document
doc.save('your_document.docx')