我刚刚学习如何在 MS Word 中使用 MAILMERGE。我想列出我的 MERGEFIELD 使用字母顺序,我的代码如下:
{ SEQ list \* alphabetic }. { MERGEFIELD item1 }
{ SEQ list \* alphabetic }. { MERGEFIELD item2 }
{ SEQ list \* alphabetic }. { MERGEFIELD item3 }
{ SEQ list \* alphabetic }. { MERGEFIELD item4 }
{ SEQ list \* alphabetic }. { MERGEFIELD item5 }
上面代码的结果是:
a. duck
b. dog
c. bird
d. cat
e. pig
当一个或多个 MERGEFIELD 值为空时,我遇到问题。例如如果 { MERGEFIELD item3 } 为空,它将生成如下列表:
a. duck
b. dog
c.
d. cat
e. pig
我想要的是这样的:
a. duck
b. dog
c. car
d. pig
如果是WindowsWord 2003或更高版本(或者可能是Word XP,我忘了),最简单的方法可能是使用以下嵌套字段代码并依靠Word“抑制空行”(这是其默认设置)
{ MERGEFIELD item1 \b "{ SEQ { IF { MERGEFIELD item1 } = "" nolist list } \*alphabetic }. " }
{ MERGEFIELD item2 \b "{ SEQ { IF { MERGEFIELD item2 } = "" nolist list } \*alphabetic }. " }
{ MERGEFIELD item3 \b "{ SEQ { IF { MERGEFIELD item3 } = "" nolist list } \*alphabetic }. " }
{ MERGEFIELD item4 \b "{ SEQ { IF { MERGEFIELD item4 } = "" nolist list } \*alphabetic }. " }
{ MERGEFIELD item5 \b "{ SEQ { IF { MERGEFIELD item5 } = "" nolist list } \*alphabetic }. " }
(所有的 { } 都必须是特殊的字段代码大括号对,您可以使用 ctrl-F9 在 Windows Word 中插入这些代码。此外,当您预览时,空行仍然会出现,并带有“..”开始,但当您实际执行合并时它们应该消失。)
如果您使用的是 Windows Word XP/2000 或更早版本,或者任何版本的 Mac Word,则不支持该标志。因此,您必须使用 IF 字段来生成空白/非空白情况的结果。问题在于,无论是否输出结果文本,通常都会评估 IF 字段内的 SE 字段,因此最终会在序列中出现间隙。即使尝试让 Word 使用两种不同的序列(如上所述的“list”和“nolist”)也不会像您希望的那样工作。但是,您可以尝试另一种方法(它应该适用于所有版本的 Word):
{ IF { MERGEFIELD item1 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item1 }
" }{ IF { MERGEFIELD item2 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item2 }
" }{ IF { MERGEFIELD item3 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item3 }
" }{ IF { MERGEFIELD item4 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item4 }
" }{ IF { MERGEFIELD item5 } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD item5 }
" }
请注意,在这种情况下,我们不能依靠 Word 来抑制因 IF 字段结果为空而产生的空行,因此我们在 MERGEFIELD 结果不为空时在 IF 字段插入的文本末尾放置一个段落标记。
任何一种方法都应该扩展到您需要的任意数量的项目,但请注意,*alphabetic 生成的“字母序列”不是
a 乙 。 。 z 啊 ab 。 。 阿兹 巴 BB 。 。
正如你所料,但是
a 乙 。 。 z 啊 BB 。 。 Z Z 啊啊 bbb 。 。 zzz
。
如果您确实想要第一个更熟悉的序列,您将需要做更多的事情。 (我以前做过这个,但可能会重新研究)。
但是,如果您想更容易地扩展项目计数,如果字段继续编号为 item6、item7 等,您可以修改字段编码,以便您只需粘贴份数即可你需要。对于使用 SEQ 的编码,您可以使用
{ MERGEFIELD "item{ SEQ i }" \b "{ SEQ { IF MERGEFIELD "item{ SEQ i \c }" } = "" nolist list } \*alphabetic }. " }
对于使用 SET 的编码,您可以使用
{ IF { MERGEFIELD "item{ SEQ i}" } <> "" "{ SET list { =list+1 } }{ list \*alphabetic }. { MERGEFIELD "item{ SEQ i \c }" }
" }
from mailmerge import MailMerge
from datetime import datetime
# Generate a single certificate
def GenerateCertificate(templateName, newName):
# Open the template
document = MailMerge(templateName)
# Replace content
document.merge(name='John Doe',
id='1010101010',
year='2020',
salary='99999',
job='Embedded Software Development Engineer')
# Save the file
document.write(newName)
if __name__ == "__main__":
templateName = 'SalaryCertificateTemplate.docx'
# Get the start time
startTime = datetime.now()
# Start the generation
for i in range(10000):
newName = f'./10000Certificates/SalaryCertificate{i}.docx'
GenerateCertificate(templateName, newName)
# Get the end time
endTime = datetime.now()
# Calculate the time difference
allSeconds = (endTime - startTime).seconds
print("Generation of 10,000 certificates took: ", str(allSeconds), " seconds")
print("Program finished!")