This commit is contained in:
Eric Lapouyade 2018-11-18 17:56:14 +01:00
parent b0560c87b9
commit 20e30c541c
82 changed files with 68 additions and 57 deletions

1
.gitignore vendored
View File

@ -21,6 +21,7 @@ var/
*.egg-info/
.installed.cfg
*.egg
tests/output/*
# PyInstaller
# Usually these files are written by a python script from a template

View File

@ -1,3 +1,8 @@
0.5.9 (2018-11-18)
------------------
- Add {% hm %} tag for table columns horizontal merging (Thanks to nickgashkov)
- Split tests/tests_files dir into templates and output dirs
0.5.8 (2018-11-08)
------------------
- autoescape support for python 2.7

View File

@ -5,7 +5,7 @@ Created : 2015-03-12
@author: Eric Lapouyade
'''
__version__ = '0.5.8'
__version__ = '0.5.9'
from lxml import etree
from docx import Document

View File

@ -7,7 +7,7 @@ Created : 2015-03-12
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('test_files/cellbg_tpl.docx')
tpl=DocxTemplate('templates/cellbg_tpl.docx')
context = {
'alerts' : [
@ -19,4 +19,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/cellbg.docx')
tpl.save('output/cellbg.docx')

View File

@ -1,6 +1,6 @@
from docxtpl import DocxTemplate
tpl=DocxTemplate('test_files/dynamic_table_tpl.docx')
tpl=DocxTemplate('templates/dynamic_table_tpl.docx')
context = {
'col_labels' : ['fruit', 'vegetable', 'stone', 'thing'],
@ -12,4 +12,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/dynamic_table.docx')
tpl.save('output/dynamic_table.docx')

View File

@ -8,22 +8,22 @@ Created : 2017-09-09
from docxtpl import DocxTemplate
# rendering the "dynamic embedded docx":
embedded_docx_tpl=DocxTemplate('test_files/embedded_embedded_docx_tpl.docx')
embedded_docx_tpl=DocxTemplate('templates/embedded_embedded_docx_tpl.docx')
context = {
'name' : 'John Doe',
}
embedded_docx_tpl.render(context)
embedded_docx_tpl.save('test_files/embedded_embedded_docx.docx')
embedded_docx_tpl.save('output/embedded_embedded_docx.docx')
# rendring the main document :
tpl=DocxTemplate('test_files/embedded_main_tpl.docx')
tpl=DocxTemplate('templates/embedded_main_tpl.docx')
context = {
'name' : 'John Doe',
}
tpl.replace_embedded('test_files/embedded_dummy.docx','test_files/embedded_static_docx.docx')
tpl.replace_embedded('test_files/embedded_dummy2.docx','test_files/embedded_embedded_docx.docx')
tpl.replace_embedded('templates/embedded_dummy.docx','templates/embedded_static_docx.docx')
tpl.replace_embedded('templates/embedded_dummy2.docx','templates/embedded_embedded_docx.docx')
tpl.render(context)
tpl.save('test_files/embedded.docx')
tpl.save('output/embedded.docx')

View File

@ -1,6 +1,6 @@
from docxtpl import *
tpl = DocxTemplate("test_files/escape_tpl.docx")
tpl = DocxTemplate('templates/escape_tpl.docx')
context = {'myvar': R('"less than" must be escaped : <, this can be done with RichText() or R()'),
'myescvar':'It can be escaped with a "|e" jinja filter in the template too : < ',
@ -9,4 +9,4 @@ context = {'myvar': R('"less than" must be escaped : <, this can be done with Ri
}
tpl.render(context)
tpl.save("test_files/escape.docx")
tpl.save('output/escape.docx')

View File

@ -1,6 +1,6 @@
from docxtpl import *
tpl = DocxTemplate("test_files/escape_tpl_auto.docx")
tpl = DocxTemplate('templates/escape_tpl_auto.docx')
context = {'myvar': R('"less than" must be escaped : <, this can be done with RichText() or R()'),
'myescvar': 'It can be escaped with a "|e" jinja filter in the template too : < ',
@ -10,4 +10,4 @@ context = {'myvar': R('"less than" must be escaped : <, this can be done with Ri
}
tpl.render(context, autoescape=True)
tpl.save("test_files/escape_auto.docx")
tpl.save('output/escape_auto.docx')

View File

@ -7,7 +7,7 @@ Created : 2015-03-12
from docxtpl import DocxTemplate
tpl=DocxTemplate('test_files/header_footer_tpl.docx')
tpl=DocxTemplate('templates/header_footer_tpl.docx')
sd = tpl.new_subdoc()
p = sd.add_paragraph('This is a sub-document to check it does not break header and footer')
@ -20,4 +20,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/header_footer.docx')
tpl.save('output/header_footer.docx')

View File

@ -7,11 +7,11 @@ Created : 2015-03-12
from docxtpl import DocxTemplate
tpl=DocxTemplate('test_files/header_footer_entities_tpl.docx')
tpl=DocxTemplate('templates/header_footer_entities_tpl.docx')
context = {
'title' : 'Header and footer test',
}
tpl.render(context)
tpl.save('test_files/header_footer_entities.docx')
tpl.save('output/header_footer_entities.docx')

View File

@ -7,13 +7,13 @@ Created : 2017-09-03
from docxtpl import DocxTemplate
DEST_FILE = 'test_files/header_footer_image.docx'
DEST_FILE = 'output/header_footer_image.docx'
tpl=DocxTemplate('test_files/header_footer_image_tpl.docx')
tpl=DocxTemplate('templates/header_footer_image_tpl.docx')
context = {
'mycompany' : 'The World Wide company',
}
tpl.replace_media('test_files/dummy_pic_for_header.png','test_files/python.png')
tpl.replace_media('templates/dummy_pic_for_header.png','templates/python.png')
tpl.render(context)
tpl.save(DEST_FILE)

View File

@ -9,7 +9,7 @@ Edited : 2016-07-19 by Eric Lapouyade
from docxtpl import DocxTemplate
tpl=DocxTemplate('test_files/header_footer_tpl_utf8.docx')
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')
@ -22,4 +22,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/header_footer_utf8.docx')
tpl.save('output/header_footer_utf8.docx')

View File

@ -2,6 +2,6 @@
from docxtpl import DocxTemplate
tpl = DocxTemplate('test_files/horizontal_merge_tpl.docx')
tpl = DocxTemplate('templates/horizontal_merge_tpl.docx')
tpl.render({})
tpl.save('test_files/horizontal_merge.docx')
tpl.save('output/horizontal_merge.docx')

View File

@ -11,29 +11,29 @@ from docx.shared import Mm, Inches, Pt
import jinja2
from jinja2.utils import Markup
tpl=DocxTemplate('test_files/inline_image_tpl.docx')
tpl=DocxTemplate('templates/inline_image_tpl.docx')
context = {
'myimage' : InlineImage(tpl,'test_files/python_logo.png',width=Mm(20)),
'myimageratio': InlineImage(tpl, 'test_files/python_jpeg.jpg', width=Mm(30), height=Mm(60)),
'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,'test_files/django.png',height=Mm(10)),
'frameworks' : [{'image' : InlineImage(tpl,'templates/django.png',height=Mm(10)),
'desc' : 'The web framework for perfectionists with deadlines'},
{'image' : InlineImage(tpl,'test_files/zope.png',height=Mm(10)),
{'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, 'test_files/pyramid.png', height=Mm(10)),
{'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,'test_files/bottle.png',height=Mm(10)),
{'image' : InlineImage(tpl,'templates/bottle.png',height=Mm(10)),
'desc' : 'Bottle is a fast, simple and lightweight WSGI micro web-framework for Python'},
{'image': InlineImage(tpl, 'test_files/tornado.png', height=Mm(10)),
{'image': InlineImage(tpl, 'templates/tornado.png', height=Mm(10)),
'desc': 'Tornado is a Python web framework and asynchronous networking library.'},
]
}
# testing that it works also when autoescape has been forced to True
jinja_env = jinja2.Environment(autoescape=True)
tpl.render(context, jinja_env)
tpl.save('test_files/inline_image.docx')
tpl.save('output/inline_image.docx')

View File

@ -7,7 +7,7 @@ Created : 2016-03-26
from docxtpl import DocxTemplate
tpl=DocxTemplate('test_files/nested_for_tpl.docx')
tpl=DocxTemplate('templates/nested_for_tpl.docx')
context = {
'dishes' : [
@ -30,4 +30,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/nested_for.docx')
tpl.save('output/nested_for.docx')

View File

@ -7,7 +7,7 @@ Created : 2015-03-12
from docxtpl import DocxTemplate
tpl=DocxTemplate('test_files/order_tpl.docx')
tpl=DocxTemplate('templates/order_tpl.docx')
context = {
'customer_name' : 'Eric',
@ -23,4 +23,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/order.docx')
tpl.save('output/order.docx')

View File

@ -7,12 +7,12 @@ Created : 2017-09-03
from docxtpl import DocxTemplate
DEST_FILE = 'test_files/replace_picture.docx'
DEST_FILE = 'output/replace_picture.docx'
tpl=DocxTemplate('test_files/replace_picture_tpl.docx')
tpl=DocxTemplate('templates/replace_picture_tpl.docx')
context = {}
tpl.replace_pic('python_logo.png','test_files/python.png')
tpl.replace_pic('python_logo.png','templates/python.png')
tpl.render(context)
tpl.save(DEST_FILE)

View File

@ -7,7 +7,7 @@ Created : 2015-03-26
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('test_files/richtext_tpl.docx')
tpl=DocxTemplate('templates/richtext_tpl.docx')
rt = RichText('an exemple of ')
rt.add('a rich text', style='myrichtextstyle')
@ -43,4 +43,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/richtext.docx')
tpl.save('output/richtext.docx')

View File

@ -7,7 +7,7 @@ Created : 2015-03-26
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('test_files/richtext_and_if_tpl.docx')
tpl=DocxTemplate('templates/richtext_and_if_tpl.docx')
context = {
@ -15,4 +15,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/richtext_and_if.docx')
tpl.save('output/richtext_and_if.docx')

View File

@ -1,10 +1,15 @@
import subprocess
import glob
import six
import os
tests = sorted(glob.glob('[A-Za-z]*.py'))
excludes = ['runtests.py']
output_dir = os.path.join(os.path.dirname(__file__),'output')
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for test in tests:
if test not in excludes:
six.print_('%s ...' % test)

View File

@ -8,7 +8,7 @@ Created : 2015-03-12
from docxtpl import DocxTemplate
from docx.shared import Inches
tpl=DocxTemplate('test_files/subdoc_tpl.docx')
tpl=DocxTemplate('templates/subdoc_tpl.docx')
sd = tpl.new_subdoc()
p = sd.add_paragraph('This is a sub-document inserted into a bigger one')
@ -22,7 +22,7 @@ sd.add_heading('Heading, level 1', level=1)
sd.add_paragraph('This is an Intense quote', style='IntenseQuote')
sd.add_paragraph('A picture :')
sd.add_picture('test_files/python_logo.png', width=Inches(1.25))
sd.add_picture('templates/python_logo.png', width=Inches(1.25))
sd.add_paragraph('A Table :')
table = sd.add_table(rows=1, cols=3)
@ -44,4 +44,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/subdoc.docx')
tpl.save('output/subdoc.docx')

View File

@ -6,7 +6,7 @@ six.print_('=' * 80)
six.print_("Generating template error for testing (so it is safe to ignore) :")
six.print_('.' * 80)
try:
tpl = DocxTemplate('test_files/template_error_tpl.docx')
tpl = DocxTemplate('templates/template_error_tpl.docx')
tpl.render({
'test_variable' : 'test variable value'
})
@ -16,7 +16,7 @@ except TemplateError as the_error:
six.print_("Context:")
for line in the_error.docx_context:
six.print_(line)
tpl.save('test_files/template_error.docx')
tpl.save('output/template_error.docx')
six.print_('.' * 80)
six.print_(" End of TemplateError Test ")
six.print_('=' * 80)

View File

Before

Width:  |  Height:  |  Size: 7.1 KiB

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

Before

Width:  |  Height:  |  Size: 7.1 KiB

After

Width:  |  Height:  |  Size: 7.1 KiB

View File

Before

Width:  |  Height:  |  Size: 4.6 KiB

After

Width:  |  Height:  |  Size: 4.6 KiB

View File

Before

Width:  |  Height:  |  Size: 9.8 KiB

After

Width:  |  Height:  |  Size: 9.8 KiB

View File

Before

Width:  |  Height:  |  Size: 176 KiB

After

Width:  |  Height:  |  Size: 176 KiB

View File

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 46 KiB

View File

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.2 KiB

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -7,7 +7,7 @@ Created : 2017-10-15
from docxtpl import DocxTemplate
tpl=DocxTemplate('test_files/vertical_merge_tpl.docx')
tpl=DocxTemplate('templates/vertical_merge_tpl.docx')
context = {
'items' : [
@ -20,4 +20,4 @@ context = {
}
tpl.render(context)
tpl.save('test_files/vertical_merge.docx')
tpl.save('output/vertical_merge.docx')

View File

@ -1,5 +1,5 @@
from docxtpl import DocxTemplate
tpl = DocxTemplate('test_files/vertical_merge_nested_tpl.docx')
tpl = DocxTemplate('templates/vertical_merge_nested_tpl.docx')
tpl.render({})
tpl.save('test_files/vertical_merge_nested.docx')
tpl.save('output/vertical_merge_nested.docx')

View File

@ -1,10 +1,10 @@
from docxtpl import DocxTemplate, RichText
tpl = DocxTemplate('test_files/word2016_tpl.docx')
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('test_files/word2016.docx')
tpl.save('output/word2016.docx')