From 4ed3d08cc011799c144b07ed7accd01082f258f0 Mon Sep 17 00:00:00 2001 From: zhengmingpei Date: Sun, 3 Nov 2019 14:00:50 +0800 Subject: [PATCH 1/2] for Chinese language, if field text contains \n, insert new paragraph instead of "
" --- .gitignore | 1 + mailmerge.py | 44 +++++++++++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index d3e03f6..ed2481c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.tox/ *.pyc +.vscode/settings.json diff --git a/mailmerge.py b/mailmerge.py index 7a66503..6b7fe85 100644 --- a/mailmerge.py +++ b/mailmerge.py @@ -271,29 +271,51 @@ def __merge_field(self, part, field, text): mf.tag = '{%(w)s}r' % NAMESPACES mf.extend(children) - nodes = [] # preserve new lines in replacement text text = text or '' # text might be None text_parts = str(text).replace('\r', '').split('\n') - for i, text_part in enumerate(text_parts): - text_node = Element('{%(w)s}t' % NAMESPACES) - text_node.text = text_part - nodes.append(text_node) - # if not last node add new line node - if i < (len(text_parts) - 1): - nodes.append(Element('{%(w)s}br' % NAMESPACES)) + # step1 insert the first line + text_node = etree.Element('{%(w)s}t' % NAMESPACES) + text_node.text = text_parts[0] ph = mf.find('MergeText') if ph is not None: # add text nodes at the exact position where # MergeText was found index = mf.index(ph) - for node in reversed(nodes): - mf.insert(index, node) + mf.insert(index, text_node) mf.remove(ph) else: - mf.extend(nodes) + mf.extend(text_node) + + current_paragraph = mf.getparent() + + # step2 insert each remaining line as different paragraph + # with the style of the same merge field + if(len(text_parts)>1): + # get the style of current merge field + # contain paragraph and text + # ** + current_paragraph_style = current_paragraph[0] + # ** + current_text_style = mf[0] + current_body = current_paragraph.getparent() + # find the next insert place + insert_index = current_body.index(current_paragraph)+1 + for k in range(1,len(text_parts)): + paragraph_node = etree.Element('{%(w)s}p' % NAMESPACES) + # copy paragraph style + paragraph_node.append(deepcopy(current_paragraph_style)) + # copy text style + text_node = etree.SubElement(paragraph_node,'{%(w)s}r' % NAMESPACES) + text_node.append(deepcopy(current_text_style)) + # insert text + etree.SubElement(text_node,'{%(w)s}t' % NAMESPACES).text = text_parts[k] + # insert new paragraph + current_body.insert(insert_index,paragraph_node) + # find the next insert place + insert_index = insert_index + 1 def merge_rows(self, anchor, rows): table, idx, template = self.__find_row_anchor(anchor) From 503f079162a5df8e3f8cff8542a8f1ac003d05ba Mon Sep 17 00:00:00 2001 From: ZhengMingpei Date: Sun, 3 Nov 2019 14:04:02 +0800 Subject: [PATCH 2/2] Update .gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index ed2481c..d3e03f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ /.tox/ *.pyc -.vscode/settings.json