diff --git a/CHANGES.rst b/CHANGES.rst index 6b91bae..78f946d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,7 @@ +0.4.11 (2018-05-16) +------------------- +- Better tabs and spaces management for MS Word 2016 + 0.4.10 (2018-05-08) ------------------ - Wheel distribution diff --git a/docs/index.rst b/docs/index.rst index 3291f53..0bd9af8 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -219,6 +219,24 @@ WARNING : unlike replace_pic() method, embdded_dummy.docx MUST exist in the temp file as the one inserted manually in the docx template. The replacement occurs in headers, footers and the whole document's body. +Microsoft Word 2016 special cases +--------------------------------- + +MS Word 2016 will ingore ``\t`` tabulations. This is special to that version. +Libreoffice or Wordpad do not have this problem. The same thing occurs for line +beginning with a jinja2 tag provinding spaces : They will be ignored. +To solve these problem, the solution is to use Richtext:: + + tpl.render({ + 'test_space_r' : RichText(' '), + 'test_tabs_r': RichText(5*'\t'), + }) + +And in your template, use the {{r notation:: + + {{r test_space_r}} Spaces will be preserved + {{r test_tabs_r}} Tabs will be displayed + Tables ------ diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 9b71c4e..68f06d7 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -5,7 +5,7 @@ Created : 2015-03-12 @author: Eric Lapouyade ''' -__version__ = '0.4.10' +__version__ = '0.4.11' from lxml import etree from docx import Document @@ -19,11 +19,10 @@ import six import binascii import os import zipfile -from six.moves import html_parser -unescape = html_parser.HTMLParser().unescape -NEWLINE = '' -NEWPARAGRAPH = '' +NEWLINE_XML = '' +NEWPARAGRAPH_XML = '' +TAB_XML = '' class DocxTemplate(object): """ Class for managing docx files as they were jinja2 templates """ @@ -397,7 +396,7 @@ class RichText(object): if not isinstance(text, six.text_type): text = text.decode('utf-8',errors='ignore') - text = escape(text).replace('\n',NEWLINE).replace('\a',NEWPARAGRAPH) + text = escape(text).replace('\n', NEWLINE_XML).replace('\a', NEWPARAGRAPH_XML).replace('\t',TAB_XML) prop = u'' @@ -447,7 +446,7 @@ class Listing(object): use {{ mylisting }} in your template and context={ mylisting:Listing(the_listing_with_newlines) } """ def __init__(self, text): - self.xml = escape(text).replace('\n',NEWLINE).replace('\a',NEWPARAGRAPH) + self.xml = escape(text).replace('\n', NEWLINE_XML).replace('\a', NEWPARAGRAPH_XML) def __unicode__(self): return self.xml diff --git a/tests/test_files/unescape_jinja2_tags.docx b/tests/test_files/unescape_jinja2_tags.docx deleted file mode 100644 index 4c90cda..0000000 Binary files a/tests/test_files/unescape_jinja2_tags.docx and /dev/null differ diff --git a/tests/test_files/unescape_jinja2_tags_tpl.docx b/tests/test_files/unescape_jinja2_tags_tpl.docx deleted file mode 100644 index 4c5cd56..0000000 Binary files a/tests/test_files/unescape_jinja2_tags_tpl.docx and /dev/null differ diff --git a/tests/test_files/word2016.docx b/tests/test_files/word2016.docx new file mode 100644 index 0000000..ae3c280 Binary files /dev/null and b/tests/test_files/word2016.docx differ diff --git a/tests/test_files/word2016_tpl.docx b/tests/test_files/word2016_tpl.docx new file mode 100644 index 0000000..ab699d3 Binary files /dev/null and b/tests/test_files/word2016_tpl.docx differ diff --git a/tests/unescape_jinja2_tags.py b/tests/unescape_jinja2_tags.py deleted file mode 100644 index 0e98c1f..0000000 --- a/tests/unescape_jinja2_tags.py +++ /dev/null @@ -1,5 +0,0 @@ -from docxtpl import * - -tpl = DocxTemplate('test_files/unescape_jinja2_tags_tpl.docx') -tpl.render({}) -tpl.save('test_files/unescape_jinja2_tags.docx') diff --git a/tests/word2016.py b/tests/word2016.py new file mode 100644 index 0000000..f448c3d --- /dev/null +++ b/tests/word2016.py @@ -0,0 +1,10 @@ +from docxtpl import DocxTemplate, RichText + +tpl = DocxTemplate('test_files/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')