Better tabs and spaces management for MS Word 2016
This commit is contained in:
parent
f2e76d0e61
commit
7437268425
@ -1,3 +1,7 @@
|
||||
0.4.11 (2018-05-16)
|
||||
-------------------
|
||||
- Better tabs and spaces management for MS Word 2016
|
||||
|
||||
0.4.10 (2018-05-08)
|
||||
------------------
|
||||
- Wheel distribution
|
||||
|
||||
@ -219,6 +219,24 @@ WARNING : unlike replace_pic() method, embdded_dummy.docx MUST exist in the temp
|
||||
file as the one inserted manually in the docx template.
|
||||
The replacement occurs in headers, footers and the whole document's body.
|
||||
|
||||
Microsoft Word 2016 special cases
|
||||
---------------------------------
|
||||
|
||||
MS Word 2016 will ingore ``\t`` tabulations. This is special to that version.
|
||||
Libreoffice or Wordpad do not have this problem. The same thing occurs for line
|
||||
beginning with a jinja2 tag provinding spaces : They will be ignored.
|
||||
To solve these problem, the solution is to use Richtext::
|
||||
|
||||
tpl.render({
|
||||
'test_space_r' : RichText(' '),
|
||||
'test_tabs_r': RichText(5*'\t'),
|
||||
})
|
||||
|
||||
And in your template, use the {{r notation::
|
||||
|
||||
{{r test_space_r}} Spaces will be preserved
|
||||
{{r test_tabs_r}} Tabs will be displayed
|
||||
|
||||
Tables
|
||||
------
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ Created : 2015-03-12
|
||||
@author: Eric Lapouyade
|
||||
'''
|
||||
|
||||
__version__ = '0.4.10'
|
||||
__version__ = '0.4.11'
|
||||
|
||||
from lxml import etree
|
||||
from docx import Document
|
||||
@ -19,11 +19,10 @@ import six
|
||||
import binascii
|
||||
import os
|
||||
import zipfile
|
||||
from six.moves import html_parser
|
||||
unescape = html_parser.HTMLParser().unescape
|
||||
|
||||
NEWLINE = '</w:t><w:br/><w:t xml:space="preserve">'
|
||||
NEWPARAGRAPH = '</w:t></w:r></w:p><w:p><w:r><w:t xml:space="preserve">'
|
||||
NEWLINE_XML = '</w:t><w:br/><w:t xml:space="preserve">'
|
||||
NEWPARAGRAPH_XML = '</w:t></w:r></w:p><w:p><w:r><w:t xml:space="preserve">'
|
||||
TAB_XML = '</w:t></w:r><w:r><w:tab/></w:r><w:r><w:t xml:space="preserve">'
|
||||
|
||||
class DocxTemplate(object):
|
||||
""" Class for managing docx files as they were jinja2 templates """
|
||||
@ -397,7 +396,7 @@ class RichText(object):
|
||||
|
||||
if not isinstance(text, six.text_type):
|
||||
text = text.decode('utf-8',errors='ignore')
|
||||
text = escape(text).replace('\n',NEWLINE).replace('\a',NEWPARAGRAPH)
|
||||
text = escape(text).replace('\n', NEWLINE_XML).replace('\a', NEWPARAGRAPH_XML).replace('\t',TAB_XML)
|
||||
|
||||
prop = u''
|
||||
|
||||
@ -447,7 +446,7 @@ class Listing(object):
|
||||
use {{ mylisting }} in your template and context={ mylisting:Listing(the_listing_with_newlines) }
|
||||
"""
|
||||
def __init__(self, text):
|
||||
self.xml = escape(text).replace('\n',NEWLINE).replace('\a',NEWPARAGRAPH)
|
||||
self.xml = escape(text).replace('\n', NEWLINE_XML).replace('\a', NEWPARAGRAPH_XML)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.xml
|
||||
|
||||
Binary file not shown.
Binary file not shown.
BIN
tests/test_files/word2016.docx
Normal file
BIN
tests/test_files/word2016.docx
Normal file
Binary file not shown.
BIN
tests/test_files/word2016_tpl.docx
Normal file
BIN
tests/test_files/word2016_tpl.docx
Normal file
Binary file not shown.
@ -1,5 +0,0 @@
|
||||
from docxtpl import *
|
||||
|
||||
tpl = DocxTemplate('test_files/unescape_jinja2_tags_tpl.docx')
|
||||
tpl.render({})
|
||||
tpl.save('test_files/unescape_jinja2_tags.docx')
|
||||
10
tests/word2016.py
Normal file
10
tests/word2016.py
Normal file
@ -0,0 +1,10 @@
|
||||
from docxtpl import DocxTemplate, RichText
|
||||
|
||||
tpl = DocxTemplate('test_files/word2016_tpl.docx')
|
||||
tpl.render({
|
||||
'test_space' : ' ',
|
||||
'test_tabs': 5*'\t',
|
||||
'test_space_r' : RichText(' '),
|
||||
'test_tabs_r': RichText(5*'\t'),
|
||||
})
|
||||
tpl.save('test_files/word2016.docx')
|
||||
Loading…
x
Reference in New Issue
Block a user