From f5831ecb468a977721033aefb54c9a7d1304e750 Mon Sep 17 00:00:00 2001 From: roba Date: Wed, 9 Sep 2015 18:28:23 +0300 Subject: [PATCH] Port to Python 3 --- .gitignore | 3 +++ docxtpl/__init__.py | 20 ++++++++++++++------ setup.py | 6 ++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 085000c..85e7c88 100644 --- a/.gitignore +++ b/.gitignore @@ -57,3 +57,6 @@ target/ .settings *.pydevproject .metadata + +#Pycharm +.idea \ No newline at end of file diff --git a/docxtpl/__init__.py b/docxtpl/__init__.py index 5b86a17..7d105ff 100644 --- a/docxtpl/__init__.py +++ b/docxtpl/__init__.py @@ -5,14 +5,14 @@ Created : 2015-03-12 @author: Eric Lapouyade ''' -__version__ = '0.1.6' +__version__ = '0.1.7' from lxml import etree from docx import Document from jinja2 import Template from cgi import escape -import copy import re +import six class DocxTemplate(object): """ Class for managing docx files as they were jinja2 templates """ @@ -26,7 +26,7 @@ class DocxTemplate(object): return self.docx def get_xml(self): - return etree.tostring(self.docx._element.body, pretty_print=True) + return etree.tostring(self.docx._element.body, encoding='unicode', pretty_print=True) def write_xml(self,filename): with open(filename,'w') as fh: @@ -93,12 +93,17 @@ class Subdoc(object): def __getattr__(self, name): return getattr(self.subdocx, name) - def __unicode__(self): + def _get_xml(self): xml = '' for p in self.paragraphs: - xml += '\n' + re.sub(r'^.*\n', '', etree.tostring(p._element,pretty_print=True)) + xml += '\n' + re.sub(r'^.*\n', '', etree.tostring(p._element, encoding='unicode', pretty_print=True)) return xml + def __unicode__(self): + return self._get_xml() + + def __str__(self): + return self._get_xml() class RichText(object): @@ -121,7 +126,7 @@ class RichText(object): strike=False): - if not isinstance(text, unicode): + if not isinstance(text, six.text_type): text = text.decode('utf-8',errors='ignore') text = escape(text).replace('\n','') @@ -158,3 +163,6 @@ class RichText(object): def __unicode__(self): return self.xml + + def __str__(self): + return self.xml diff --git a/setup.py b/setup.py index 20816c8..1d3bfd9 100644 --- a/setup.py +++ b/setup.py @@ -32,6 +32,8 @@ setup(name='docxtpl', "Development Status :: 4 - Beta", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.4", ], keywords='jinja2', url='https://github.com/elapouya/python-docx-template', @@ -39,6 +41,6 @@ setup(name='docxtpl', author_email='elapouya@gmail.com', license='LGPL 2.1', packages=['docxtpl'], - install_requires = ['Sphinx<1.3b', 'sphinxcontrib-napoleon', 'python-docx','jinja2', 'lxml'], - eager_resources = ['docs'], + install_requires=['six', 'Sphinx<1.3b', 'sphinxcontrib-napoleon', 'python-docx', 'jinja2', 'lxml'], + eager_resources=['docs'], zip_safe=False)