-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic support for adding SVG pictures to docx files
- Loading branch information
Showing
6 changed files
with
96 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# encoding: utf-8 | ||
|
||
from __future__ import absolute_import, division, print_function | ||
|
||
import xml.etree.ElementTree as ET | ||
|
||
from .constants import MIME_TYPE | ||
from .image import BaseImageHeader | ||
|
||
|
||
class Svg(BaseImageHeader): | ||
""" | ||
Image header parser for SVG images. | ||
""" | ||
|
||
@classmethod | ||
def from_stream(cls, stream): | ||
""" | ||
Return |Svg| instance having header properties parsed from SVG image | ||
in *stream*. | ||
""" | ||
px_width, px_height = cls._dimensions_from_stream(stream) | ||
return cls(px_width, px_height, 72, 72) | ||
|
||
@property | ||
def content_type(self): | ||
""" | ||
MIME content type for this image, unconditionally `image/svg+xml` for | ||
SVG images. | ||
""" | ||
return MIME_TYPE.SVG | ||
|
||
@property | ||
def default_ext(self): | ||
""" | ||
Default filename extension, always 'svg' for SVG images. | ||
""" | ||
return "svg" | ||
|
||
@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 = int(root.attrib["width"]) | ||
height = int(root.attrib["height"]) | ||
return width, height |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters