Python - 显示一个 COM 对象


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)

这将在我的收件箱中打印电子邮件的正文。 我想做的是揭示此代码的每个阶段发生了什么,以便我可以更好地理解它,但是当我尝试打印收件箱时,我收到的消息:

<COMObject <unknown>>

如何揭示这一点,以便我可以开始看到我正在使用什么。

我也在寻找一个有关于使用python与MS Outlook交互的明确文档的地方,如果有人可以分享的话。

在这里试试:

  • http://msdn.microsoft.com/en-us/library/office/aa221870(v=office.11).aspx

在 Outlook 对象模型中,大多数对象都有一个 Class 属性,该属性返回类型为 OlObjectType 的枚举,说明它是什么类型的对象。展望(实际上是MS Office)对象共有的其他属性是ParentApplication

如果你真的想这样做,应该很容易创建一个函数describe_outlook_object它返回一个包含有用信息的字符串。当然,您必须自己编写。

或者,如果您只想探索对象模型,则可以比在Outlook中按Alt + F11并使用Visual Basic更糟糕。(您必须启用宏。

我还

不能发表评论,但想补充 Ben 的答案(这在类似情况下对我有很大帮助)

我想要一种方法从Outlook中的多个PST文件/帐户中抓取电子邮件

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)来获取消息是一种模式。 希望这对某人有所帮助,因为我花了几个小时的谷歌搜索才达到这一点。

最新更新