Solved: convert outlook email to text file python

The main problem with converting Outlook email to text files is that Outlook does not always preserve formatting information in the email. This can make it difficult to understand the content of the email.

-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 ] result , data = mail .fetch(latest_email_id , “(RFC822)”) raw_email = data [ 0 ][ 1 ] # converts byte literal to string removing b” msg = email.message_from_string(raw_email) savepath = os.getcwd() + ‘/’ + str(msg[‘Subject’]) + ‘.txt’ # or savepath = ‘C:\Users\Downloads\’ strMsg=str(“Subject: “+msg[‘Subject’]+”

“+”From: “+msg[‘From’]+”

“+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 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you’re looking for? Browse other questions tagged 3.x email outlook text-files or ask your own question. asked 2 years ago viewed 2486 times active 2 years ago Blog Stack Overflow Podcast #93 – A Very Spolsky Halloween Special Related 9How do I read an email from Gmail with Python?0Python imaplib error when trying to connect to gmail2Python IMAP4 read only one message from Gmail inbox1Reading e-mails with python imaplib1Gmail IMAP IDLE not working on Ubuntu server0Get all messages from gmail account with python imaplib3Reading emails from gmail account in python3imaplib error when reading Gmail0Python ImapLib search function not working Hot Network Questions Why is it difficult for water waves to cancel each other? How can I get players back into a previous town they visited during character creation? Should I make my character suspect an upcoming twist or not? Should I use “a” or “an” when I abbreviate a word? How should a student’s inefficient calculation be pointed out? Why was there a robot in Rocky IV? What limits the speed of light in special relativity if information can travel faster than light between galaxies? Does AWS offer a way to route HTTPS traffic to two different EC2 instances based on directory path? Can you share some screenshots of editor’s control panels? How do organic chemistry mechanisms become accepted if they are never observed experimentally (like in ab initio molecular orbital theory)? What kind of approval does Donald Trump need to build his wall? If 60 is equal to LX—— Why isn’t 11 am + 1 hour == 12:00 pm? Dashed Tubes in 3D Is it accurate to compare the nuclearization of North Korea with that of Pakistan and India?” class fizzbuzz(): What limits the speed of light in special relativity if information can travel faster than light between galaxies?” class fizzbuzz(): How much smaller were medieval farm animals in England than todayWhy was Harry Truman chosen to be Vice President in 1944 over Henry Wallace?’average’: invalid syntax more hot questions question feed lang-py about us tour help blog chat data legal privacy policy work here advertising info mobile contact us feedback Technology Life / Arts Culture / Recreation Science Other Stack Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters Game Development TeX – LaTeX Software Engineering Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica Salesforce ExpressionEngine® Answers Cryptography Code Review Magento Signal Processing Raspberry Pi Programming Puzzles & Code Golf more (7) Photography Science Fiction & Fantasy Graphic Design Movies & TV Music: Practice & Theory Seasoned Advice (cooking) Home Improvement Personal Finance & Money Academia more (8) English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity English Language Learners Japanese Language Arqade (gaming) Bicycles Role-playing Games Anime & Manga Motor Vehicle Maintenance & Repair more (17) MathOverflow Mathematics Cross Validated (stats) Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more (3) Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent site design / logo © 2017 Stack Exchange Inc; user contributions licensed under cc by-sa 3.x by hosted on serverfault

Work with text files

There are a few ways to work with text files in Python. The simplest way is to open the file in a text editor like Notepad or TextEdit, and type your code.

Another way is to use the file object in Python. You can open the file using the open() function, and then use the read() and write() functions to read and write data to the file, respectively.

Finally, you can use the os.path module to access the path of a text file. You can then use the gettext() function to get information about the file, such as its name and size.

Outlook email

Outlook is a popular email client used by millions of people around the world. It is available on many different platforms, including Windows, MacOS, and Linux. Outlook has a rich feature set that allows you to manage your email, calendar, and contacts easily. In this tutorial, we will discuss how to work with Outlook email in Python.

First, you will need to create an instance of Outlook object. You can do this by using the built-in Outlook() function:

Outlook = Outlook()

Next, you will need to create an instance of the Email object. This object represents an individual email message in your mailbox:

email = Email( ‘YourEmailAddress’ )

You can then access various properties and methods of the Email object:

email.subject() print (email.subject)

‘Hello World!’

Related posts:

Leave a Comment