diff --git a/CHANGES.rst b/CHANGES.rst index 6d30e3d..39efbc1 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,7 @@ +0.1.9 (2016-1-18) +----------------- +- better subdoc management : accept tables + 0.1.8 (2015-11-05) ------------------ - better xml code cleaning around Jinja2 tags diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index ae6f821..01b1ff1 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -5,7 +5,7 @@ Created : 2015-03-12 @author: Eric Lapouyade ''' -__version__ = '0.1.8' +__version__ = '0.1.9' from lxml import etree from docx import Document @@ -95,9 +95,7 @@ class Subdoc(object): return getattr(self.subdocx, name) def _get_xml(self): - xml = '' - for p in self.paragraphs: - xml += '\n' + re.sub(r'^.*\n', '', etree.tostring(p._element, encoding='unicode', pretty_print=True)) + xml = re.sub(r']*>','',etree.tostring(self.subdocx._element.body, encoding='unicode', pretty_print=False)) return xml def __unicode__(self): diff --git a/tests/python_logo.png b/tests/python_logo.png new file mode 100644 index 0000000..01a4870 Binary files /dev/null and b/tests/python_logo.png differ diff --git a/tests/subdoc.py b/tests/subdoc.py index bb9afcf..5c8c94f 100644 --- a/tests/subdoc.py +++ b/tests/subdoc.py @@ -6,6 +6,7 @@ Created : 2015-03-12 ''' from docxtpl import DocxTemplate +from docx.shared import Inches tpl=DocxTemplate('test_files/subdoc_tpl.docx') @@ -17,6 +18,27 @@ p.add_run(' generated with python by using ') p.add_run('python-docx').italic = True p.add_run(' library') +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('python_logo.png', width=Inches(1.25)) + +sd.add_paragraph('A Table :') +table = sd.add_table(rows=1, cols=3) +hdr_cells = table.rows[0].cells +hdr_cells[0].text = 'Qty' +hdr_cells[1].text = 'Id' +hdr_cells[2].text = 'Desc' +recordset=( (1,101,'Spam'), + (2,42,'Eggs'), + (3,631,'Spam,spam, eggs, and ham') ) +for item in recordset: + row_cells = table.add_row().cells + row_cells[0].text = str(item[0]) + row_cells[1].text = str(item[1]) + row_cells[2].text = item[2] + context = { 'mysubdoc' : sd, } diff --git a/tests/test_files/cellbg.docx b/tests/test_files/cellbg.docx index 4668431..b265148 100644 Binary files a/tests/test_files/cellbg.docx and b/tests/test_files/cellbg.docx differ diff --git a/tests/test_files/order.docx b/tests/test_files/order.docx index 8e2daf0..68ccc5c 100644 Binary files a/tests/test_files/order.docx and b/tests/test_files/order.docx differ diff --git a/tests/test_files/richtext.docx b/tests/test_files/richtext.docx index f87cdbb..e4c3aa6 100644 Binary files a/tests/test_files/richtext.docx and b/tests/test_files/richtext.docx differ diff --git a/tests/test_files/subdoc.docx b/tests/test_files/subdoc.docx index 02c6f0b..a4e389a 100644 Binary files a/tests/test_files/subdoc.docx and b/tests/test_files/subdoc.docx differ diff --git a/tests/test_files/subdoc_tpl.docx b/tests/test_files/subdoc_tpl.docx index 2965a55..3fe37d8 100644 Binary files a/tests/test_files/subdoc_tpl.docx and b/tests/test_files/subdoc_tpl.docx differ