已解決:將 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)

'你好世界!'

相關文章:

發表評論