我是Python的新手。我需要从Outlook中提取电子邮件并获取所有属性/属性。
一个一个地检索属性,对于那些我知道它们存在的属性/属性来说,这样做很好(.Subject,.Body等)。
但是,我需要获得所有可能的属性。这就是我的问题所在。 我已经寻找了几个小时,唯一找到的答案都是使用:
- vars()
- dir()
- inspect.getmembers(obj)
__dict__
- 等等。
- .Subject
- .Body
- .SentOn
- 等等。
这是我测试笔记本的摘录:
####### Retrieve email from Outlook #######
import win32com.client
objOutlookMAPI=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
### Define folder
objOlFolder = objOutlookMAPI.GetDefaultFolder(6)
### Retrieve ant print email
objOlMessages = objOlFolder.Items
# objMessage : class 'win32com.client.CDispatch'
objMessage = objOlMessages.GetLast()
print(objMessage.Subject)
> Are you going to Las Vegas for Black Hat, DefCon, Bsides, or Hacking Diversity? Either or join us on our adventures!
vars(objMessage)
> {'_builtMethods_': {},
'_enum_': None,
'_lazydata_': (<PyITypeInfo at 0x0000021EC7B7D170 with obj at 0x0000021EC7B4B2F8>,
<PyITypeComp at 0x0000021EC7B7D620 with obj at 0x0000021EC7B4B058>),
'_mapCachedItems_': {},
'_oleobj_': <PyIDispatch at 0x0000021EC7B7D290 with obj at 0x0000021EC7B4AAA8>,
'_olerepr_': <win32com.client.build.LazyDispatchItem at 0x21ec8a7ba90>,
'_unicode_to_string_': None,
'_username_': 'GetLast'}
dir(objMessage)
#import inspect
#inspect.getmembers(objMessage)
> [`'_ApplyTypes_'`,
`'_FlagAsMethod'`,
`'_LazyAddAttr_'`,
`'_NewEnum'`,
`'_Release_'`,
`'__AttrToID__'`,
`'__LazyMap__'`,
`'__bool__'`,
`'__call__'`,
`'__class__'`,
`'__delattr__'`,
`'__dict__'`,
`'__dir__'`,
`'__doc__'`,
`'__eq__'`,
`'__format__'`,
`'__ge__'`,
`'__getattr__'`,
`'__getattribute__'`,
`'__getitem__'`,
`'__gt__'`,
`'__hash__'`,
`'__init__'`,
`'__init_subclass__'`,
`'__int__'`,
`'__le__'`,
`'__len__'`,
`'__lt__'`,
`'__module__'`,
`'__ne__'`,
`'__new__'`,
`'__reduce__'`,
`'__reduce_ex__'`,
`'__repr__'`,
`'__setattr__'`,
`'__setitem__'`,
`'__sizeof__'`,
`'__str__'`,
`'__subclasshook__'`,
`'__weakref__'`,
`'_builtMethods_'`,
`'_enum_'`,
`'_find_dispatch_type_'`,
`'_get_good_object_'`,
`'_get_good_single_object_'`,
`'_lazydata_'`,
`'_make_method_'`,
`'_mapCachedItems_'`,
`'_oleobj_'`,
`'_olerepr_'`,
`'_print_details_'`,
`'_proc_'`,
`'_unicode_to_string_'`,
`'_username_'`,
`'_wrap_dispatch_'`]
__getattr__
或__getattribute__
中可能有一些魔法。 - Stephen Rauchwin32com
无法解决这个问题。但是:(1)如果它是支持IDispatchEx
的IDispatch
接口,您可以直接QueryInterface
到该接口并调用GetIDsOfNames
方法。(2)如果它是不支持IDispatchEx
的IDispatch
接口,则可以在其上调用GetTypeInfo
。(2a)如果生成的ITypeInfo
支持ITypeInfo2
,则可以QI该接口并对其进行枚举。(2b)如果不支持,但它确实支持GetTypeAttr
和GetFuncDesc
,则可以使用这些方法。(3)如果都不是,则没有办法。 - abarnertwin32com
搜索这些方法名称,看看是否有任何示例。我在快速搜索中没有找到任何内容,但我找到了这个问题,针对ATL,它涵盖了如何使用所有这些方法,如果您知道如何从C++进行翻译。 - abarnert