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

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

import win32com.client
import oscount = 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.Countattachments = msg.Attachmentsif 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 oscount = 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.Countattachments = msg.Attachmentsif 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"):])

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

Python 读取.msg文件中的附件和内容相关推荐

  1. python打开excel的函数-Python读取excel文件中带公式的值的实现

    在进行excel文件读取的时候,我自己设置了部分直接从公式获取单元格的值 但是用之前的读取方法进行读取的时候,返回值为空 import os import xlrd from xlutils.copy ...

  2. python获取股指_用Python读取csv文件中的沪深300指数历史交易数据

    保存路径:D:\python\用Python读取csv文件中的沪深300指数历史交易数据 程序名称:readcsvhs300.py: 数据名称:沪深300指数历史交易数据.csv: 开发环境:Win7 ...

  3. 【C++】读取txt文件中指定行的内容

    使用c++读取TXT文件中指定行的内容 classification_classes_ILSVRC2012.txt:下载链接 验证: #include <iostream> #includ ...

  4. python读取xlsx文件中的日期

    因为在python中直接读取xlsx文件中的日期,而不进行额外处理的话,会读取错误--即:只能读取成浮点数,而不能以日期的格式获取. 导入xlrd 首先保证要导入xlrd,若没有,可执行下面语句下载 ...

  5. 用python读取txt文件中的数据并画各类图形展示_Python实现读取txt文件中的数据并绘制出图形操作示例...

    本文实例讲述了Python实现读取txt文件中的数据并绘制出图形操作.分享给大家供大家参考,具体如下: 下面的是某一文本文件中的数据. 6.1101,17.592 5.5277,9.1302 8.51 ...

  6. Python读取excel文件中的数据,绘制折线图、散点图

    https://www.cnblogs.com/liulinghua90/p/9935642.html https://blog.csdn.net/qq_32458499/article/detail ...

  7. python读取html文件中的表格数据_Python 读取各类文件格式的文本信息 | doc,excel,html,mht...

    原标题:Python 读取各类文件格式的文本信息 | doc,excel,html,mht 众所周知,python最强大的地方在于,python社区汇总拥有丰富的第三方库,开源的特性,使得有越来越多的 ...

  8. c/c++读取txt文件中指定行的内容_和尧名大叔一起从0开始学Python编程-简单读写文件

    0基础自学编程是很痛苦的一件事情,所以我想把自己学习的这个过程记录下来,让想学编程的人少走弯路,大叔文化程度较低,可能会犯一些错误,欢迎大家督促我. 今天,我们来学习一下用Python简单读写文件,这 ...

  9. python读取txt文件中的数字_python从txt文件读取数据

    (作为一个python初学者,记录一点学习期间的笔记,方便日后查阅,若有错误或者更加便捷的方法,望指出!) 1.读取TXT文件数据,并对其中部分数据进行划分.一部分作为训练集数据,一部分作为测试集数据 ...

最新文章

  1. 原子层沉积(ALD)和化学气相沉积(CVD)微电子制造铜金属化的研究进展
  2. Python(四)IP代理
  3. 【学习笔记】Python - Lxml
  4. 从源码的角度说说Activity的setContentView的原理
  5. oracle last day比较,PLSQL LAST_DAY用法及代码示例
  6. python在数据分析方面的应用、下列说法正确_智慧树知到大数据分析的python基础答案...
  7. 在Python中使用try-except-else是否是一种好习惯?
  8. 蓝桥杯2017年第八届C/C++省赛B组第四题-方格分割
  9. linux源代码分析和阅读工具比较
  10. 数学建模MATLAB难不难,如何才能在数学建模竞赛中取得好成绩
  11. 【数据库】unity3d实现连接mysql 下载安装连接全过程
  12. 三分钟快速了解怎么查询ip地址,怎么更换ip地址!
  13. 百度地图api根据详细地址反查坐标
  14. 计算机专业武汉的就业方向,计算机专业好就业吗
  15. python发明家_Python版多图表范例
  16. UR机器人装箱姿态_UR10 RG2机械臂手臂+RealsenseZR300 机器人手眼标定 系列第四篇
  17. 2021年G1工业锅炉司炉证考试及G1工业锅炉司炉作业考试题库
  18. Excel 金额大写
  19. 智能小区计算机网络系统,浅谈计算机网络系统在智能小区实现与运用.doc
  20. uniapp中微信扫码打开下载链接添加引导页

热门文章

  1. Java中的i=i+1和i+=1以及i++和++i详解
  2. Arrays.deepToString()与Arrays.toString()的区别
  3. 网络营销理论研究:网络营销信息传递原理及其实践价值
  4. python预测股票价格tushare_Python:tushare获取A股指数数据,使用LSTM进行预测,pythontushare,并...
  5. 报如下错误:android.util.AndroidRuntimeException: You cannot combine custom titles with other title featur
  6. 【KATA练习日记】关于std::accumulate的使用
  7. 硬件加油站 | 传感器 - PIR 人体红外传感器
  8. Android音视频开发入门(5)使用LAME编码一个PCM文件,为了跳槽强刷1000道Android真题
  9. 笔记(待续)-动力学逆问题相关基础知识
  10. 黑苹果一些奇怪的事情