Fix codestyling according to flake8 rules
This commit is contained in:
parent
45a46eb924
commit
9175f0cbd6
@ -12,9 +12,6 @@
|
||||
# All configuration values have a default; values that are commented out
|
||||
# serve to show the default.
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# If extensions (or modules to document with autodoc) are in another directory,
|
||||
# add these directories to sys.path here. If the directory is relative to the
|
||||
# documentation root, use os.path.abspath to make it absolute, like shown here.
|
||||
@ -186,10 +183,8 @@ htmlhelp_basename = 'python-docx-templatedoc'
|
||||
latex_elements = {
|
||||
# The paper size ('letterpaper' or 'a4paper').
|
||||
# 'papersize': 'letterpaper',
|
||||
|
||||
# The font size ('10pt', '11pt' or '12pt').
|
||||
# 'pointsize': '10pt',
|
||||
|
||||
# Additional stuff for the LaTeX preamble.
|
||||
# 'preamble': '',
|
||||
}
|
||||
|
||||
@ -11,7 +11,7 @@ __version__ = '0.9.0'
|
||||
|
||||
from lxml import etree
|
||||
from docx import Document
|
||||
from docx.opc.oxml import serialize_part_xml, parse_xml
|
||||
from docx.opc.oxml import parse_xml
|
||||
from docx.opc.part import XmlPart
|
||||
import docx.oxml.ns
|
||||
from docx.opc.constants import RELATIONSHIP_TYPE as REL_TYPE
|
||||
@ -71,6 +71,7 @@ class DocxTemplate(object):
|
||||
# into xml source also unescape html entities
|
||||
src_xml = re.sub(r'(?<={)(<[^>]*>)+(?=[\{%])|(?<=[%\}])(<[^>]*>)+(?=\})', '',
|
||||
src_xml, flags=re.DOTALL)
|
||||
|
||||
def striptags(m):
|
||||
return re.sub('</w:t>.*?(<w:t>|<w:t [^>]*>)', '',
|
||||
m.group(0), flags=re.DOTALL)
|
||||
@ -317,7 +318,7 @@ class DocxTemplate(object):
|
||||
width = 0.0
|
||||
new_average = None
|
||||
for c in columns:
|
||||
if not c.get(ns+'w') == None:
|
||||
if not c.get(ns+'w') is None:
|
||||
width += float(c.get(ns+'w'))
|
||||
# try to keep proportion of table
|
||||
if width > 0:
|
||||
@ -376,7 +377,6 @@ class DocxTemplate(object):
|
||||
extra_space = removed_width / len(columns_left)
|
||||
extra_space = int(extra_space)
|
||||
|
||||
|
||||
for c in columns_left:
|
||||
c.set(ns+'w', str(int(float(c.get(ns+'w')) + extra_space)))
|
||||
|
||||
@ -591,8 +591,8 @@ class DocxTemplate(object):
|
||||
|
||||
part_map[name] = (doc_part.rels[rel].target_ref,
|
||||
doc_part.rels[rel].target_part)
|
||||
|
||||
except:
|
||||
# FIXME: figure out what exceptions are thrown here and catch more specific exceptions
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
return part_map
|
||||
@ -658,7 +658,8 @@ class RichText(object):
|
||||
if text:
|
||||
self.add(text, **text_prop)
|
||||
|
||||
def add(self, text, style=None,
|
||||
def add(self, text,
|
||||
style=None,
|
||||
color=None,
|
||||
highlight=None,
|
||||
size=None,
|
||||
@ -720,7 +721,6 @@ class RichText(object):
|
||||
prop += (u'<w:rFonts w:ascii="{font}" w:hAnsi="{font}" w:cs="{font}"/>'
|
||||
.format(font=font))
|
||||
|
||||
|
||||
xml = u'<w:r>'
|
||||
if prop:
|
||||
xml += u'<w:rPr>%s</w:rPr>' % prop
|
||||
@ -730,7 +730,6 @@ class RichText(object):
|
||||
% (url_id, xml))
|
||||
self.xml += xml
|
||||
|
||||
|
||||
def __unicode__(self):
|
||||
return self.xml
|
||||
|
||||
@ -802,5 +801,3 @@ class InlineImage(object):
|
||||
|
||||
def __html__(self):
|
||||
return self._insert_image()
|
||||
|
||||
|
||||
|
||||
3
setup.py
3
setup.py
@ -6,6 +6,7 @@ import sys
|
||||
# To register onto Pypi :
|
||||
# python setup.py sdist bdist_wheel upload
|
||||
|
||||
|
||||
def read(*names):
|
||||
values = dict()
|
||||
for name in names:
|
||||
@ -28,6 +29,7 @@ News
|
||||
%(CHANGES)s
|
||||
""" % read('README', 'CHANGES')
|
||||
|
||||
|
||||
def get_version(pkg):
|
||||
path = os.path.join(os.path.dirname(__file__), pkg, '__init__.py')
|
||||
if sys.version_info >= (3, 0):
|
||||
@ -40,6 +42,7 @@ def get_version(pkg):
|
||||
return m.group(1)
|
||||
raise RuntimeError("Unable to find __version__ string in %s." % path)
|
||||
|
||||
|
||||
setup(name='docxtpl',
|
||||
version=get_version('docxtpl'),
|
||||
description='Python docx template engine',
|
||||
|
||||
@ -11,10 +11,30 @@ 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' },
|
||||
{
|
||||
'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',
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ import jinja2
|
||||
|
||||
jinja_env = jinja2.Environment()
|
||||
|
||||
|
||||
# to create new filters, first create functions that accept the value to filter
|
||||
# as first argument, and filter parameters as next arguments
|
||||
def my_filterA(value, my_string_arg):
|
||||
@ -21,6 +22,7 @@ def my_filterB(value, my_float_arg):
|
||||
return_value = value + my_float_arg
|
||||
return return_value
|
||||
|
||||
|
||||
# Then, declare them to jinja like this :
|
||||
jinja_env.filters['my_filterA'] = my_filterA
|
||||
jinja_env.filters['my_filterB'] = my_filterB
|
||||
|
||||
@ -8,7 +8,7 @@ context = {
|
||||
{'label': 'yellow', 'cols': ['banana', 'capsicum', 'pyrite', 'taxi']},
|
||||
{'label': 'red', 'cols': ['apple', 'tomato', 'cinnabar', 'doubledecker']},
|
||||
{'label': 'green', 'cols': ['guava', 'cucumber', 'aventurine', 'card']},
|
||||
]
|
||||
],
|
||||
}
|
||||
|
||||
tpl.render(context)
|
||||
|
||||
@ -23,14 +23,23 @@ context = {
|
||||
'name': 'John Doe',
|
||||
}
|
||||
|
||||
tpl.replace_embedded('templates/embedded_dummy.docx','templates/embedded_static_docx.docx')
|
||||
tpl.replace_embedded('templates/embedded_dummy2.docx','output/embedded_embedded_docx.docx')
|
||||
tpl.replace_embedded(
|
||||
'templates/embedded_dummy.docx', 'templates/embedded_static_docx.docx'
|
||||
)
|
||||
tpl.replace_embedded(
|
||||
'templates/embedded_dummy2.docx', 'output/embedded_embedded_docx.docx'
|
||||
)
|
||||
|
||||
# The zipname is the one you can find when you open docx with WinZip, 7zip (Windows)
|
||||
# or unzip -l (Linux). The zipname starts with "word/embeddings/".
|
||||
# Note that the file is renamed by MSWord, so you have to guess a little bit...
|
||||
tpl.replace_zipname('word/embeddings/Feuille_Microsoft_Office_Excel3.xlsx','templates/real_Excel.xlsx')
|
||||
tpl.replace_zipname('word/embeddings/Pr_sentation_Microsoft_Office_PowerPoint4.pptx','templates/real_PowerPoint.pptx')
|
||||
tpl.replace_zipname(
|
||||
'word/embeddings/Feuille_Microsoft_Office_Excel3.xlsx', 'templates/real_Excel.xlsx'
|
||||
)
|
||||
tpl.replace_zipname(
|
||||
'word/embeddings/Pr_sentation_Microsoft_Office_PowerPoint4.pptx',
|
||||
'templates/real_PowerPoint.pptx',
|
||||
)
|
||||
|
||||
tpl.render(context)
|
||||
tpl.save('output/embedded.docx')
|
||||
@ -1,11 +1,18 @@
|
||||
from docxtpl import *
|
||||
from docxtpl import DocxTemplate, R, Listing
|
||||
|
||||
tpl = DocxTemplate('templates/escape_tpl.docx')
|
||||
|
||||
context = {'myvar': R('"less than" must be escaped : <, this can be done with RichText() or R()'),
|
||||
context = {
|
||||
'myvar': R(
|
||||
'"less than" must be escaped : <, this can be done with RichText() or R()'
|
||||
),
|
||||
'myescvar': 'It can be escaped with a "|e" jinja filter in the template too : < ',
|
||||
'nlnp': R('Here is a multiple\nlines\nstring\aand some\aother\aparagraphs\aNOTE: the current character styling is removed'),
|
||||
'mylisting': Listing('the listing\nwith\nsome\nlines\nand special chars : <>&\f ... and a page break'),
|
||||
'nlnp': R(
|
||||
'Here is a multiple\nlines\nstring\aand some\aother\aparagraphs\aNOTE: the current character styling is removed'
|
||||
),
|
||||
'mylisting': Listing(
|
||||
'the listing\nwith\nsome\nlines\nand special chars : <>&\f ... and a page break'
|
||||
),
|
||||
'page_break': R('\f'),
|
||||
}
|
||||
|
||||
|
||||
@ -14,7 +14,8 @@ XML_RESERVED = """<"&'>"""
|
||||
|
||||
tpl = DocxTemplate('templates/escape_tpl_auto.docx')
|
||||
|
||||
context = {'nested_dict': {name(text_type(c)): c for c in XML_RESERVED},
|
||||
context = {
|
||||
'nested_dict': {name(text_type(c)): c for c in XML_RESERVED},
|
||||
'autoescape': 'Escaped "str & ing"!',
|
||||
'autoescape_unicode': u'This is an escaped <unicode> example \u4f60 & \u6211',
|
||||
'iteritems': iteritems,
|
||||
|
||||
@ -10,7 +10,9 @@ from docxtpl import DocxTemplate
|
||||
tpl = DocxTemplate('templates/header_footer_tpl.docx')
|
||||
|
||||
sd = tpl.new_subdoc()
|
||||
p = sd.add_paragraph('This is a sub-document to check it does not break header and footer')
|
||||
p = sd.add_paragraph(
|
||||
'This is a sub-document to check it does not break header and footer'
|
||||
)
|
||||
|
||||
context = {
|
||||
'title': 'Header and footer test',
|
||||
|
||||
@ -34,4 +34,3 @@ tpl.save(file_obj)
|
||||
file_obj.seek(0)
|
||||
with open(DEST_FILE2, 'wb') as f:
|
||||
f.write(file_obj.read())
|
||||
|
||||
|
||||
@ -12,7 +12,9 @@ from docxtpl import DocxTemplate
|
||||
tpl = DocxTemplate('templates/header_footer_tpl_utf8.docx')
|
||||
|
||||
sd = tpl.new_subdoc()
|
||||
p = sd.add_paragraph(u'This is a sub-document to check it does not break header and footer with utf-8 characters inside the template .docx')
|
||||
p = sd.add_paragraph(
|
||||
u'This is a sub-document to check it does not break header and footer with utf-8 characters inside the template .docx'
|
||||
)
|
||||
|
||||
context = {
|
||||
'title': u'헤더와 푸터',
|
||||
|
||||
@ -6,32 +6,40 @@ Created : 2017-01-14
|
||||
'''
|
||||
|
||||
from docxtpl import DocxTemplate, InlineImage
|
||||
|
||||
# for height and width you have to use millimeters (Mm), inches or points(Pt) class :
|
||||
from docx.shared import Mm, Inches, Pt
|
||||
from docx.shared import Mm
|
||||
import jinja2
|
||||
from jinja2.utils import Markup
|
||||
|
||||
tpl = DocxTemplate('templates/inline_image_tpl.docx')
|
||||
|
||||
context = {
|
||||
'myimage': InlineImage(tpl, 'templates/python_logo.png', width=Mm(20)),
|
||||
'myimageratio': InlineImage(tpl, 'templates/python_jpeg.jpg', width=Mm(30), height=Mm(60)),
|
||||
|
||||
'frameworks' : [{'image' : InlineImage(tpl,'templates/django.png',height=Mm(10)),
|
||||
'desc' : 'The web framework for perfectionists with deadlines'},
|
||||
|
||||
{'image' : InlineImage(tpl,'templates/zope.png',height=Mm(10)),
|
||||
'desc' : 'Zope is a leading Open Source Application Server and Content Management Framework'},
|
||||
|
||||
{'image': InlineImage(tpl, 'templates/pyramid.png', height=Mm(10)),
|
||||
'desc': 'Pyramid is a lightweight Python web framework aimed at taking small web apps into big web apps.'},
|
||||
|
||||
{'image' : InlineImage(tpl,'templates/bottle.png',height=Mm(10)),
|
||||
'desc' : 'Bottle is a fast, simple and lightweight WSGI micro web-framework for Python'},
|
||||
|
||||
{'image': InlineImage(tpl, 'templates/tornado.png', height=Mm(10)),
|
||||
'desc': 'Tornado is a Python web framework and asynchronous networking library.'},
|
||||
]
|
||||
'myimageratio': InlineImage(
|
||||
tpl, 'templates/python_jpeg.jpg', width=Mm(30), height=Mm(60)
|
||||
),
|
||||
'frameworks': [
|
||||
{
|
||||
'image': InlineImage(tpl, 'templates/django.png', height=Mm(10)),
|
||||
'desc': 'The web framework for perfectionists with deadlines',
|
||||
},
|
||||
{
|
||||
'image': InlineImage(tpl, 'templates/zope.png', height=Mm(10)),
|
||||
'desc': 'Zope is a leading Open Source Application Server and Content Management Framework',
|
||||
},
|
||||
{
|
||||
'image': InlineImage(tpl, 'templates/pyramid.png', height=Mm(10)),
|
||||
'desc': 'Pyramid is a lightweight Python web framework aimed at taking small web apps into big web apps.',
|
||||
},
|
||||
{
|
||||
'image': InlineImage(tpl, 'templates/bottle.png', height=Mm(10)),
|
||||
'desc': 'Bottle is a fast, simple and lightweight WSGI micro web-framework for Python',
|
||||
},
|
||||
{
|
||||
'image': InlineImage(tpl, 'templates/tornado.png', height=Mm(10)),
|
||||
'desc': 'Tornado is a Python web framework and asynchronous networking library.',
|
||||
},
|
||||
],
|
||||
}
|
||||
# testing that it works also when autoescape has been forced to True
|
||||
jinja_env = jinja2.Environment(autoescape=True)
|
||||
|
||||
@ -12,21 +12,33 @@ tpl=DocxTemplate('templates/nested_for_tpl.docx')
|
||||
context = {
|
||||
'dishes': [
|
||||
{'name': 'Pizza', 'ingredients': ['bread', 'tomato', 'ham', 'cheese']},
|
||||
{'name' : 'Hamburger', 'ingredients' : ['bread','chopped steak', 'cheese', 'sauce']},
|
||||
{'name' : 'Apple pie', 'ingredients' : ['flour','apples', 'suggar', 'quince jelly']},
|
||||
{
|
||||
'name': 'Hamburger',
|
||||
'ingredients': ['bread', 'chopped steak', 'cheese', 'sauce'],
|
||||
},
|
||||
{
|
||||
'name': 'Apple pie',
|
||||
'ingredients': ['flour', 'apples', 'suggar', 'quince jelly'],
|
||||
},
|
||||
],
|
||||
'authors': [
|
||||
{'name' : 'Saint-Exupery', 'books' : [
|
||||
{
|
||||
'name': 'Saint-Exupery',
|
||||
'books': [
|
||||
{'title': 'Le petit prince'},
|
||||
{'title': "L'aviateur"},
|
||||
{'title': 'Vol de nuit'},
|
||||
]},
|
||||
{'name' : 'Barjavel', 'books' : [
|
||||
],
|
||||
},
|
||||
{
|
||||
'name': 'Barjavel',
|
||||
'books': [
|
||||
{'title': 'Ravage'},
|
||||
{'title': "La nuit des temps"},
|
||||
{'title': 'Le grand secret'},
|
||||
]},
|
||||
]
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
tpl.render(context)
|
||||
|
||||
@ -19,7 +19,7 @@ context = {
|
||||
'in_europe': True,
|
||||
'is_paid': False,
|
||||
'company_name': 'The World Wide company',
|
||||
'total_price' : '100,000,000.00'
|
||||
'total_price': '100,000,000.00',
|
||||
}
|
||||
|
||||
tpl.render(context)
|
||||
|
||||
@ -10,9 +10,7 @@ from docxtpl import DocxTemplate, RichText
|
||||
tpl = DocxTemplate('templates/richtext_and_if_tpl.docx')
|
||||
|
||||
|
||||
context = {
|
||||
'foobar': RichText('Foobar!', color='ff0000')
|
||||
}
|
||||
context = {'foobar': RichText('Foobar!', color='ff0000')}
|
||||
|
||||
tpl.render(context)
|
||||
tpl.save('output/richtext_and_if.docx')
|
||||
|
||||
@ -30,9 +30,7 @@ hdr_cells = table.rows[0].cells
|
||||
hdr_cells[0].text = 'Qty'
|
||||
hdr_cells[1].text = 'Id'
|
||||
hdr_cells[2].text = 'Desc'
|
||||
recordset=( (1,101,'Spam'),
|
||||
(2,42,'Eggs'),
|
||||
(3,631,'Spam,spam, eggs, and ham') )
|
||||
recordset = ((1, 101, 'Spam'), (2, 42, 'Eggs'), (3, 631, 'Spam,spam, eggs, and ham'))
|
||||
for item in recordset:
|
||||
row_cells = table.add_row().cells
|
||||
row_cells[0].text = str(item[0])
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from docxtpl import DocxTemplate, RichText
|
||||
from docxtpl import DocxTemplate
|
||||
from jinja2.exceptions import TemplateError
|
||||
import six
|
||||
|
||||
@ -7,9 +7,7 @@ six.print_("Generating template error for testing (so it is safe to ignore) :")
|
||||
six.print_('.' * 80)
|
||||
try:
|
||||
tpl = DocxTemplate('templates/template_error_tpl.docx')
|
||||
tpl.render({
|
||||
'test_variable' : 'test variable value'
|
||||
})
|
||||
tpl.render({'test_variable': 'test variable value'})
|
||||
except TemplateError as the_error:
|
||||
six.print_(six.text_type(the_error))
|
||||
if hasattr(the_error, 'docx_context'):
|
||||
|
||||
@ -16,7 +16,7 @@ context = {
|
||||
{'desc': 'Guido', 'qty': 1, 'price': '100,000,000.00'},
|
||||
],
|
||||
'total_price': '100,000,000.00',
|
||||
'category' : 'Book'
|
||||
'category': 'Book',
|
||||
}
|
||||
|
||||
tpl.render(context)
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
from docxtpl import DocxTemplate, RichText
|
||||
|
||||
tpl = DocxTemplate('templates/word2016_tpl.docx')
|
||||
tpl.render({
|
||||
tpl.render(
|
||||
{
|
||||
'test_space': ' ',
|
||||
'test_tabs': 5 * '\t',
|
||||
'test_space_r': RichText(' '),
|
||||
'test_tabs_r': RichText(5 * '\t'),
|
||||
})
|
||||
}
|
||||
)
|
||||
tpl.save('output/word2016.docx')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user