CodinglyClueless
CodinglyClueless

Reputation: 82

Write Excel file from Outlook Emails

I have written some code in Python that reads through emails in an Outlook folder using win32com.client. I can read it in easily, have all of the logic built out, now want to write an Excel file as I iterate through all of the messages. This is where I have a problem.

My latest attempt used xlwt but I am open to using anything. The problem I am running into is that when I try to write a cell with the Sender or the Date from the outlook email, I get the following error:

Exception: Unexpected data type

Does anyone know how I can fix this/get around it?

Do I have to convert the .Sender, .Date instances to some other form?

Quick sample below:

import win32com.client
import xlwt
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
book = xlwt.Workbook(encoding="utf-8")
sheet = book.add_sheet("New Sheet")
for folder in inbox_folders:
    fold = folder.Items
    for messages in fold:
        date = fold.ReceivedTime
        sender = fold.Sender
        sheet.write(1,0,date)
        sheet.write(2,0,sender)

Upvotes: 1

Views: 4127

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66341

Replace sheet.write(2,0,sender) with sheet.write(2,0,sender.Name) or sheet.write(2,0,sender.Address).

Upvotes: 4

Related Questions