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

时间:2013-09-17 09:01:07

标签: python windows python-2.7 outlook-2010

我使用win32com module访问Outlook。

我希望掌握任务并标记邮件 - Outlook为它们提供了许多不同的名称,并将它们视为不同类型的"对象"。但是,我想获取任务主题列表以及按任务/待办事项列表(Outlook 2010)时显示的截止日期。

enter image description here

@utapyngo想出了一个very useful C# code example - 但我真的需要一些帮助将它翻译成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);
}

1 个答案:

答案 0 :(得分:8)

Here is an article解释了一切:

  

将待办事项与任务混淆是很容易的,但请记住,待办事项可以是电子邮件,联系人或任务。只要您将其标记为后续操作,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))

编辑:修复了编码问题