gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:31:35 24 4
gpt4 key购买 nike

我正在使用 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);
}

最佳答案

Here is an article这解释了一切:

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!

它还有许多 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))

编辑:修复了编码问题

关于python - 使用 Python 获取 Outlook 待办事项列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18845512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com