简体   繁体   English

从Outlook电子邮件中写入Excel文件

[英]Write Excel file from Outlook Emails

I have written some code in Python that reads through emails in an Outlook folder using win32com.client. 我已经用Python编写了一些代码,这些代码使用win32com.client读取Outlook文件夹中的电子邮件。 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. 我可以轻松阅读它,拥有所有逻辑,现在想在遍历所有消息时编写一个Excel文件。 This is where I have a problem. 这是我有问题的地方。

My latest attempt used xlwt but I am open to using anything. 我最近的尝试是使用xlwt,但是我愿意使用任何东西。 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: 我遇到的问题是,当我尝试使用Outlook电子邮件中的“发件人”或“日期”编写单元格时,出现以下错误:

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? 我是否需要将.Sender,.Date实例转换为其他形式?

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)

sheet.write(2,0,sender.Name)sheet.write(2,0,sender.Address).替换sheet.write(2,0,sender) sheet.write(2,0,sender.Address).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM