Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 0 commits into from

Conversation

takis
Copy link

@takis takis commented Feb 22, 2024

See issues #351, #651, #659.

This replaces this pull request from two years ago:
#1107

@takis
Copy link
Author

takis commented Feb 22, 2024

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
Copy link

mzaanen commented Mar 11, 2024

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
Copy link

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
Copy link
Author

takis commented May 2, 2024

I've just created new pull request replacing this one:
#1386

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants