2014-01-08 26 views
1
import win32com.client 
outlook=win32com.clent.Dispatch("Outlook.Application").GetNamespace("MAPI") 
inbox=outlook.GetDefaultFolder(6) 
messages=inbox.Items 
message.messages.GetLast() 
body_content=message.Body 
print(body_content) 

这将在我的收件箱中打印电子邮件的正文。我想要做的是揭示了什么是这个代码的每个阶段发生的事情,让我可以更好地理解它,但是当我尝试打印收件箱,消息我得到:Python - 显示COM对象

<COMObject <unknown>> 

如何揭示这使我可以开始看到我在做什么。

我也在寻找一个地方,它有明确的文档围绕使用python与MS Outlook进行交互,如果任何人都可以共享。

回答

1

试一下:

Outlook对象模型中,大多数对象有Class属性,它返回OlObjectType类型的枚举,称它是什么类型的对象。 Outlook(甚至MS Office)对象的其他属性是ParentApplication

如果你真的想要,它应该很容易使一个函数describe_outlook_object返回一个字符串与有用的信息。你当然必须自己写。

或者,如果你只是想探索对象模型,你可能会比在Outlook中点击Alt + F11更糟糕,并与Visual Basic玩。 (你将不得不启用宏)

0

我不能评论还没有,但希望添加到Ben的回答(这让我非常类似的情况)

我想办法凑多个PST文件的电子邮件/账户在Outlook

import win32com.client 

outlook_object = win32com.client.Dispatch("Outlook.Application") 

namespace = outlook_object.GetNamespace("MAPI") 

# collection of accounts 
accounts = namespace.Folders 

# number of outlook accounts 
accounts_count = accounts.Count 

# .Item(1) not .Item(0) because counting starts at 1 
account1 = accounts.Item(1) 

# collection of folders for account1 
account_folders = account1.Folders 
# number of folders under outlook account 
account_folders_count = account_folders.Count 

# print account1 folder names 
for folder in range(account_folders_count): 
    # must be +1 because .Folder(0) and .Item(0) do not work 
    print str(folder+1)+":", account_folders.Item(folder+1) 

有使用Folders.CountFolders.Item(1)踏踏实实地消息的模式。希望这可以帮助某人,因为花了我数小时的时间才得到这一点。