'Pulling paragraphs from outlook emails and importing them into .txt or .doc
so the script I'm trying to write in Python would search for certain subjects such as "City status update" and "City issues" in my inbox and search for variables such as the city names. if the city names are in the email then it would pull that whole line of text to the period (.) and load it into a .txt file. Such as "Detroit: Cakes 15, Pies 12, Drinks 19." and not pull anything else. Below are two examples of the format for the emails along with what I have so far.
Subject: City status update
Detroit: Cakes 15, Pies 12, Drinks 19. New York: Cakes 9, Pies 14, Drinks 2.
Subject: City issues
Detroit: 2 lost items, 2 unclaimed, 6 delivered.
Chicago: 5 lost items, 1 unclaimed, 9 delivered.
New York: 7 lost items, 3 unclaimed, 16 delivered.
import win32com.client
import os, re, html
outlook = win32com.client.Dispatch("OutlookApplication").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages.Sort("[ReceivedTime]", True)
messages = inbox.items
message = messages.GetFirst()
subject = message.Subject
city = input("What city/cities are you looking for?")
for message in messages:
if message.Subject ["City status update, City issues"]
name =str(message.subject)
regex = re.search(r"city", msg.HTMLbody)
body = regex.group()
OlSaveAsType = {
"olTXT": 0,
"olRTF": 1,
"olTemplate": 2,
"olMSG": 3,
"olDoc": 4,
"olHTML": 5,
"olVCard": 6,
"olVCal": 7,
"olICal": 8
}
message.SaveAs(os.getcwd()+'//'+name, OlSaveAsType(['olTXT])
results = body
r = re.compile('.*/.*/.*:.*')
if results, True
print(results - ["."])
else()
This is what I have so far and a bunch of errors.
Solution 1:[1]
First of all, there is no need to iterate over all items in a folder.
for message in messages:
if message.Subject ["City status update, City issues"]
name =str(message.subject)
regex = re.search(r"city", msg.HTMLbody)
body = regex.group()
Instead, the Outlook object model provides the Find/FindNext or Restrict methods of the Items class. Read more about them in the following articles:
- How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
- How To: Use Restrict method to retrieve Outlook mail items from a folder
To deal with a message body I'd suggest using the Word object model. The WordEditor property of the Inspector class returns the Microsoft Word Document Object Model of the message being displayed.
The Outlook object model supports three main ways of customizing the message body:
- The Body property returns or sets a string representing the clear-text body of the Outlook item.
- The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the
HTMLBodyproperty will always update the Body property immediately. - The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eugene Astafiev |
