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

Nor*_*ldt 10 python windows outlook-2010 python-2.7

我正在使用win32com模块访问Outlook .

我希望得到一个任务并标记邮件--Outlook有很多不同的名称,并查看它们的不同类型的"对象".但是,我想获取任务主题列表以及按下任务/待办事项列表(Outlook 2010)时显示的截止日期.

在此输入图像描述

@utapyngo提出了一个非常有用的C#代码示例 - 但我真的需要一些帮助将它转换为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);
}
Run Code Online (Sandbox Code Playgroud)

uta*_*ngo 8

这篇文章解释了一切:

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

编辑:修复了编码问题