2 回答
TA贡献1847条经验 获得超7个赞
我不知道你的文件是如何命名的,它们是如何分布在不同文件夹中的,请尝试将它们的所有名称连同路径一起放在 Excel 工作表中的一列中,然后按照你对名称和邮件所做的方式遍历它们
attachment = r'{}.csv'.format(filepaths from excel sheet) with open(attachment, 'r') as my_attachment: myfile = my_attachment.read()
TA贡献1887条经验 获得超5个赞
下面是完整的代码。错误来了,因为缺少 PATH。win32com lib 需要完整路径,即使脚本在与附件相同的文件夹中运行也是如此。现在完美运行。:)
import glob, as
import win32com.client as win32
import pandas as pd
for file in glob.glob("*file.csv"):
print(file)
email_list = pd.read_csv(file)
names = email_list['FolderOwner']
emails = email_list['EmailAddress']
attachments = email_list['AttachmentPath']
PATH = "C:\\Users\\roy\\myfolder\\"
for i in range(len(emails)):
print("Sending email with " + file)
name = names[i]
email = emails[i]
attachment = attachments[i]
attachment1 = PATH + attachment
with open(attachment1, 'r') as my_attachment:
myfile = my_attachment.read()
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = email
mail.Subject = 'Message subject'
mail.Body = 'Hello ' + name
mail.Attachments.Add(attachment1)
mail.Send()
break
添加回答
举报