1

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)
  • 1
    Do you have an [MCVE](http://stackoverflow.com/help/mcve) example that you'd be willing to post? It would improve your post so that other might be able to help you. – Richard Erickson Dec 15 '15 at 15:33
  • 1
    Thanks Richard, I added a quick sample of something that would be similar to what I am trying to do. Any help is greatly appreciated! – CodinglyClueless Dec 15 '15 at 16:08
  • As an alternative to xlwt, you may want to consider openpyxl also: https://openpyxl.readthedocs.org/en/default/ – CodeJockey Dec 15 '15 at 17:29

1 Answers1

4

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

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78