利用python批量获取多个outlook邮件附件的方法

利用python批量获取多个outlook邮件附件的方法

import os
import extract_msg
#批量获取多个outlook邮件附件的方法:
#步骤0:pip install extract_msg #说明:依赖的包比较多推荐用python虚拟环境
#步骤1:在outlook中选中对应的邮件保存到文件夹
#步骤2:遍历文件夹找出带msg后缀的所有文件,参考:https://blog.csdn.net/cryhelyxx/article/details/45219947
#步骤3:提取msg文件中的邮件附件,参考:https://www.cnblogs.com/rong-z/p/12753408.html

def  read_fujian(file_name):
    msg = extract_msg.Message(file_name)
    """
    print(msg.subject)
    print(msg.cc)
    print(msg.bcc)
    print(msg.sender)
    print(msg.to)
    print(msg.path)
    print(msg.attachments)
    print(msg.date)
    print(msg.body)
    print(msg.htmlBody)
    """
    for attachment in msg.attachments:
        attachment.save(customPath='./file')


def getFileName(path):
    ''' 获取指定目录下的所有指定后缀的文件名 '''

    f_list = os.listdir(path)
    # print f_list
    for i in f_list:
        # os.path.splitext():分离文件名与扩展名
        if os.path.splitext(i)[1] == '.msg':
            print(i)
            #保存所有附件
            read_fujian(i)

if __name__ == '__main__':

    path = '.'
    getFileName(path)

猜你喜欢

转载自blog.csdn.net/sinat_24354307/article/details/129813452
今日推荐