发布于2019-08-07 13:29 阅读(560) 评论(0) 点赞(5) 收藏(4)
参考:Python3读取Outlook邮件并写入MySQL - 知乎
查看特定时间,特定发件人,特定邮箱账号的邮件,写入pandas Datafrme 格式
import re
from win32com.client.gencache import EnsureDispatch as Dispatch
import pandas as pd
def read_email(date,my_account,sent_account):
# 读取邮箱
outlook = Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")
Accounts = mapi.Folders
# 读取的员工账号
names =[]
# 短信内容
contents=[]
# 读取邮件存入pandas
c=['Root_Directory_Name_1', 'Level_1_FolderName_1', 'Level_2_FolderName_1', 'ReceivedTime_1', 'SenderName_1', 'to_to_1', 'cc_cc_1', 'Subject_1', 'MessageID_1', 'ConversationTopic_1', 'ConversationID_1', 'ConversationIndex_1', 'EmailBody_1']
df = pd.DataFrame(columns=c)
for Account_Name in Accounts:
# 只查找需要的邮箱账号信息
if Account_Name.Name==my_account:
# print(' >> 正在查询的帐户名称:',Account_Name.Name,'\n')
Level_1_Names = Account_Name.Folders
for Level_1_Name in Level_1_Names:
# 只需要收件箱的邮件
if Level_1_Name.Name=='收件箱':
# print(' - 正在查询一级目录:' , Level_1_Name.Name)
Mail_1_Messages = Level_1_Name.Items
# 将邮件按日期排序,可减少遍历内容
Mail_1_Messages.Sort("[ReceivedTime]", True)
for xx in Mail_1_Messages: # xx = 'mail' # 开始查看单个邮件的信息
Root_Directory_Name_1 = Account_Name.Name
Level_1_FolderName_1 = Level_1_Name.Name
Level_2_FolderName_1 = ''
if (hasattr(xx, 'ReceivedTime')):
# ReceivedTime_1 = str(xx.ReceivedTime)[:-6] # 接收时间
ReceivedTime_1 = str(xx.ReceivedTime)[:10] # 接收日期
else:
ReceivedTime_1 = ''
if ReceivedTime_1!=date: #只要特定日期的邮件
break
if (hasattr(xx, 'SenderName')): # 发件人
SenderName_1 = xx.SenderName
else:
SenderName_1 = ''
if SenderName_1 !=sent_account: #只要特定发件人的邮件
continue
if (hasattr(xx, 'Subject')): # 主题
Subject_1 = xx.Subject
else:
Subject_1 = ''
if (hasattr(xx, 'Body')): # 邮件内容
EmailBody_1 = xx.Body
else:
EmailBody_1 = ''
if (hasattr(xx, 'To')): # 收件人
to_to_1 = xx.To
else:
to_to_1 = ''
if (hasattr(xx, 'CC')): # 抄送人
cc_cc_1 = xx.CC
else:
cc_cc_1 = ''
if (hasattr(xx, 'EntryID')): # 邮件MessageID
MessageID_1 = xx.EntryID
else:
MessageID_1 = ''
if (hasattr(xx, 'ConversationTopic')): # 会话主题
ConversationTopic_1 = xx.ConversationTopic
else:
ConversationTopic_1 = ''
if (hasattr(xx, 'ConversationID')): # 会话ID
ConversationID_1 = xx.ConversationID
else:
ConversationID_1 = ''
if (hasattr(xx, 'ConversationIndex')): # 会话记录相对位置
ConversationIndex_1 = xx.ConversationIndex
else:
ConversationIndex_1 = ''
data = [Root_Directory_Name_1, Level_1_FolderName_1, Level_2_FolderName_1, ReceivedTime_1, SenderName_1, to_to_1, cc_cc_1, Subject_1, MessageID_1, ConversationTopic_1, ConversationID_1, ConversationIndex_1, EmailBody_1]
dataf=dict(zip(c,data))
df = df.append(dataf,ignore_index=True)
return df
作者:无敌是多么寂寞
链接:https://www.pythonheidong.com/blog/article/11224/c0a91766c1b8b7344c0a/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!