Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# -*- coding: utf-8 -*-
from docxtpl import DocxTemplate
tpl = DocxTemplate('templates/horizontal_merge_tpl.docx')
tpl.render({})
tpl.save('output/horizontal_merge.docx')
DEST_FILE = 'output/header_footer_image_file_obj.docx'
DEST_FILE2 = 'output/header_footer_image_file_obj2.docx'
tpl=DocxTemplate('templates/header_footer_image_tpl.docx')
context = {
'mycompany' : 'The World Wide company',
}
dummy_pic = io.BytesIO(open('templates/dummy_pic_for_header.png', 'rb').read())
new_image = io.BytesIO(open('templates/python.png', 'rb').read())
tpl.replace_media(dummy_pic, new_image)
tpl.render(context)
tpl.save(DEST_FILE)
tpl = DocxTemplate('templates/header_footer_image_tpl.docx')
dummy_pic.seek(0)
new_image.seek(0)
tpl.replace_media(dummy_pic, new_image)
tpl.render(context)
file_obj = io.BytesIO()
tpl.save(file_obj)
file_obj.seek(0)
with open(DEST_FILE2, 'wb') as f:
f.write(file_obj.read())
# -*- coding: utf-8 -*-
'''
Created : 2016-07-19
@author: AhnSeongHyun
Edited : 2016-07-19 by Eric Lapouyade
'''
from docxtpl import DocxTemplate
tpl=DocxTemplate('templates/header_footer_tpl_utf8.docx')
sd = tpl.new_subdoc()
p = sd.add_paragraph(u'This is a sub-document to check it does not break header and footer with utf-8 characters inside the template .docx')
context = {
'title' : u'헤더와 푸터',
'company_name' : u'세계적 회사',
'date' : u'2016-03-17',
'mysubdoc' : sd,
}
tpl.render(context)
tpl.save('output/header_footer_utf8.docx')
from docxtpl import DocxTemplate, RichText
tpl = DocxTemplate('templates/word2016_tpl.docx')
tpl.render({
'test_space' : ' ',
'test_tabs': 5*'\t',
'test_space_r' : RichText(' '),
'test_tabs_r': RichText(5*'\t'),
})
tpl.save('output/word2016.docx')
from docxtpl import DocxTemplate, RichText
from jinja2.exceptions import TemplateError
import six
six.print_('=' * 80)
six.print_("Generating template error for testing (so it is safe to ignore) :")
six.print_('.' * 80)
try:
tpl = DocxTemplate('templates/template_error_tpl.docx')
tpl.render({
'test_variable' : 'test variable value'
})
except TemplateError as the_error:
six.print_(six.text_type(the_error))
if hasattr(the_error, 'docx_context'):
six.print_("Context:")
for line in the_error.docx_context:
six.print_(line)
tpl.save('output/template_error.docx')
six.print_('.' * 80)
six.print_(" End of TemplateError Test ")
six.print_('=' * 80)
# -*- coding: utf-8 -*-
'''
Created : 2017-01-14
@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, Inches, Pt
import jinja2
from jinja2.utils import Markup
tpl=DocxTemplate('templates/inline_image_tpl.docx')
context = {
'myimage' : InlineImage(tpl,'templates/python_logo.png',width=Mm(20)),
'myimageratio': InlineImage(tpl, 'templates/python_jpeg.jpg', width=Mm(30), height=Mm(60)),
'frameworks' : [{'image' : InlineImage(tpl,'templates/django.png',height=Mm(10)),
'desc' : 'The web framework for perfectionists with deadlines'},
{'image' : InlineImage(tpl,'templates/zope.png',height=Mm(10)),
'desc' : 'Zope is a leading Open Source Application Server and Content Management Framework'},
{'image': InlineImage(tpl, 'templates/pyramid.png', height=Mm(10)),
'desc': 'Pyramid is a lightweight Python web framework aimed at taking small web apps into big web apps.'},
{'image' : InlineImage(tpl,'templates/bottle.png',height=Mm(10)),
'desc' : 'Bottle is a fast, simple and lightweight WSGI micro web-framework for Python'},
# -*- coding: utf-8 -*-
'''
Created : 2015-03-12
@author: Eric Lapouyade
'''
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('templates/cellbg_tpl.docx')
context = {
'alerts' : [
{'date' : '2015-03-10', 'desc' : RichText('Very critical alert',color='FF0000', bold=True), 'type' : 'CRITICAL', 'bg': 'FF0000' },
{'date' : '2015-03-11', 'desc' : RichText('Just a warning'), 'type' : 'WARNING', 'bg': 'FFDD00' },
{'date' : '2015-03-12', 'desc' : RichText('Information'), 'type' : 'INFO', 'bg': '8888FF' },
{'date' : '2015-03-13', 'desc' : RichText('Debug trace'), 'type' : 'DEBUG', 'bg': 'FF00FF' },
],
}
tpl.render(context)
tpl.save('output/cellbg.docx')
# -*- coding: utf-8 -*-
'''
Created : 2015-03-12
@author: Eric Lapouyade
'''
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('templates/cellbg_tpl.docx')
context = {
'alerts' : [
{'date' : '2015-03-10', 'desc' : RichText('Very critical alert',color='FF0000', bold=True), 'type' : 'CRITICAL', 'bg': 'FF0000' },
{'date' : '2015-03-11', 'desc' : RichText('Just a warning'), 'type' : 'WARNING', 'bg': 'FFDD00' },
{'date' : '2015-03-12', 'desc' : RichText('Information'), 'type' : 'INFO', 'bg': '8888FF' },
{'date' : '2015-03-13', 'desc' : RichText('Debug trace'), 'type' : 'DEBUG', 'bg': 'FF00FF' },
],
}
tpl.render(context)
tpl.save('output/cellbg.docx')
# -*- coding: utf-8 -*-
'''
Created : 2015-03-26
@author: Eric Lapouyade
'''
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('templates/richtext_tpl.docx')
rt = RichText()
rt.add('a rich text', style='myrichtextstyle')
rt.add(' with ')
rt.add('some italic', italic=True)
rt.add(' and ')
rt.add('some violet', color='#ff00ff')
rt.add(' and ')
rt.add('some striked', strike=True)
rt.add(' and ')
rt.add('some small', size=14)
rt.add(' or ')
rt.add('big', size=60)
rt.add(' text.')
rt.add('\nYou can add an hyperlink, here to ')
rt.add('google',url_id=tpl.build_url_id('http://google.com'))
rt.add('\nEt voilà ! ')
rt.add('\n1st line')
from docxtpl import DocxTemplate, RichText
tpl = DocxTemplate('templates/word2016_tpl.docx')
tpl.render({
'test_space' : ' ',
'test_tabs': 5*'\t',
'test_space_r' : RichText(' '),
'test_tabs_r': RichText(5*'\t'),
})
tpl.save('output/word2016.docx')