我正在尝试执行以下操作,但最终输入到 Outlook 的数据是混乱的,请查看下面的粘贴链接以获取输入、输出和代码
1. 使用 minidom 从 xml 读取数据 2. 如果有超链接,则用正则表达式替换以添加 ahref 属性 3. 将数据输出到 Outlook
INPUT/Output:-
http://pastie.org/5408694
我的代码:-
# Import package to access Outlook
import win32com.client
import re
import xml.dom.minidom as minidom
resultslis=[]
def getsanityresults(xmlfile):
dom = minidom.parse(xmlfile)
data=dom.getElementsByTagName('Sanity_Results')
textnode = data[0].childNodes[0]
testresults=textnode.data
for line in testresults.splitlines():
line = line.strip('\r,\n')
line = re.sub(r'(http://[^\s]+|//[^\s]+|\\\\[^\s]+)', r'<a href="\1">\1</a>', line)
print line
resultslis.append(line)
return resultslis
def main ():
file = open('results.xml','r')
sanityresults=getsanityresults(file)
print sanityresults
msg_body=("<HTML><head></head>"
"<body> <font face = \"Calibri\" <br>"
"<font face = \"Calibri\"%s<br><br>"
"</body></html>"
) % (sanityresults)
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.HTMLBody = msg_body
newMail.display()
if __name__ == '__main__':
main()