[python-win32] Windows Address Book Example ...

Clinton Lee Taylor clintonlee.taylor at gmail.com
Sun Mar 16 12:49:18 CET 2008


Greetings ...

 Just for completeness, I thought I would  post my final test for
dumping just people in the Windows Address Book (WAB) with a Fax
number ...

 Please understand that I'm just a beginner with python, so, no laughing ... ;-)

 I'm sure that there could be quite a bit more error checking, like
the Outlook dumps have, but I don't quite understand yet traps and
things like that ... Almost slapped myself because I could not find a
switch statement for multiple if and then there was things about
create class ... swish, straight over the top of my head ... ;-)

 Okay, I'm using the Open Source, but LGPL project called WABAccess
... http://wabaccess.sourceforge.net/

 I have three files to create an exe file, still working on making it
a quite test to see what else I'm missing, so a single click exe with
all the DLL's and stuff inside, but that will come much later ...

 First file ... wab-01.py
import win32com.client

if win32com.client.gencache.is_readonly == True:

    #allow gencache to create the cached wrapper objects
    win32com.client.gencache.is_readonly = False

    # under p2exe the call in gencache to __init__() does not happen
    # so we use Rebuild() to force the creation of the gen_py folder
    win32com.client.gencache.Rebuild()

    # NB You must ensure that the python...\win32com.client.gen_py dir
does not exist
    # to allow creation of the cache in %temp%

from win32com.client.gencache import EnsureDispatch
from win32com.client import constants

objSession = EnsureDispatch("WABAccess.Session", bForDemand=0)

#print objSession

#Open the WAB with an identity. If no identity is already open,
#the Identity Manager is launch to prompt the user to choose an indentity.
#objSession.Open(True)
#
#The WAB is open without an identity.
objSession.Open(False)

if objSession.Identities.LastIdentity ==
"{00000000-0000-0000-0000-000000000000}":
    print "No Identity Selected"
else:
    print "The Identity " +
objSession.Identities(objSession.Identities.LastIdentity).Name + " is
selected"

#print objSession.Identities.DefaultIdentity
#print objSession.Identities.Count
#print objSession.Containers.Count

for oContU in objSession.Containers:
    for oItem in oContU.Elements:
        for oContP in oItem.Properties:
             if oContP.Id == constants.wabPR_PRIMARY_FAX_NUMBER:
                print oItem.Name
                print "Primary Fax " + oContP.Value
             elif oContP.Id == constants.wabPR_BUSINESS_FAX_NUMBER:
                print oItem.Name
                print "Business Fax " + oContP.Value
             elif oContP.Id == constants.wabPR_HOME_FAX_NUMBER:
                print oItem.Name
                print "Home Fax " + oContP.Value

objSession.Refresh
objSession.Close
print "Done."

Second file ... setup.py
from distutils.core import setup

import py2exe



setup(console=['wab-01.py'])


and last file ... maketest.bat
@echo off

Rem As per following webpage ...

Rem http://www.py2exe.org/index.cgi/UsingEnsureDispatch

Rem

Rem Rename gen_py to something else before running the setup

Rem

echo Rename gen_py to org

ren C:\Python24\Lib\site-packages\win32com\gen_py
C:\Python24\Lib\site-packages\win32com\gen_py.org



C:\python24\python wab-01.py

pause

C:\python24\python setup.py install

pause

C:\python24\python setup.py py2exe

pause

Rem rename gen_py.org back gen_py

echo Rename gen_py.org to back

ren C:\Python24\Lib\site-packages\win32com\gen_py.org
C:\Python24\Lib\site-packages\win32com\gen_py




 I hope this help's anybody else that might be looking for similar
code to start with ...

Thanks
Mailed
LeeT


More information about the python-win32 mailing list