Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建一个for循环,为每个唯一的收件人生成带有html数据框的单独电子邮件_Python_Pandas - Fatal编程技术网

Python 创建一个for循环,为每个唯一的收件人生成带有html数据框的单独电子邮件

Python 创建一个for循环,为每个唯一的收件人生成带有html数据框的单独电子邮件,python,pandas,Python,Pandas,我想创建一个for循环,为每个唯一的收件人(使用第一列)创建带有html数据框的单独电子邮件 数据帧如下所示: 预期产出: 我当前的代码: for index, row in df.iterrows(): email = (row['Email Address']) subject = (row['Subject']) table = df.iloc[:, 2:].to_html(index = False) olMailItem = 0x0 obj = win32com.client.D

我想创建一个for循环,为每个唯一的收件人(使用第一列)创建带有html数据框的单独电子邮件

数据帧如下所示:

预期产出:

我当前的代码:

for index, row in df.iterrows():
email = (row['Email Address'])
subject = (row['Subject'])
table = df.iloc[:, 2:].to_html(index = False)
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = subject

newMail.HTMLbody = ( '''
<html>
<head>
</head>
    <body>
        <p>Hi All,</p>
        <p>{TABLE}</p>
        <p>Best Regards,</p>
    </body>
</html>'''.format(TABLE = table))

newMail.To = email
newMail.display()
对于索引,df.iterrows()中的行:
电子邮件=(第[‘电子邮件地址’]行)
主题=(第['subject'行])
table=df.iloc[:,2:]to_html(index=False)
olMailItem=0x0
obj=win32com.client.Dispatch(“Outlook.Application”)
newMail=obj.CreateItem(olMailItem)
newMail.Subject=Subject
newMail.HTMLbody=(“”)
大家好

{TABLE}

致以最良好的祝愿

''。格式(表格=表格)) newMail.To=电子邮件 newMail.display()
要按唯一电子邮件对表进行分组,您需要使用groupby:

grp = df.groupby('email') #group data by email
sent_mail = [] #store emails to which messages have been sent here
for index, row in df.iterrows():
    if row['email'] in sent_mail: #check if mail has already been sent to prevent sending multile mails
        pass
    else:
        email = (row['email'])
        sent_mail.append(email)
        subject = (row['subject'])
        table = grp.get_group(email).to_html(index = False)
        olMailItem = 0x0
        obj = win32com.client.Dispatch("Outlook.Application")
        newMail = obj.CreateItem(olMailItem)
        newMail.Subject = subject

        newMail.HTMLbody = ( '''
        <html>
        <head>
        </head>
            <body>
                <p>Hi All,</p>
                <p>{TABLE}</p>
                <p>Best Regards,</p>
            </body>
        </html>'''.format(TABLE = table))

        newMail.To = email
        newMail.display()
grp=df.groupby('email')#通过电子邮件对数据进行分组
sent_mail=[]。#存储已在此处发送邮件的电子邮件
对于索引,df.iterrows()中的行:
如果已发送邮件中的第['email']行:#检查邮件是否已发送,以防止发送多个邮件
通过
其他:
电子邮件=(第['email']行)
已发送邮件。附加(电子邮件)
主题=(第['subject'行])
table=grp.get\u group(email).to\u html(index=False)
olMailItem=0x0
obj=win32com.client.Dispatch(“Outlook.Application”)
newMail=obj.CreateItem(olMailItem)
newMail.Subject=Subject
newMail.HTMLbody=(“”)
大家好

{TABLE}

致以最良好的祝愿

''。格式(表格=表格)) newMail.To=电子邮件 newMail.display()
您的代码的输出是什么?您好,Seyi,脚本工作起来很有魅力,但是我在第二个电子邮件地址收到了两封电子邮件(TestTwo@xyz.com),可能是检查工作不正常。对不起,我的代码中遗漏了一行。我现在编辑了我的答案,你可以再试一次Hanks Seyi它工作得很好,你在这项任务中为我节省了很多时间:)