generating emails group by tuple values python -
i have tuple in format (name, email, units sold). generate html table, grouped name , embed table email through outlook.
my code:
from itertools import groupby 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 = [] name, rows in groupby(mytuple, itemgetter(0)): table = [] 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 subject" newmail.htmlbody = html_code newmail.to = "sampleemail@gmail.com" newmail.display()
desired output:
this open 3 emails html code in body.
email 1:
<html> <table> <tr><td>andrew</td><td>20</td><td></tr> <tr><td>andrew</td><td>56</td><td></tr> </table> </html>
email 2:
<html> <table> <tr><td>jim</td><td>12</td><td></tr> <tr><td>jim</td><td>15</td><td></tr> </table> </html>
email 3:
<html> <table> <tr><td>sarah</td><td>43</td><td></tr> </table> </html>
here's problem:
for name, value2 in rows: table.append( "<tr><td>{}</td><td>{}</td></tr>".format( name, value2 ))
change to:
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>"
the groupby function still returns 3-tuples.
python python-2.7
No comments:
Post a Comment