Python Forum
Read All Emails from Outlook and add the word counts to a DataFrame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read All Emails from Outlook and add the word counts to a DataFrame
#1
import win32com.client
import os
import time
import datetime as dt
import re
import pandas as pd

DateFilter = dt.datetime.now() - dt.timedelta(days = 2)

#Outlook MAPI
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
#Inbox Folder
inbox = outlook.GetDefaultFolder(6)
root_folder = outlook.Folders.Item(1)
print(root_folder.Name)
#Sort emails in inbox
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
#Filter emails to go through
DateFilterMsg = messages.Restrict("[ReceivedTime] >= '" + DateFilter.strftime('%m/%d/%Y %H:%M %p')+"'")

for message in messages:
if message.Class==43: # Get COM Objects.
body = message.HTMLBody.lower()
content = ('Body: {}'.format(body))#format
words = message.Body.split(" ")#split
words.sort()#sort
uniquewords = list((word,words.count(word)) for word in set(words))
df = pd.DataFrame(uniquewords, columns = ['word_name','word_cnt']) #Word Name and respective Counts
print(df)
This is how get the output, May be it is breaking the word counts by email. I want to see all the word counts in one dataFrame and not by breakUp. For example word system should give me a count of 2 and not two separate tables.

Output:
Index word_name word_cnt 0 System 1 Index word_name word_cnt 0 System 1
Yoriz write Sep-15-2022, 11:53 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Note I added code tags but the code you pasted has no indentation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting column of values into muliple columns of counts highland44 0 257 Feb-01-2024, 12:48 AM
Last Post: highland44
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,419 Oct-06-2023, 04:32 PM
Last Post: cubangt
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 759 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Search Outlook Inbox for set of values cubangt 1 1,102 Jun-28-2023, 09:29 PM
Last Post: cubangt
  Save image from outlook email cubangt 1 707 Jun-07-2023, 06:52 PM
Last Post: cubangt
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,621 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  PyPi Download Counts and Warehouse Migration? tlee0818 1 1,306 Mar-20-2022, 07:41 PM
Last Post: snippsat
  Sending Emails on Autopilot Gyga_Hawk 3 1,696 Mar-15-2022, 08:20 AM
Last Post: Larz60+
  Mark outlook emails as read using Python! shane88 2 6,572 Feb-24-2022, 11:19 PM
Last Post: Pedroski55
  Trying out the parsing/reading of emails from my outlook cubangt 0 6,185 Jan-12-2022, 08:59 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020