python-openxml / python-docx

Create and modify Word documents with Python
MIT License
4.54k stars 1.11k forks source link

Add basic support for adding SVG pictures to docx files #1343

Closed takis closed 5 months ago

takis commented 7 months ago

See issues #351, #651, #659.

This replaces this pull request from two years ago: https://github.com/python-openxml/python-docx/pull/1107

takis commented 7 months ago

This is the simple code I've been testing with:

import datetime

from docx import Document

SVG = """<svg version="1.1"
     width="300" height="200"
     xmlns="http://www.w3.org/2000/svg">

  <rect width="100%" height="100%" fill="red" />
  <circle cx="150" cy="100" r="80" fill="green" />
  <text x="150" y="125" font-size="60" text-anchor="middle" fill="white">SVG</text>
</svg>
"""
svg_filename = "drawing.svg"
with open(svg_filename, "w") as f:
    f.write(SVG)

document = Document()
now = datetime.datetime.now()
document.add_heading("Document Title", 0)
document.add_heading(f"{now}", 0)

document.add_picture(svg_filename)

document.save("demo.docx")
mzaanen commented 6 months ago

Thank you so much for this fix. I just could not get QR codes sharp in a docx document.

First I used .png files using segno and did "add_picture" in python-docx. The .png images are sharp, but the add_picture resizes and blurs it all. Result is a rescaled and blurred image in the .docx.

Now I make .svg files using segno and I use your branch (https://github.com/takis/python-docx.git, master) to do the add_picture and it is now spot on; no blurring, sharp as anything.

You saved my day ! Thanks again.

michaelarfreed commented 5 months ago

I'd love to see this PR approved! It is a great fix for a feature that windows supports. I need this feature for code I'm developing, and can't rely on a branch outside of the main branch, so would love to see the PR approved !!

takis commented 5 months ago

I've just created new pull request replacing this one: https://github.com/python-openxml/python-docx/pull/1386