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 .