diff --git a/docxtpl/inline_image.py b/docxtpl/inline_image.py index f860749..e369d3f 100644 --- a/docxtpl/inline_image.py +++ b/docxtpl/inline_image.py @@ -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 @@ -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 @@ -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 ( "%s" - '' % pic + '' % pic.xml ) def __unicode__(self):