Update RichText
This commit is contained in:
parent
28f7f6b6dd
commit
1483bae331
@ -2,4 +2,5 @@ eclipse.preferences.version=1
|
||||
encoding//docxtpl/__init__.py=utf-8
|
||||
encoding//tests/cellbg.py=utf-8
|
||||
encoding//tests/order.py=utf-8
|
||||
encoding//tests/richtext.py=utf-8
|
||||
encoding//tests/subdoc.py=utf-8
|
||||
|
||||
@ -10,6 +10,7 @@ __version__ = '0.1.1'
|
||||
from lxml import etree
|
||||
from docx import Document
|
||||
from jinja2 import Template
|
||||
from cgi import escape
|
||||
import copy
|
||||
import re
|
||||
|
||||
@ -58,6 +59,7 @@ class DocxTemplate(object):
|
||||
def render_xml(self,src_xml,context):
|
||||
template = Template(src_xml)
|
||||
dst_xml = template.render(context)
|
||||
dst_xml = dst_xml.replace('{_{','{{').replace('}_}','}}').replace('{_%','{%').replace('%_}','%}')
|
||||
return dst_xml
|
||||
|
||||
def build_xml(self,context):
|
||||
@ -105,7 +107,7 @@ class RichText(object):
|
||||
def __init__(self, text=None, **text_prop):
|
||||
self.xml = ''
|
||||
if text:
|
||||
self.add_run(text, **text_prop)
|
||||
self.add(text, **text_prop)
|
||||
|
||||
def add(self, text, style=None,
|
||||
color=None,
|
||||
@ -116,36 +118,41 @@ class RichText(object):
|
||||
underline=False,
|
||||
strike=False):
|
||||
|
||||
prop = ''
|
||||
|
||||
if not isinstance(text, unicode):
|
||||
text = text.decode('utf-8',errors='ignore')
|
||||
text = escape(text).replace('\n','<w:br/>')
|
||||
|
||||
prop = u''
|
||||
|
||||
if style:
|
||||
prop += '<w:rStyle w:val="%s"/>' % style
|
||||
prop += u'<w:rStyle w:val="%s"/>' % style
|
||||
if color:
|
||||
if color[0] == '#':
|
||||
color = color[1:]
|
||||
prop += '<w:color w:val="%s"/>' % color
|
||||
prop += u'<w:color w:val="%s"/>' % color
|
||||
if highlight:
|
||||
if highlight[0] == '#':
|
||||
highlight = highlight[1:]
|
||||
prop += '<w:highlight w:val="%s"/>' % highlight
|
||||
prop += u'<w:highlight w:val="%s"/>' % highlight
|
||||
if size:
|
||||
prop += '<w:sz w:val="%s"/>' % size
|
||||
prop += '<w:szCs w:val="%s"/>' % size
|
||||
prop += u'<w:sz w:val="%s"/>' % size
|
||||
prop += u'<w:szCs w:val="%s"/>' % size
|
||||
if bold:
|
||||
prop += '<w:b/>'
|
||||
prop += u'<w:b/>'
|
||||
if italic:
|
||||
prop += '<w:i/>'
|
||||
prop += u'<w:i/>'
|
||||
if underline:
|
||||
if underline not in ['single','double']:
|
||||
underline = 'single'
|
||||
prop += '<w:u w:val="%s"/>' % underline
|
||||
prop += u'<w:u w:val="%s"/>' % underline
|
||||
if strike:
|
||||
prop += '<w:strike/>'
|
||||
prop += u'<w:strike/>'
|
||||
|
||||
self.xml += '<w:r>'
|
||||
self.xml += u'<w:r>'
|
||||
if prop:
|
||||
self.xml += '<w:rPr>%s</w:rPr>' % prop
|
||||
self.xml += '<w:t>%s</w:t></w:r>\n' % text
|
||||
self.xml += u'<w:rPr>%s</w:rPr>' % prop
|
||||
self.xml += u'<w:t xml:space="preserve">%s</w:t></w:r>\n' % text
|
||||
|
||||
def __unicode__(self):
|
||||
return xml
|
||||
return self.xml
|
||||
|
||||
@ -5,16 +5,16 @@ Created : 2015-03-12
|
||||
@author: Eric Lapouyade
|
||||
'''
|
||||
|
||||
from docxtpl import DocxTemplate
|
||||
from docxtpl import DocxTemplate, RichText
|
||||
|
||||
tpl=DocxTemplate('test_files/cellbg_tpl.docx')
|
||||
|
||||
context = {
|
||||
'alerts' : [
|
||||
{'date' : '2015-03-10', 'desc' : 'Very critical alert', 'type' : 'CRITICAL', 'bg': 'FF0000' },
|
||||
{'date' : '2015-03-11', 'desc' : 'Just a warning', 'type' : 'WARNING', 'bg': 'FFDD00' },
|
||||
{'date' : '2015-03-12', 'desc' : 'Information', 'type' : 'INFO', 'bg': '8888FF' },
|
||||
{'date' : '2015-03-13', 'desc' : 'Debug trace', 'type' : 'DEBUG', 'bg': 'FF00FF' },
|
||||
{'date' : '2015-03-10', 'desc' : RichText('Very critical alert',color='FF0000', bold=True), 'type' : 'CRITICAL', 'bg': 'FF0000' },
|
||||
{'date' : '2015-03-11', 'desc' : RichText('Just a warning'), 'type' : 'WARNING', 'bg': 'FFDD00' },
|
||||
{'date' : '2015-03-12', 'desc' : RichText('Information'), 'type' : 'INFO', 'bg': '8888FF' },
|
||||
{'date' : '2015-03-13', 'desc' : RichText('Debug trace'), 'type' : 'DEBUG', 'bg': 'FF00FF' },
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
36
tests/richtext.py
Normal file
36
tests/richtext.py
Normal file
@ -0,0 +1,36 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
'''
|
||||
Created : 2015-03-26
|
||||
|
||||
@author: Eric Lapouyade
|
||||
'''
|
||||
|
||||
from docxtpl import DocxTemplate, RichText
|
||||
|
||||
tpl=DocxTemplate('test_files/richtext_tpl.docx')
|
||||
|
||||
rt = RichText('an exemple of ')
|
||||
rt.add('a rich text', style='myrichtextstyle')
|
||||
rt.add(' with ')
|
||||
rt.add('some italic', italic=True)
|
||||
rt.add(' and ')
|
||||
rt.add('some violet', color='#ff00ff')
|
||||
rt.add(' and ')
|
||||
rt.add('some striked', strike=True)
|
||||
rt.add(' and ')
|
||||
rt.add('some small', size=14)
|
||||
rt.add(' or ')
|
||||
rt.add('big', size=60)
|
||||
rt.add(' text.')
|
||||
rt.add(' Et voilà ! ')
|
||||
rt.add('\n1st line')
|
||||
rt.add('\n2nd line')
|
||||
rt.add('\n3rd line')
|
||||
rt.add('\n\n<cool>')
|
||||
|
||||
context = {
|
||||
'example' : rt,
|
||||
}
|
||||
|
||||
tpl.render(context)
|
||||
tpl.save('test_files/richtext.docx')
|
||||
Binary file not shown.
Binary file not shown.
BIN
tests/test_files/richtext.docx
Normal file
BIN
tests/test_files/richtext.docx
Normal file
Binary file not shown.
BIN
tests/test_files/richtext_tpl.docx
Normal file
BIN
tests/test_files/richtext_tpl.docx
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user