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

Added spacing attrs to InlineImage #567

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions docxtpl/inline_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@author: Eric Lapouyade
"""
from docx.oxml import OxmlElement, parse_xml
from docx.oxml import OxmlElement
from docx.oxml.ns import qn


Expand All @@ -19,11 +19,20 @@ class InlineImage(object):
width = None
height = None
anchor = None
spacing_left = None
spacing_right = None
spacing_top = None
spacing_bottom = None

def __init__(self, tpl, image_descriptor, width=None, height=None, anchor=None):
def __init__(self, tpl, image_descriptor, width=None, height=None, anchor=None,
spacing_left=None, spacing_right=None, spacing_top=None, spacing_bottom=None):
self.tpl, self.image_descriptor = tpl, image_descriptor
self.width, self.height = width, height
self.anchor = anchor
self.spacing_left = spacing_left
self.spacing_right = spacing_right
self.spacing_top = spacing_top
self.spacing_bottom = spacing_bottom

def _add_hyperlink(self, run, url, part):
# Create a relationship for the hyperlink
Expand Down Expand Up @@ -54,18 +63,24 @@ def _insert_image(self):
self.image_descriptor,
self.width,
self.height,
).xml
)
if self.spacing_top is not None:
pic.set('distT', str(self.spacing_top))
if self.spacing_bottom is not None:
pic.set('distB', str(self.spacing_bottom))
if self.spacing_left is not None:
pic.set('distL', str(self.spacing_left))
if self.spacing_right is not None:
pic.set('distR', str(self.spacing_right))
if self.anchor:
run = parse_xml(pic)
if run.xpath(".//a:blip"):
if pic.xpath(".//a:blip"):
hyperlink = self._add_hyperlink(
run, self.anchor, self.tpl.current_rendering_part
pic, self.anchor, self.tpl.current_rendering_part
)
pic = hyperlink.xml

pic = hyperlink
return (
"</w:t></w:r><w:r><w:drawing>%s</w:drawing></w:r><w:r>"
'<w:t xml:space="preserve">' % pic
'<w:t xml:space="preserve">' % pic.xml
)

def __unicode__(self):
Expand Down
Loading