Port to Python 3
This commit is contained in:
parent
8f472222d7
commit
f5831ecb46
3
.gitignore
vendored
3
.gitignore
vendored
@ -57,3 +57,6 @@ target/
|
|||||||
.settings
|
.settings
|
||||||
*.pydevproject
|
*.pydevproject
|
||||||
.metadata
|
.metadata
|
||||||
|
|
||||||
|
#Pycharm
|
||||||
|
.idea
|
||||||
@ -5,14 +5,14 @@ Created : 2015-03-12
|
|||||||
@author: Eric Lapouyade
|
@author: Eric Lapouyade
|
||||||
'''
|
'''
|
||||||
|
|
||||||
__version__ = '0.1.6'
|
__version__ = '0.1.7'
|
||||||
|
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from docx import Document
|
from docx import Document
|
||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
from cgi import escape
|
from cgi import escape
|
||||||
import copy
|
|
||||||
import re
|
import re
|
||||||
|
import six
|
||||||
|
|
||||||
class DocxTemplate(object):
|
class DocxTemplate(object):
|
||||||
""" Class for managing docx files as they were jinja2 templates """
|
""" Class for managing docx files as they were jinja2 templates """
|
||||||
@ -26,7 +26,7 @@ class DocxTemplate(object):
|
|||||||
return self.docx
|
return self.docx
|
||||||
|
|
||||||
def get_xml(self):
|
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):
|
def write_xml(self,filename):
|
||||||
with open(filename,'w') as fh:
|
with open(filename,'w') as fh:
|
||||||
@ -93,12 +93,17 @@ class Subdoc(object):
|
|||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
return getattr(self.subdocx, name)
|
return getattr(self.subdocx, name)
|
||||||
|
|
||||||
def __unicode__(self):
|
def _get_xml(self):
|
||||||
xml = ''
|
xml = ''
|
||||||
for p in self.paragraphs:
|
for p in self.paragraphs:
|
||||||
xml += '<w:p>\n' + re.sub(r'^.*\n', '', etree.tostring(p._element,pretty_print=True))
|
xml += '<w:p>\n' + re.sub(r'^.*\n', '', etree.tostring(p._element, encoding='unicode', pretty_print=True))
|
||||||
return xml
|
return xml
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return self._get_xml()
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self._get_xml()
|
||||||
|
|
||||||
|
|
||||||
class RichText(object):
|
class RichText(object):
|
||||||
@ -121,7 +126,7 @@ class RichText(object):
|
|||||||
strike=False):
|
strike=False):
|
||||||
|
|
||||||
|
|
||||||
if not isinstance(text, unicode):
|
if not isinstance(text, six.text_type):
|
||||||
text = text.decode('utf-8',errors='ignore')
|
text = text.decode('utf-8',errors='ignore')
|
||||||
text = escape(text).replace('\n','<w:br/>')
|
text = escape(text).replace('\n','<w:br/>')
|
||||||
|
|
||||||
@ -158,3 +163,6 @@ class RichText(object):
|
|||||||
|
|
||||||
def __unicode__(self):
|
def __unicode__(self):
|
||||||
return self.xml
|
return self.xml
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.xml
|
||||||
|
|||||||
6
setup.py
6
setup.py
@ -32,6 +32,8 @@ setup(name='docxtpl',
|
|||||||
"Development Status :: 4 - Beta",
|
"Development Status :: 4 - Beta",
|
||||||
"Programming Language :: Python :: 2",
|
"Programming Language :: Python :: 2",
|
||||||
"Programming Language :: Python :: 2.7",
|
"Programming Language :: Python :: 2.7",
|
||||||
|
"Programming Language :: Python :: 3",
|
||||||
|
"Programming Language :: Python :: 3.4",
|
||||||
],
|
],
|
||||||
keywords='jinja2',
|
keywords='jinja2',
|
||||||
url='https://github.com/elapouya/python-docx-template',
|
url='https://github.com/elapouya/python-docx-template',
|
||||||
@ -39,6 +41,6 @@ setup(name='docxtpl',
|
|||||||
author_email='elapouya@gmail.com',
|
author_email='elapouya@gmail.com',
|
||||||
license='LGPL 2.1',
|
license='LGPL 2.1',
|
||||||
packages=['docxtpl'],
|
packages=['docxtpl'],
|
||||||
install_requires = ['Sphinx<1.3b', 'sphinxcontrib-napoleon', 'python-docx','jinja2', 'lxml'],
|
install_requires=['six', 'Sphinx<1.3b', 'sphinxcontrib-napoleon', 'python-docx', 'jinja2', 'lxml'],
|
||||||
eager_resources = ['docs'],
|
eager_resources=['docs'],
|
||||||
zip_safe=False)
|
zip_safe=False)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user