Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
takis wants to merge0 commits intopython-openxml:masterfromtakis:master

Conversation

takis
Copy link

See issues#351,#651,#659.

sb-eip, rojikada, JaverHumberto, and IvanDoganov reacted with thumbs up emoji
@takis
Copy link
Author

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

importdatetimefromdocximportDocumentSVG="""<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"withopen(svg_filename,"w")asf: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")
pcko1 and FrankBold reacted with thumbs up emoji

@liam-veitch
Copy link

liam-veitch commentedAug 3, 2022
edited
Loading

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:
<svg baseProfile='full' height='100.000000%' version='1.1' viewBox='0.000000 0.000000 1240.000000 1753.000000' width='100.000000%' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>

in svg.py:

    @classmethod    def _dimensions_from_stream(cls, stream):        stream.seek(0)        data = stream.read()        root = ET.fromstring(data)        # FIXME: The width could be expressed as '4cm'        # See https://www.w3.org/TR/SVG11/struct.html#NewDocument        width_str = root.attrib["width"]        height_str = root.attrib["height"]        if width_str.endswith(r'%') and height_str.endswith(r'%'):            vb_lims = str(root.attrib["viewBox"]).split(' ')            width_scale = float(width_str[:-1]) * 0.01 * 72/96            height_scale = float(height_str[:-1]) * 0.01 * 72/96            width = int(width_scale * float(vb_lims[2]))            height = int(height_scale * float(vb_lims[3]))        elif width_str.endswith('pt') and height_str.endswith('pt'):            width = int(float(width_str[:-2]))            height = int(float(height_str[:-2]))        else:            width = int(width_str)            height = int(height_str)        return width, height

@JaverHumberto
Copy link

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.

BenoitMarchand, fjtcin, killia15, and Kladdy reacted with thumbs up emoji

@Kladdy
Copy link

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 calleddocx_svg_patch.py, and at the top of your own python script you haveimport docx_svg_patch. I haven't tested this extensively, and I am not sure how well this plays with other image types now, but it's at least a start.

Link to gist:https://gist.github.com/Kladdy/d3bdb9bbf2c38d4f194ea9a7904fc3f2

@takis
Copy link
Author

I've created a new pull request for this, as there were many conflicts with the updated code base:
#1343

@takis
Copy link
Author

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.

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:
https://github.com/takis/python-docx/

JaverHumberto reacted with thumbs up emoji

@takis
Copy link
Author

Hi@Kladdy

I've updated the pull request and I'm also still hoping that it will get merged one day.

Kladdy and Joko75 reacted with thumbs up emoji

@goomesthiago
Copy link

Do you have any updates on this?
The SVG files are still not compatible with python-docx?

@takis
Copy link
Author

Hi@goomesthiago

Unfortunately not, but I do try to keep my fork in sync with the main repository.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

5 participants
@takis@liam-veitch@JaverHumberto@Kladdy@goomesthiago

[8]ページ先頭

©2009-2025 Movatter.jp