diff --git a/CHANGES.rst b/CHANGES.rst index 7386d61..eba4fa7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,8 @@ +0.5.5 (2018-10-05) +------------------ +- Cast to string non-string value given to RichText or Listing objects +- Import html.escape instead of cgi.escape (deprecated) + 0.5.4 (2018-09-19) ------------------ - Declare package as python2 and python3 compatible for wheel distrib diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 5a0b34c..41d38dc 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -5,7 +5,7 @@ Created : 2015-03-12 @author: Eric Lapouyade ''' -__version__ = '0.5.4' +__version__ = '0.5.5' from lxml import etree from docx import Document @@ -14,7 +14,11 @@ import docx.oxml.ns from docx.opc.constants import RELATIONSHIP_TYPE as REL_TYPE from jinja2 import Template from jinja2.exceptions import TemplateError -from cgi import escape +try: + from html import escape +except ImportError: + # cgi.escape is deprecated in python 3.7 + from cgi import escape import re import six import binascii @@ -421,6 +425,9 @@ class RichText(object): url_id=None): + # If not a string : cast to string (ex: int, dict etc...) + if not isinstance(text, (six.text_type, six.binary_type)): + text = six.text_type(text) if not isinstance(text, six.text_type): text = text.decode('utf-8',errors='ignore') text = escape(text).replace('\n', NEWLINE_XML).replace('\a', NEWPARAGRAPH_XML).replace('\t',TAB_XML) @@ -484,6 +491,9 @@ class Listing(object): use {{ mylisting }} in your template and context={ mylisting:Listing(the_listing_with_newlines) } """ def __init__(self, text): + # If not a string : cast to string (ex: int, dict etc...) + if not isinstance(text, (six.text_type, six.binary_type)): + text = six.text_type(text) self.xml = escape(text).replace('\n', NEWLINE_XML).replace('\a', NEWPARAGRAPH_XML) def __unicode__(self): diff --git a/tests/test_files/cellbg.docx b/tests/test_files/cellbg.docx index b880666..28b02b1 100644 Binary files a/tests/test_files/cellbg.docx and b/tests/test_files/cellbg.docx differ diff --git a/tests/test_files/dynamic_table.docx b/tests/test_files/dynamic_table.docx index 271ccd2..c018e98 100644 Binary files a/tests/test_files/dynamic_table.docx and b/tests/test_files/dynamic_table.docx differ diff --git a/tests/test_files/embedded.docx b/tests/test_files/embedded.docx index af2169a..779fa1f 100644 Binary files a/tests/test_files/embedded.docx and b/tests/test_files/embedded.docx differ diff --git a/tests/test_files/embedded_embedded_docx.docx b/tests/test_files/embedded_embedded_docx.docx index 8f7fc35..7af8367 100644 Binary files a/tests/test_files/embedded_embedded_docx.docx and b/tests/test_files/embedded_embedded_docx.docx differ diff --git a/tests/test_files/escape.docx b/tests/test_files/escape.docx index 04ad670..7c45987 100644 Binary files a/tests/test_files/escape.docx and b/tests/test_files/escape.docx differ diff --git a/tests/test_files/header_footer.docx b/tests/test_files/header_footer.docx index ad7705a..d79ab50 100644 Binary files a/tests/test_files/header_footer.docx and b/tests/test_files/header_footer.docx differ diff --git a/tests/test_files/header_footer_entities.docx b/tests/test_files/header_footer_entities.docx index c35e3f5..230da7c 100644 Binary files a/tests/test_files/header_footer_entities.docx and b/tests/test_files/header_footer_entities.docx differ diff --git a/tests/test_files/header_footer_image.docx b/tests/test_files/header_footer_image.docx index 3c1bc67..d9d4adb 100644 Binary files a/tests/test_files/header_footer_image.docx and b/tests/test_files/header_footer_image.docx differ diff --git a/tests/test_files/header_footer_utf8.docx b/tests/test_files/header_footer_utf8.docx index cfe8983..dd0091f 100644 Binary files a/tests/test_files/header_footer_utf8.docx and b/tests/test_files/header_footer_utf8.docx differ diff --git a/tests/test_files/inline_image.docx b/tests/test_files/inline_image.docx index 4afbc04..7d28c6e 100644 Binary files a/tests/test_files/inline_image.docx and b/tests/test_files/inline_image.docx differ diff --git a/tests/test_files/nested_for.docx b/tests/test_files/nested_for.docx index 9b5befd..c7895b8 100644 Binary files a/tests/test_files/nested_for.docx and b/tests/test_files/nested_for.docx differ diff --git a/tests/test_files/order.docx b/tests/test_files/order.docx index 026811a..3fad155 100644 Binary files a/tests/test_files/order.docx and b/tests/test_files/order.docx differ diff --git a/tests/test_files/replace_picture.docx b/tests/test_files/replace_picture.docx index 132b850..8e50d61 100644 Binary files a/tests/test_files/replace_picture.docx and b/tests/test_files/replace_picture.docx differ diff --git a/tests/test_files/richtext.docx b/tests/test_files/richtext.docx index 5081d1c..0e954b3 100644 Binary files a/tests/test_files/richtext.docx and b/tests/test_files/richtext.docx differ diff --git a/tests/test_files/richtext_and_if.docx b/tests/test_files/richtext_and_if.docx index b39209c..7d021e3 100644 Binary files a/tests/test_files/richtext_and_if.docx and b/tests/test_files/richtext_and_if.docx differ diff --git a/tests/test_files/subdoc.docx b/tests/test_files/subdoc.docx index a9d6f4c..2a89a89 100644 Binary files a/tests/test_files/subdoc.docx and b/tests/test_files/subdoc.docx differ diff --git a/tests/test_files/template_error.docx b/tests/test_files/template_error.docx index 1af5457..f458c64 100644 Binary files a/tests/test_files/template_error.docx and b/tests/test_files/template_error.docx differ diff --git a/tests/test_files/vertical_merge.docx b/tests/test_files/vertical_merge.docx index 626d4cd..b0adeab 100644 Binary files a/tests/test_files/vertical_merge.docx and b/tests/test_files/vertical_merge.docx differ diff --git a/tests/test_files/vertical_merge_nested.docx b/tests/test_files/vertical_merge_nested.docx index 697859d..917d29a 100644 Binary files a/tests/test_files/vertical_merge_nested.docx and b/tests/test_files/vertical_merge_nested.docx differ diff --git a/tests/test_files/word2016.docx b/tests/test_files/word2016.docx index 85192f0..211ad86 100644 Binary files a/tests/test_files/word2016.docx and b/tests/test_files/word2016.docx differ