Я провел небольшое исследование на эту тему.Я увидел эту поваренную книгу здесь и попробовал.Он работал нормально, но после некоторого обновления / восстановления моего Outlook, он больше не работает.Сообщение об ошибке: AttributeError: olFolderContacts
.
Я также попробовал эти предложения здесь , но не сработало.
Сообщение об ошибке com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The attempted operation failed. An object could not be found.', None, 0, -2147221233), None)
.
Может ли кто-нибудь помочь определить проблему?
код здесь:
import win32com.client
import sys
DEBUG = 0
class MSOutlook:
def __init__(self):
self.outlookFound = 0
try:
self.oOutlookApp = \
win32com.client.Dispatch("Outlook.Application")
self.outlookFound = 1
except:
print ("MSOutlook: unable to load Outlook")
self.records = []
def loadContacts(self, keys=None):
if not self.outlookFound:
return
# this should use more try/except blocks or nested blocks
onMAPI = self.oOutlookApp.GetNamespace("MAPI")
ofContacts = \
onMAPI.GetDefaultFolder(win32com.client.constants.olContact)
if DEBUG:
print ("number of contacts:", len(ofContacts.Items))
for oc in range(len(ofContacts.Items)):
contact = ofContacts.Items.Item(oc + 1)
if contact.Class == win32com.client.constants.olContact:
if keys is None:
# if we were't give a set of keys to use
# then build up a list of keys that we will be
# able to process
# I didn't include fields of type time, though
# those could probably be interpreted
keys = []
for key in contact._prop_map_get_:
if isinstance(getattr(contact, key), (int, str, 'unicode')):
keys.append(key)
if DEBUG:
keys.sort()
print ("Fields\n======================================")
for key in keys:
print (key)
record = {}
for key in keys:
record[key] = getattr(contact, key)
if DEBUG:
print (oc, record['FullName'])
self.records.append(record)
def output(self):
dic={}
for i in self.records:
dic[i['FullName']] = [i['Email1Address']]
return dic
if __name__ == '__main__':
if DEBUG:
print ("attempting to load Outlook")
oOutlook = MSOutlook()
# delayed check for Outlook on win32 box
if not oOutlook.outlookFound:
print( "Outlook not found")
sys.exit(1)
fields = ['FullName',
'Email1Address',
]
if DEBUG:
import time
print ("loading records...")
startTime = time.time()
# you can either get all of the data fields
# or just a specific set of fields which is much faster
#oOutlook.loadContacts()
oOutlook.loadContacts(fields)
if DEBUG:
print ("loading took %f seconds" % (time.time() - startTime))
for i in oOutlook.records:
print ("Contact: %s" % i['FullName'])
print ("Contact: %s" % i['Email1Address'])