Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Outlook To-Do-list using Python

I am accessing Outlook with the win32com module.

I want to get a hold of the task and flagged mails - Outlook has a lot of different names for them and look at them as to different type of "objects". However I want to get the list of Task Subjects and the due dates that appear when I press Task/To-Do List (Outlook 2010).

enter image description here

@utapyngo came up with a very useful C# code example - But I really need some help translating it to 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);
}
like image 881
Norfeldt Avatar asked Sep 17 '13 09:09

Norfeldt


People also ask

Is there a todo list in Outlook?

To view tasks and to-do lists in Outlook, select the Tasks icon at the bottom of the screen. You'll see two items listed under My Tasks: Tasks – Review a list of all your tasks. To-Do List – View items flagged in your emails.

What is the difference between Outlook To Do list and Tasks?

Unlike Tasks, To Do items are essentially a personal list of things to do that is accessed from the separate To Do section of Outlook. They are not included in the calendar. There are two ways to create a new item in the To Do list.


1 Answers

Here is an article that explains everything:

It is easy to confuse a To-do item with a task, but bear in mind that a To-do item can either be an e-mail, contact or task. An Outlook item becomes a to-do item as soon as you flag it for follow-up. To get a list of to-do items use the Outlook Namespace object to get a reference to the default to-do folder. Be careful though, you need to check the type of the objects before accessing their properties!

It also has many examples in C#. If you have experience with win32com, you should be able to translate them into Python.

EDIT: Here is one of them translated:

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))

EDIT: Fixed problems with encoding

like image 131
utapyngo Avatar answered Sep 26 '22 12:09

utapyngo