-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Some hack to include SVG #798
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
base: develop
Are you sure you want to change the base?
Some hack to include SVG #798
Conversation
Have tried this(https://github.com/pfernique/python-docx/tree/feature/add_svg_picture) with word 2013. |
Unfortunatly SVG works only on word >= 2016 (more details here) |
Thank you for the link. I think that is not very important which vector format to use. def _dimensions_from_stream(cls, stream):
#https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wmf/828e1864-7fe7-42d8-ab0a-1de161b32f27
stream.seek(0)
META_PLACEABLE = stream.read()
print("META_PLACEABLE")
mkey = META_PLACEABLE[0:4] # Key (4 bytes): Identification value that indicates the presence of a placeable metafile header. This value MUST be 0x9AC6CDD7.
mhmw = META_PLACEABLE[4:6] # HWmf (2 bytes): The resource handle to the metafile, when the metafile is in memory. When the metafile is on disk, this field MUST contain 0x0000. This attribute of the metafile is specified in the Type field of the META_HEADER Record.
mbbox = META_PLACEABLE[6:14] # BoundingBox (8 bytes): The rectangle in the playback context (or simply the destination rectangle), measured in logical units, for displaying the metafile. The size of a logical unit is specified by the Inch field.
minch = META_PLACEABLE[14:16] # Inch (2 bytes): The number of logical units per inch used to represent the image. This value can be used to scale an image.
|
Merge at some point? |
Would like to see this merged. |
@@ -81,7 +80,10 @@ def new_pic_inline(cls, shape_id, rId, filename, cx, cy): | |||
specified by the argument values. | |||
""" | |||
pic_id = 0 # Word doesn't seem to use this, but does not omit it | |||
pic = CT_Picture.new(pic_id, filename, rId, cx, cy) | |||
if filename.endswith('.svg'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm misreading this - this would only capture SVG is actually provided as a file (that has a filename
go with it). There are many use cases where pictures are generated on the fly in in-memory buffers and therefore not provided as files. Any way to account for that?
(For example the PNG equivalent for matplotlib + docx would be something like:
import base64
import io
import docx
from docx.shared import Inches
import matplotlib.figure as figure
fig = figure.Figure()
ax = fig.subplots()
ax.plot([1, 2])
buf = io.BytesIO()
fig.savefig(buf, format='png')
document = docx.Document()
document.add_heading('Document Title', 0)
document.add_picture(buf, width=Inches(1.25))
document.save('demo.docx')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true that this seems strange but when dispatch is done, a filename image.svg
is given for a stream (must be the same for png
). So even if you provide in-memory buffers, it's good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't realize that - thank you for calling it out. I looked a bit further (which I should have from the beginning):filename is generated here in image.py
.
Definitely looking forward to this. Word now supports SVG natively, i.e. images are imported as editable vector objects. It's converted to DrawingML in the docx file with a reference to the embedded SVG file: <asvg:svgBlip xmlns:asvg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" r:embed="rId5"/> Then, in Word, it can be converted into an actual shape, and the SVG file is removed from the docx media to be replaced with the usual |
4762810
to
0a8e9c4
Compare
shape.py
andpkgwriter.py
are particularly ugly but it works with Word for Office 365 (16.0)