我有一个元组的格式(名称,电子邮件,单位出售)。我希望生成一个HTML表,按名称分组,然后通过Outlook将该表嵌入到电子邮件中。
我的守则:
from itertools import groupby
from operator import itemgetter
import win32com.client
mytuple = [('Andrew','Andrew@gmail.com','20'),('Jim',"Jim@gmail.com",'12'),("Sarah","Sarah@gmail.com",'43'),("Jim","Jim@gmail.com",'15'),("Andrew","Andrew@gmail.com",'56')]
mytuple = sorted(mytuple)
FULL_HTML = []
for name, rows in groupby(mytuple, itemgetter(0)):
table = []
for name, value2 in rows:
table.append(
"<tr><td>{}</td><td>{}</td></tr>".format(
name, value2 ))
html_code = "<html><table>" + str(table) + "</table></html>"
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "This is the subject"
newMail.HTMLBody = html_code
newMail.To = "sampleemail@gmail.com"
newMail.Display()期望产出:
这将打开3封电子邮件与HTML代码在他们的身体。
电子邮件1:
<html>
<table>
<tr><td>Andrew</td><td>20</td><td></tr>
<tr><td>Andrew</td><td>56</td><td></tr>
</table>
</html>电子邮件2:
<html>
<table>
<tr><td>Jim</td><td>12</td><td></tr>
<tr><td>Jim</td><td>15</td><td></tr>
</table>
</html>电子邮件3:
<html>
<table>
<tr><td>Sarah</td><td>43</td><td></tr>
</table>
</html>发布于 2014-11-11 01:59:36
你的问题是:
for name, value2 in rows:
table.append(
"<tr><td>{}</td><td>{}</td></tr>".format(
name, value2 ))将此更改为:
for n, e, id in rows:
table.append(
"<tr><td>{}</td><td>{}</td></tr>".format(
n, id ))
html_code = "<html><table>" + ''.join(table) + "</table></html>"groupby函数仍然返回3个元组。
https://stackoverflow.com/questions/26856180
复制相似问题