InlineImage now possible in header/footer
This commit is contained in:
parent
6496abf307
commit
30578cf88c
@ -1,3 +1,7 @@
|
|||||||
|
0.11.4 (2021-04-06)
|
||||||
|
-------------------
|
||||||
|
- It is now possible to put InlineImage in header/footer
|
||||||
|
|
||||||
0.11.2 (2020-11-09)
|
0.11.2 (2020-11-09)
|
||||||
-------------------
|
-------------------
|
||||||
- fix #323
|
- fix #323
|
||||||
|
|||||||
@ -4,7 +4,7 @@ Created : 2015-03-12
|
|||||||
|
|
||||||
@author: Eric Lapouyade
|
@author: Eric Lapouyade
|
||||||
"""
|
"""
|
||||||
__version__ = '0.11.3'
|
__version__ = '0.11.4'
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
import io
|
import io
|
||||||
@ -41,6 +41,7 @@ class DocxTemplate(object):
|
|||||||
self.zipname_to_replace = {}
|
self.zipname_to_replace = {}
|
||||||
self.pic_to_replace = {}
|
self.pic_to_replace = {}
|
||||||
self.pic_map = {}
|
self.pic_map = {}
|
||||||
|
self.current_rendering_part = None
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
return getattr(self.docx, name)
|
return getattr(self.docx, name)
|
||||||
@ -208,9 +209,7 @@ class DocxTemplate(object):
|
|||||||
|
|
||||||
return src_xml
|
return src_xml
|
||||||
|
|
||||||
current_rendering_part = None
|
def render_xml_part(self, src_xml, part, context, jinja_env=None):
|
||||||
|
|
||||||
def render_xml(self, src_xml, part, context, jinja_env=None):
|
|
||||||
src_xml = src_xml.replace(r'<w:p>', '\n<w:p>')
|
src_xml = src_xml.replace(r'<w:p>', '\n<w:p>')
|
||||||
try:
|
try:
|
||||||
self.current_rendering_part = part
|
self.current_rendering_part = part
|
||||||
@ -219,7 +218,6 @@ class DocxTemplate(object):
|
|||||||
else:
|
else:
|
||||||
template = Template(src_xml)
|
template = Template(src_xml)
|
||||||
dst_xml = template.render(context)
|
dst_xml = template.render(context)
|
||||||
self.current_rendering_part = None
|
|
||||||
except TemplateError as exc:
|
except TemplateError as exc:
|
||||||
if hasattr(exc, 'lineno') and exc.lineno is not None:
|
if hasattr(exc, 'lineno') and exc.lineno is not None:
|
||||||
line_number = max(exc.lineno - 4, 0)
|
line_number = max(exc.lineno - 4, 0)
|
||||||
@ -270,7 +268,7 @@ class DocxTemplate(object):
|
|||||||
def build_xml(self, context, jinja_env=None):
|
def build_xml(self, context, jinja_env=None):
|
||||||
xml = self.get_xml()
|
xml = self.get_xml()
|
||||||
xml = self.patch_xml(xml)
|
xml = self.patch_xml(xml)
|
||||||
xml = self.render_xml(xml, self.docx._part, context, jinja_env)
|
xml = self.render_xml_part(xml, self.docx._part, context, jinja_env)
|
||||||
return xml
|
return xml
|
||||||
|
|
||||||
def map_tree(self, tree):
|
def map_tree(self, tree):
|
||||||
@ -297,7 +295,7 @@ class DocxTemplate(object):
|
|||||||
xml = self.get_part_xml(part)
|
xml = self.get_part_xml(part)
|
||||||
encoding = self.get_headers_footers_encoding(xml)
|
encoding = self.get_headers_footers_encoding(xml)
|
||||||
xml = self.patch_xml(xml)
|
xml = self.patch_xml(xml)
|
||||||
xml = self.render_xml(xml, part, context, jinja_env)
|
xml = self.render_xml_part(xml, part, context, jinja_env)
|
||||||
yield relKey, xml.encode(encoding)
|
yield relKey, xml.encode(encoding)
|
||||||
|
|
||||||
def map_headers_footers_xml(self, relKey, xml):
|
def map_headers_footers_xml(self, relKey, xml):
|
||||||
|
|||||||
19
tests/header_footer_inline_image.py
Normal file
19
tests/header_footer_inline_image.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
'''
|
||||||
|
Created : 2021-04-06
|
||||||
|
|
||||||
|
@author: Eric Lapouyade
|
||||||
|
'''
|
||||||
|
|
||||||
|
from docxtpl import DocxTemplate, InlineImage
|
||||||
|
|
||||||
|
# for height and width you have to use millimeters (Mm), inches or points(Pt) class :
|
||||||
|
from docx.shared import Mm
|
||||||
|
|
||||||
|
tpl = DocxTemplate('templates/header_footer_inline_image_tpl.docx')
|
||||||
|
|
||||||
|
context = {
|
||||||
|
'inline_image': InlineImage(tpl, 'templates/django.png', height=Mm(10))
|
||||||
|
}
|
||||||
|
tpl.render(context)
|
||||||
|
tpl.save('output/header_footer_inline_image.docx')
|
||||||
BIN
tests/templates/header_footer_inline_image_tpl.docx
Normal file
BIN
tests/templates/header_footer_inline_image_tpl.docx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user