Я изучаю win32com для python, и у меня странная проблема.
Я пытаюсь экспортировать контакты Outlook в список словаря. Мой код отлично работает с win32com.client.Dispatch ("Outlook.Application). Но он возвращает 0 контактов с win32com.client.gencache.EnsureDispatch (" Outlook.Application), который должен быть быстрее и безопаснее ". Вот мой код:
class MapiImport():
def __init__(self):
self.olApp = win32com.client.Dispatch("Outlook.Application")
self.namespace = self.olApp.GetNamespace(u"MAPI")
# olFolderContacts = 10 :
self.mapiContacts = self.namespace.GetDefaultFolder(10).Items
def getContacts(self, *fields):
contacts = []
# Class == 40 is ContactItem
# Class == 69 is DistListItem
# Exclude ditribution list and others objects != ContactItem
for contact in filter(lambda x: x.Class == 40,self.mapiContacts) :
if not fields :
ctact = dict((x.Name,x.Value) for x in contact.ItemProperties)
else :
ctact = {}
for field in fields :
itemProp = contact.itemProperties[field]
ctact[field] = itemProp.Value
contacts.append(ctact)
return contacts
#====TEST SCRIPT====
myMAPI = MapiImport()
fields = (u"LastName",u"FirstName",u"Companies",
u"HomeTelephoneNumber",u"Home2TelephoneNumber",
u"MobileTelephoneNumber",
u"BusinessTelephoneNumber",u"Business2TelephoneNumber",
u"Email1Address",u"Email2Address",u"Email3Address",
u"HomeAddress",u"BusinessAddress",
u"Birthday",u"Anniversary",
u"Body")
print(myMAPI.getContacts(*fields))
Итак, когда я заменяю:
olApp = win32com.client.Dispatch("Outlook.Application")
С участием :
olApp = win32com.client.gencache.EnsureDispatch("Outlook.Application")
Он возвращает следующие ошибки:
Traceback (most recent call last):
File "D:\Documents and Settings\da7950\Mes documents\Dropbox\cheetahImporter\mapiImport.py", line 42, in <module>
print(myMAPI.getContacts(*fields))
File "D:\Documents and Settings\da7950\Mes documents\Dropbox\cheetahImporter\mapiImport.py", line 19, in getContacts
for contact in filter(lambda x: x.Class == 40,self.mapiContacts) :
File "D:\Documents and Settings\da7950\Mes documents\Python27\lib\site-packages\win32com\gen_py\00062FFF-0000-0000-C000-000000000046x0x9x2\_Items.py", line 122, in __getitem__
return self._get_good_object_(self._oleobj_.Invoke(*(81, LCID, 1, 1, item)), "Item")
com_error: (-2147352567, "Une exception s'est produite.", (4096, u'Microsoft Office Outlook', u'Index de la matrice en dehors des limites.', None, 0, -2147352567), None)
Сообщение означает «Матричный индекс выходит за границы». Самое странное, что после того, как я вызвал EnsureDispatch, win32com.client.Dispatch больше не работает. Мне нужно удалить pywin32 и переустановить его ...
Я использую 64-разрядную версию Python2.7.3 с 32-разрядной версией Outlook 2007
Спасибо
win32com.client.Dispatch
не работает, потому что вы создали кеш вTemp\gen_py
папке - person Winand   schedule 19.04.2016