Skip to content

Commit

Permalink
Added option to set spacing of InlineImage
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmarcosdarosa committed Oct 23, 2024
1 parent 0607e71 commit f2b6508
Showing 1 changed file with 24 additions and 9 deletions.
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

0 comments on commit f2b6508

Please sign in to comment.