Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# -*- coding: utf-8 -*-
'''
Created : 2015-03-12
@author: Eric Lapouyade
'''
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('templates/cellbg_tpl.docx')
context = {
'alerts' : [
{'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' },
],
}
tpl.render(context)
tpl.save('output/cellbg.docx')
# -*- coding: utf-8 -*-
'''
Created : 2015-03-26
@author: Eric Lapouyade
'''
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('templates/richtext_tpl.docx')
rt = RichText()
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('\nYou can add an hyperlink, here to ')
rt.add('google',url_id=tpl.build_url_id('http://google.com'))
rt.add('\nEt voilà ! ')
rt.add('\n1st line')
from docxtpl import DocxTemplate, RichText
tpl = DocxTemplate('templates/word2016_tpl.docx')
tpl.render({
'test_space' : ' ',
'test_tabs': 5*'\t',
'test_space_r' : RichText(' '),
'test_tabs_r': RichText(5*'\t'),
})
tpl.save('output/word2016.docx')
# -*- coding: utf-8 -*-
'''
Created : 2015-03-12
@author: Eric Lapouyade
'''
from docxtpl import DocxTemplate, RichText
tpl=DocxTemplate('templates/cellbg_tpl.docx')
context = {
'alerts' : [
{'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' },
],
}
tpl.render(context)
tpl.save('output/cellbg.docx')
rt.add('google',url_id=tpl.build_url_id('http://google.com'))
rt.add('\nEt voilà ! ')
rt.add('\n1st line')
rt.add('\n2nd line')
rt.add('\n3rd line')
rt.add('\n\n')
rt.add('\nFonts :\n',underline=True)
rt.add('Arial\n',font='Arial')
rt.add('Courier New\n',font='Courier New')
rt.add('Times New Roman\n',font='Times New Roman')
rt.add('\n\nHere some')
rt.add('superscript', superscript=True)
rt.add(' and some')
rt.add('subscript', subscript=True)
rt_embedded = RichText('an example of ')
rt_embedded.add(rt)
context = {
'example' : rt_embedded,
}
tpl.render(context)
tpl.save('output/richtext.docx')
def new_run(self):
self.runs.append(RichText(''))
self.run = self.runs[-1]
def traverse(self, elem):
def __unicode__(self):
output = ''
list_number = 1
for para in self.paragraphs:
# logmessage("Got a paragraph where style is " + para['params']['style'] + " and indentation is " + text_type(para['params']['indentation']))
output += ''
if para['params']['style'] in ('ul', 'ol', 'blockquote'):
output += ''
output += ''
if para['params']['style'] == 'ul':
output += text_type(RichText("•\t"))
if para['params']['style'] == 'ol':
output += text_type(RichText(text_type(list_number) + ".\t"))
list_number += 1
else:
list_number = 1
for run in para['runs']:
output += text_type(run)
output += ''
return output
def start_link(self, url):
def __init__(self, tpl):
self.paragraphs = [dict(params=dict(style='p', indentation=0), runs=[RichText('')])]
self.current_paragraph = self.paragraphs[-1]
self.run = self.current_paragraph['runs'][-1]
self.bold = False
self.italic = False
self.underline = False
self.strike = False
self.indentation = 0
self.style = 'p'
self.still_new = True
self.size = None
self.tpl = tpl
def new_paragraph(self):
def new_paragraph(self):
if self.still_new:
# logmessage("new_paragraph is still new and style is " + self.style + " and indentation is " + text_type(self.indentation))
self.current_paragraph['params']['style'] = self.style
self.current_paragraph['params']['indentation'] = self.indentation
return
# logmessage("new_paragraph where style is " + self.style + " and indentation is " + text_type(self.indentation))
self.current_paragraph = dict(params=dict(style=self.style, indentation=self.indentation), runs=[RichText('')])
self.paragraphs.append(self.current_paragraph)
self.run = self.current_paragraph['runs'][-1]
self.still_new = True
def __str__(self):
def __unicode__(self):
output = ''
list_number = 1
for para in self.paragraphs:
# logmessage("Got a paragraph where style is " + para['params']['style'] + " and indentation is " + text_type(para['params']['indentation']))
output += ''
if para['params']['style'] in ('ul', 'ol', 'blockquote'):
output += ''
output += ''
if para['params']['style'] == 'ul':
output += text_type(RichText("•\t"))
if para['params']['style'] == 'ol':
output += text_type(RichText(text_type(list_number) + ".\t"))
list_number += 1
else:
list_number = 1
for run in para['runs']:
output += text_type(run)
output += ''
return output
def start_link(self, url):