使用Python获取Outlook待办事项列表

使用Python获取Outlook待办事项列表,python,windows,python-2.7,outlook-2010,Python,Windows,Python 2.7,Outlook 2010,我正在使用访问Outlook 我想了解一下任务和标记的邮件——Outlook对它们有很多不同的名称,并将它们视为不同类型的“对象”。但是,我想获取任务主题列表和在按下任务/待办事项列表时显示的到期日期(Outlook 2010) @utapyngo提出了一个-但我真的需要一些帮助将其翻译成python Outlook.NameSpace ns = null; Outlook.MAPIFolder todoFolder = null; Outlook.Items todoFolderItems

我正在使用访问Outlook

我想了解一下任务和标记的邮件——Outlook对它们有很多不同的名称,并将它们视为不同类型的“对象”。但是,我想获取任务主题列表和在按下任务/待办事项列表时显示的到期日期(Outlook 2010)

@utapyngo提出了一个-但我真的需要一些帮助将其翻译成python

Outlook.NameSpace ns = null;
Outlook.MAPIFolder todoFolder = null;
Outlook.Items todoFolderItems = null;
Outlook.TaskItem task = null;
Outlook.ContactItem contact = null;
Outlook.MailItem email = null;
string todoString = string.Empty;

try
{
    ns = OutlookApp.Session;
    todoFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderToDo);
    todoFolderItems = todoFolder.Items;

    for (int i = 1; i <= todoFolderItems.Count; i++)
    {
        object outlookItem = todoFolderItems[i];
        if (outlookItem is Outlook.MailItem)
        {
            email = outlookItem as Outlook.MailItem;
            todoString += String.Format("Email: {0} Due:{1}{2}",
                email.Subject, email.TaskDueDate, Environment.NewLine);
        }
        else if (outlookItem is Outlook.ContactItem)
        {
            contact = outlookItem as Outlook.ContactItem;
            todoString += String.Format("Contact: {0} Due:{1}{2}",
                contact.FullName, contact.TaskDueDate, Environment.NewLine);
        }
        else if (outlookItem is Outlook.TaskItem)
        {
            task = outlookItem as Outlook.TaskItem;
            todoString += String.Format("Task: {0} Due: {1}{2}",
                task.Subject, task.DueDate, Environment.NewLine);
        }
        else
            MessageBox.Show("Unknown Item type");
        Marshal.ReleaseComObject(outlookItem);
    }
    MessageBox.Show(todoString);
}
finally
{
    if (todoFolderItems != null)
        Marshal.ReleaseComObject(todoFolderItems);
    if (todoFolder != null)
        Marshal.ReleaseComObject(todoFolder);
    if (ns != null)
        Marshal.ReleaseComObject(ns);
}
Outlook.NameSpace ns=null;
Outlook.MAPIFolder todoFolder=null;
Outlook.Items todoFolderItems=null;
Outlook.TaskItem任务=null;
Outlook.ContactItem contact=null;
Outlook.MailItem电子邮件=空;
string todoString=string.Empty;
尝试
{
ns=OutlookApp.Session;
todoFolder=ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderToDo);
todoFolderItems=todoFolder.Items;
对于(inti=1;i,它解释了一切:

很容易将待办事项与任务混淆,但请记住,待办事项可以是电子邮件、联系人或任务。Outlook项目一旦标记为待办事项以供后续操作,就会成为待办事项。若要获取待办事项列表,请使用Outlook命名空间对象获取对默认待办文件夹的引用。不过,请小心,您需要进行检查在访问对象的属性之前,对象的类型

它还有许多C#中的示例。如果您有使用win32com的经验,您应该能够将它们翻译成Python

编辑:以下是其中一个翻译:

import sys
import win32com.client

olFolderTodo = 28

outlook = win32com.client.Dispatch("Outlook.Application")
ns = outlook.GetNamespace("MAPI")
todo_folder = ns.GetDefaultFolder(olFolderTodo)
todo_items = todo_folder.Items

def print_encoded(s):
    print s.encode(sys.stdout.encoding, 'replace')

for i in range(1, 1 + len(todo_items)):
    item = todo_items[i]
    if item.__class__.__name__ == '_MailItem':
        print_encoded(u'Email: {0}. Due: {1}'.format(item.Subject, item.TaskDueDate))
    elif item.__class__.__name__ == '_ContactItem':
        print_encoded(u'Contact: {0}. Due: {1}'.format(item.FullName, item.TaskDueDate))
    elif item.__class__.__name__ == '_TaskItem':
        print_encoded(u'Task: {0}. Due: {1}'.format(item.Subject, item.DueDate))
    else:
        print_encoded(u'Unknown Item type: {0}'.format(item))

编辑:修复了编码问题

我很喜欢你的答案,但发现将C#代码翻译成python非常困难。win32com非常有用,但大多数人都承认,大多数时候我看不到逻辑,只看到结果。@Norfeldt,我已经翻译了你要求的示例。它对你有用吗?先生,你真是太棒了我!非常感谢!我如何检查任务/待办事项是否标记为已完成-因为我只想要一个未完成任务的列表?我得到
UnicodeEncodeError:'charmap'编解码器无法在位置36:字符映射到
中对字符u'\u2013'进行编码-我尝试了
打印电子邮件:{0}。到期:{1}。格式(item.Subject,item.TaskDueDate,编码为='utf-8')