借鉴转载网址: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"):])
这个处理是自己的业务需求,不需要的可以删掉。