已解决:将 outlook 电子邮件转换为文本文件 python

将 Outlook 电子邮件转换为文本文件的主要问题是 Outlook 并不总是保留电子邮件中的格式信息。 这可能会让人难以理解电子邮件的内容。

-3.x email outlook text-files share|improve this question edited Jan 14 '15 at 8:13 asked Jan 14 '15 at 7:51 user3775557 1,8271133 add a comment| 2 Answers 2 active oldest votes up vote 0 down vote accepted You can use the email module to read the message and then save it to a file. The following code will read the message from your inbox and save it as a text file in your current directory. import os import imaplib import email def save_attachment(msg, download_folder="/tmp"): """Given a message, save its attachments to the specified download folder (default is /tmp)""" att_path = "No attachment found." for part in msg.walk(): if part.get_content_maintype() == 'multipart': continue if part.get('Content-Disposition') is None: continue filename = part.get_filename() if filename is not None: sv_path = os.path.join(download_folder, filename) content = part.get_payload(decode=True) try: with open(sv_path, 'wb') as fp: fp.write(content) except IOError: print("Error saving attachment to", sv_path) return att_path return sv-path userName = "yourmail@gmailcom" password = "yourpassword" mail = imaplib.IMAP4('imap-mailhost') mail .login(userName , password ) mail .select("inbox") result , data 

= mail .search(None, “ALL”) ids = data [ 0 ] id_list = ids.split() latest_email_id = id_list[- 1 ] 结果,data = mail .fetch(latest_email_id , “(RFC822)”) raw_email = data [ 0 ][ 1 ] # 将字节文字转换为字符串删除 b” msg = email.message_from_string(raw_email) savepath = os.getcwd() + '/' + str(msg['Subject']) + '.txt' #或 savepath = 'C:\Users\Downloads\' strMsg=str(“主题:”+msg['主题']+”

“+”发件人:“+msg['发件人']+”

“+msg.get_payload()) fp=open(savepath,'w') fp.write(strMsg) fp.close() share|improve this answer edited Jan 14 '15 at 8:13 answered Jan 14'15 at 7 :54 user3775557 1,8271133 添加评论| 您的答案草稿已保存草稿已丢弃注册或登录使用 Google 注册使用 Facebook 注册使用电子邮件和密码注册以访客身份发布电子邮件姓名电子邮件以访客身份发布姓名电子邮件丢弃发布您的答案即表示您同意隐私政策和服务条款。 不是您要找的答案? 浏览其他标记为 3.x email outlook text-files 的问题或提出您自己的问题。 问 2 年前 浏览 2486 次活跃 2 年前 博客堆栈溢出播客 #93 – 一个非常 Spolsky 万圣节特别相关 9我如何使用 Python 阅读来自 Gmail 的电子邮件?0尝试连接到 gmail 时出现 Python imaplib 错误2Python IMAP4 只读取来自Gmail收件箱1使用python imaplib阅读邮件1Gmail IMAP IDLE not working on Ubuntu server0使用python imaplib从gmail账户获取所有邮件3使用python阅读来自gmail账户的邮件3阅读Gmail时出现imaplib错误0Python ImapLib搜索功能不工作 网络热点问题 为什么水波很难取消彼此? 我怎样才能让玩家回到他们在角色创建期间访问过的先前城镇? 我是否应该让我的角色怀疑即将发生的转折? 缩写单词时应该使用“a”还是“an”? 如何指出学生计算效率低下? 为什么《洛奇 IV》中会有机器人? 如果信息可以比星系间的光速传播得更快,那么狭义相对论中的光速是什么限制的? AWS 是否提供一种方法来根据目录路径将 HTTPS 流量路由到两个不同的 EC2 实例? 你能分享一些编辑器控制面板的截图吗? 如果从未通过实验观察到有机化学机制(如从头算分子轨道理论),它们如何被接受? 唐纳德特朗普需要什么样的批准才能建造他的墙? 如果 60 等于 LX—— 为什么不是上午 11 点 + 1 小时 == 中午 12:00?

处理文本文件

在 Python 中有几种处理文本文件的方法。 最简单的方法是在记事本或 TextEdit 等文本编辑器中打开文件,然后键入您的代码。

另一种方法是使用 Python 中的文件对象。 您可以使用 open() 函数打开文件,然后使用 read() 和 write() 函数分别读取和写入数据到文件中。

最后,您可以使用 os.path 模块来访问文本文件的路径。 然后,您可以使用 gettext() 函数获取有关文件的信息,例如文件的名称和大小。

Outlook电子邮件

Outlook 是全球数百万人使用的流行电子邮件客户端。 它可以在许多不同的平台上使用,包括 Windows、MacOS 和 Linux。 Outlook 具有丰富的功能集,可让您轻松管理电子邮件、日历和联系人。 在本教程中,我们将讨论如何在 Python 中使用 Outlook 电子邮件。

首先,您需要创建 Outlook 对象的实例。 您可以使用内置的 Outlook() 函数来执行此操作:

展望 = 展望()

接下来,您需要创建电子邮件对象的实例。 此对象代表您邮箱中的一封电子邮件:

电子邮件 = 电子邮件('您的电子邮件地址')

然后,您可以访问 Email 对象的各种属性和方法:

email.subject() 打印(email.subject)

'你好世界!'

相关文章:

发表评论