1

I am attempting to use win32com library to download outlook emails from a shared mailbox.

My code is below:

import win32com.client
import win32com
import pandas as pd
from unidecode import unidecode

import datetime

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

folder = outlook.Folders.Item("My Shared Mailbox Name")
inbox = folder.Folders.Item("Inbox")

msg = inbox.Items

subject = []
body = []
sender = []
sendermail = []
senttime = []

for message in msg:
    subject.append(message.Subject)
    body.append(message.Body)
    sender.append(unidecode(message.SenderName))
    sendermail.append(unidecode(message.SenderEmailAddress))
    senttime.append(message.SentOn)

maildata = pd.DataFrame({'Subject':subject,'Mail Body':body, 'Sender Email Address': sendermail, 'Sender Name': sender, 'Sent Time': senttime})

The error happens in the last line when I try and convert the lists into a DataFrame.

I receive below error:

AttributeError: 'pywintypes.datetime' object has no attribute 'nanosecond'

The time is extracted in a different format that what I am used to:

pywintypes.datetime(2020, 11, 24, 14, 59, 9, tzinfo=TimeZoneInfo('GMT Standard Time', True))

Since I am using win32com library, the time is extracted in pywintypes.datetime format which I am unable to convert into a format which can be added to a DataFrame.

I looked around and it seems that the documentation for win32com library isn't great.

I did find Tim Golden's Python stuff and a few threads like this one which didn't solve the problem. I am sure its pretty much in front of my eyes and I am unable to put 2 and 2 together.

I would appreciate if someone can point me in the right direction on how to convert .

excelman
  • 189
  • 1
  • 2
  • 10

3 Answers3

1

I know it's a only a workaround, but you could convert the pywintypes.datetime to a string (and slice off the extra trailing characters) before converting it to a pandas datetime.

message = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(6).Items.GetLast()
pd.to_datetime(str(message.senton)[:-6])
1

It seems that the timezone creates the problem.

key_time_var = 'Sent Time' ## The column containing the datetime data
df[key_time_var] = df[key_time_var].dt.tz_convert(None)

Something like this can remove the Timezone and then it should work

  • +1 pandas do read the data, but than I couldn't even see the dataframe - this works and it's the simplest solution imho – lexc May 11 '23 at 11:08
0

I faced the exact same issue. My solution is as follows;

d = mail.receivedtime.date()

d will be like datetime.date(2016, 4, 6)

Also you can get

t = mail.receivedtime.time()

t will be like datetime.time(23, 15, 13)

And you combine them in this way:

newdate = datetime.datetime.combine(d,t)

Then you can get what you want!

I believe this is late answer for you, but I do hope it will be helpful for other people who faces the same issue!

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
Shin
  • 1
  • 1