Python 读取.msg文件中的附件和内容

借鉴转载网址:https://www.cnblogs.com/rongge95500/p/12753408.html

在网上看到一段代码,自己加了个文件名字空格字符的处理函数,感觉python很酷。

import win32com.client
import os

count = 0
root_path = "C:/xxx/xxx/Desktop/xxx/"

def get_attachments(file_name, path_name):
    """
    获取.msg文件内的附件
    :param file_name: .msg文件路径
    :param path_name: 附件存放目录
    :return: None
    """
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    msg = outlook.OpenSharedItem(file_name)
    
    count_attachments = msg.Attachments.Count
    attachments = msg.Attachments
    if count_attachments > 0:
        for att in attachments:
            att.SaveAsFile(os.path.join(path_name, att.FileName))
 
    del outlook, msg  # 释放资源

def get_all_attachments():   
          for root, dirs, files in os.walk(root_path):  # root, dirs不能删掉,否则程序报错
              for file in files:  
                  get_attachments(os.path.join(root_path, file),root_path)
                  
def delete_space():
    for file_name in os.listdir(root_path):
        if len(file_name.split(" ")) >1:
            os.rename(os.path.join(root_path,file_name),
                      os.path.join(root_path,file_name.replace(" ","_")))
            


delete_space()              
get_all_attachments()

同事又加强了下,增加了提取邮件中内容的代码部分,厉害厉害。

import win32com.client
import os

count = 0
root_path = "C:/xxx/xxx/"


def get_attachments(file_name, path_name):
    """
    获取.msg文件内的附件
    :param file_name: .msg文件路径
    :param path_name: 附件存放目录
    :return: None
    """
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    msg = outlook.OpenSharedItem(file_name)

    count_attachments = msg.Attachments.Count
    attachments = msg.Attachments

    if count_attachments > 0:
        for att in attachments:
            att.SaveAsFile(os.path.join(path_name, att.FileName))
            with open(root_path+att.FileName+".emailbody.txt", 'w') as file_object:
                if (msg.body[0:16]=="xxx"):
                    file_object.write(msg.body[msg.body.find("xxx"):])
                else:
                    file_object.write(msg.body)
                #print('#'.join(list(set(msg.To.split(";")))))
                file_object.write('\n')
                sortedList = list(set(msg.To.split(";")))
                sortedList.sort()
                #print('#'.join(sortedList))
                file_object.write('#'.join(sortedList))

    del outlook, msg, file_object  # 释放资源


def get_all_attachments():
    for root, dirs, files in os.walk(root_path):  # root, dirs不能删掉,否则程序报错
        for file in files:
            get_attachments(os.path.join(root_path, file), root_path)


def delete_space():
    for file_name in os.listdir(root_path):
        if len(file_name.split(" ")) > 1:
            os.rename(os.path.join(root_path, file_name),
                      os.path.join(root_path, file_name.replace(" ", "_")))


delete_space()
get_all_attachments()

注意“xxx”的地方记得替换哦。另外:

if (msg.body[0:16]=="xxx"):
                    file_object.write(msg.body[msg.body.find("xxx"):])

这个处理是自己的业务需求,不需要的可以删掉。

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值