python script to windows exe

sandeep shiningsandy at gmail.com
Mon May 19 05:59:22 EDT 2008


hi
the py code that i have written is here.when i run this code i wont
get any errors it just works fine for me.but when i created the exe i
start getting an error in my 'getMailContent' function. The error
description is

TypeError:unsupported operand type(s) for :- 'instance' and 'int'

i dont know why i am start getting this error when i run it through
the exe.


import win32com,win32com.client
import os,os.path
import codecs
import zipfile

#@Author:::Sandeep Kumar Sharma

#outlook application refrence
outlook_app=0
#outlook ids to access different folders look into msdn for more info.
not a preffered way as i am hardcoding data here
OlDefaultFolders={'olFolderCalendar':9,'olFolderConflicts':
19,'olFolderContacts':10,'olFolderDeletedItems':3,'olFolderDrafts':
16,'olFolderInbox':6,'olFolderJournal':11,'olFolderJunk':
23,'olFolderLocalFailures':21,'olFolderNotes':12,'olFolderOutbox':
4,'olFolderSentMail':5,'olFolderServerFailures':
22,'olFolderSyncIssues':20,'olFolderTasks':
13,'olPublicFoldersAllPublicFolders':18}
#outlook types to save mailItem look into msdn for more info
#although doesnot work for me :-(
OlSaveAsType={'olTXT': 0,'olRTF':1,'olTemplate': 2,'olMSG': 3,'olDoc':
4,'olHTML':5,'olVCard': 6,'olVCal':7,'olICal': 8};

#refrence to content in inbox
inbox_obj=0

#function which will initialise outlook and return its reference
def getAppRef():
    temp=win32com.client.Dispatch("OutLook.Application")
    return temp.GetNamespace("MAPI")

#function to return the folders in the outlook
def getOutLookFolders(a,b=OlDefaultFolders['olFolderInbox']):
    return a.GetDefaultFolder(b)

#function to get email content
def getMailContent(obj):
    txt_file=codecs.open('data.html',encoding='utf-8',mode='w')
    for kk in range(len(obj.Items),1,-1):
    #for kk in range(len(obj.Items-1),0,-1):
        #print 'hello'
        print 'writting file='+str(kk)
        mailItem=obj.Items[kk]
        writeData(mailItem,txt_file)

        #print mailItem.OlSaveAsType.olMSG
        #saveCopy(mailItem)
        #print "sender="+mailItem.SenderName+'
SenderEmailId='+str(mailItem.SenderEmailAddress)+'
Time='+str(mailItem.ReceivedTime)
        #print 'Subject='+mailItem.Subject+' size='+str(mailItem.Size)

    txt_file.close()
    '''
    file_zip=zipfile.ZipFile(txt_file,"w",zipfile.ZIP_DEFLATED)
    file_zip.write('data.log')
    file_zip.close()
    '''

#function to create a directory
#obviously not a best way :-( but i think can expected this sort of
mistakes from beginners
def createDir():
    path=os.path.abspath("\email")
    if(os.path.exists(path)):
        print "Directory already exists"
    else:
        os.system("md "+path)

#function to save a copy of email
def writeData(mailItem,file):
    data="<p>"
    sender='<h4>SenderName</
h4>'+checkStringType(mailItem.SenderName)
    time='<br><h4>Time</
h4>'+checkStringType(str(mailItem.ReceivedTime))
    attachment='<br><h5>Attachments Count</
h5>'+str(len(mailItem.Attachments))
    edata='<h4>Email Content</h4>'+checkStringType(mailItem.Body)+"</
p><hr/>"
    dataToWrite=data+sender+time+attachment+edata
    getAttachmentInfo(mailItem.Attachments)
    file.write(getHTMLString(dataToWrite))
    #checkStringType(dataToWrite)

def getAttachmentInfo(atmts):
    for kk in range(1,len(atmts)):
        atmt=atmts[kk]
        #print "File Name="+atmt.FileName+'
DisplayName='+atmt.DisplayName+' PathName='+atmt.PathName+' '
        abc=os.path.isdir(os.getcwd()+'\email')

        if(abc==True):
            print 'directory exists'

        else:
            os.mkdir(os.getcwd()+'\email')

        path=os.path.abspath(os.getcwd()+'\email')
        atmt.SaveAsFile(path+"\\"+atmt.DisplayName)



# function to check whether the character encoding is ascii or smthing
else
def checkStringType(a):

    if isinstance(a,str):
       b='not a unicode string'

    else:
        a.encode('utf-8')
        #print 'unicode type'

    return a

#function to save the coopy of an email
#:-( but smhow it generate error whenever i make a call to it
def saveCopy(mailItem):

    name="\\"+mailItem.Subject+"__"+str(mailItem.ReceivedTime)
    print name
    #global outlook_app
    try:
        mailItem.SaveAs(path+name+".txt",OlSaveAsType['olTXT'])
    except BaseException:
        print BaseException

def getHTMLString(b):
    a='<html><head><title>Your Email Data log is here</title></
head><body>'+b+'</body></html>'
    return a

#main entrance to the program
def main():
    global outlook_app,inbox_obj
    outlook_app=getAppRef()
    #print outlook_app.OlSaveAsType.olMSG
    print '=================='
    print dir(outlook_app)
    print '=================='
    inbox_obj=getOutLookFolders(outlook_app)
    print dir(inbox_obj)
    print (inbox_obj.Items)
    #saveCopy(inbox_obj.Items[1])
    getMailContent(inbox_obj)



main()


my setup file has this code

from distutils.core import setup
import py2exe

setup(console=['outlook.py'])


i have just copied and pasted it from the tutorial available at the
py2exe site and changed the filename with mine filename.


thanks and regards
sandeep kumar sharma

> There is nothing special in executables produced by py2exe. I mean
> that the debugging strategy is as always. A good start might be in
> adding logging\tracing facilities both to script and the executable.
> Comparing two trace files could give you some clue.



More information about the Python-list mailing list