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 hyperlink option in InlineImage #552

Merged
Merged
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
35 changes: 32 additions & 3 deletions docxtpl/inline_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

@author: Eric Lapouyade
"""

from docx.oxml import OxmlElement, parse_xml
from docx.oxml.ns import qn

class InlineImage(object):
"""Class to generate an inline image
Expand All @@ -15,17 +16,45 @@ class InlineImage(object):
image_descriptor = None
width = None
height = None
anchor = None

def __init__(self, tpl, image_descriptor, width=None, height=None):
def __init__(self, tpl, image_descriptor, width=None, height=None, anchor=None):
self.tpl, self.image_descriptor = tpl, image_descriptor
self.width, self.height = width, height
self.anchor = anchor

def _add_hyperlink(self, run, url, part):
# Create a relationship for the hyperlink
r_id = part.relate_to(url, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink', is_external=True)

# Find the <wp:docPr> and <pic:cNvPr> element
docPr = run.xpath('.//wp:docPr')[0]
cNvPr = run.xpath('.//pic:cNvPr')[0]

# Create the <a:hlinkClick> element
hlinkClick1 = OxmlElement('a:hlinkClick')
hlinkClick1.set(qn('r:id'), r_id)
hlinkClick2 = OxmlElement('a:hlinkClick')
hlinkClick2.set(qn('r:id'), r_id)

# Insert the <a:hlinkClick> element right after the <wp:docPr> element
docPr.append(hlinkClick1)
cNvPr.append(hlinkClick2)

return run

def _insert_image(self):
pic = self.tpl.current_rendering_part.new_pic_inline(
self.image_descriptor,
self.width,
self.height
self.height,
).xml
if self.anchor:
run = parse_xml(pic)
if run.xpath('.//a:blip'):
hyperlink = self._add_hyperlink(run, self.anchor, self.tpl.current_rendering_part)
pic = hyperlink.xml

return '</w:t></w:r><w:r><w:drawing>%s</w:drawing></w:r><w:r>' \
'<w:t xml:space="preserve">' % pic

Expand Down
Loading