Add basic support for adding SVG pictures to docx files#1107
Add basic support for adding SVG pictures to docx files#1107takis wants to merge 0 commit intopython-openxml:masterfrom
Conversation
|
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") |
|
Hi @takis thanks for the improvement - for my SVG files there are relative scalings in the width and height attributes. I had to take the data from the viewbox attribute. This is a complete 'hack' and probably does not respect SVG specification regarding pt/pixel/ppi etc. Example svg header: in svg.py: |
|
Hi @takis, I'm new using docx (and python), and I'm tryng to load some SVG images in a word document, looking for that I found this, but try to replicate the code you were testing with and it doesn't work for me, I get the docx.image.exceptions.UnrecognizedImageError. So it isn't "included" in docx now? if not, is there a way for using your code? thanks in advance, and sorry if it's not a place for asking this, i'll remove the comment if needed. |
|
This would be amazing if it could be merged into a new release. In the meantime, if somebody wants to play around with SVG files, I created a monkey patch just as was made in the case for EMF files: #196 (comment). Just add the code from the gist below to a file called Link to gist: https://gist.github.com/Kladdy/d3bdb9bbf2c38d4f194ea9a7904fc3f2 |
|
I've created a new pull request for this, as there were many conflicts with the updated code base: |
Hi @JaverHumberto, no, unfortunately, the code was not merged. I've created a new pull request and updated the code though. If you want to try it, you can use my fork for now: |
|
Hi @Kladdy I've updated the pull request and I'm also still hoping that it will get merged one day. |
|
Do you have any updates on this? |
|
Unfortunately not, but I do try to keep my fork in sync with the main repository. |
See issues #351, #651, #659.