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:unsup ported 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,win32c om.client
import os,os.path
import codecs
import zipfile
#@Author:::Sand eep 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
OlDefaultFolder s={'olFolderCal endar':9,'olFol derConflicts':
19,'olFolderCon tacts':10,'olFo lderDeletedItem s':3,'olFolderD rafts':
16,'olFolderInb ox':6,'olFolder Journal':11,'ol FolderJunk':
23,'olFolderLoc alFailures':21, 'olFolderNotes' :12,'olFolderOu tbox':
4,'olFolderSent Mail':5,'olFold erServerFailure s':
22,'olFolderSyn cIssues':20,'ol FolderTasks':
13,'olPublicFol dersAllPublicFo lders':18}
#outlook types to save mailItem look into msdn for more info
#although doesnot work for me :-(
OlSaveAsType={' olTXT': 0,'olRTF':1,'ol Template': 2,'olMSG': 3,'olDoc':
4,'olHTML':5,'o lVCard': 6,'olVCal':7,'o lICal': 8};
#refrence to content in inbox
inbox_obj=0
#function which will initialise outlook and return its reference
def getAppRef():
temp=win32com.c lient.Dispatch( "OutLook.Applic ation")
return temp.GetNamespa ce("MAPI")
#function to return the folders in the outlook
def getOutLookFolde rs(a,b=OlDefaul tFolders['olFolderInbox']):
return a.GetDefaultFol der(b)
#function to get email content
def getMailContent( obj):
txt_file=codecs .open('data.htm l',encoding='ut f-8',mode='w')
for kk in range(len(obj.I tems),1,-1):
#for kk in range(len(obj.I tems-1),0,-1):
#print 'hello'
print 'writting file='+str(kk)
mailItem=obj.It ems[kk]
writeData(mailI tem,txt_file)
#print mailItem.OlSave AsType.olMSG
#saveCopy(mailI tem)
#print "sender="+mailI tem.SenderName+ '
SenderEmailId=' +str(mailItem.S enderEmailAddre ss)+'
Time='+str(mail Item.ReceivedTi me)
#print 'Subject='+mail Item.Subject+' size='+str(mail Item.Size)
txt_file.close( )
'''
file_zip=zipfil e.ZipFile(txt_f ile,"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.ab spath("\email")
if(os.path.exis ts(path)):
print "Directory already exists"
else:
os.system("md "+path)
#function to save a copy of email
def writeData(mailI tem,file):
data="<p>"
sender='<h4>Sen derName</
h4>'+checkStrin gType(mailItem. SenderName)
time='<br><h4>T ime</
h4>'+checkStrin gType(str(mailI tem.ReceivedTim e))
attachment='<br ><h5>Attachment s Count</
h5>'+str(len(ma ilItem.Attachme nts))
edata='<h4>Emai l Content</h4>'+checkStrin gType(mailItem. Body)+"</
p><hr/>"
dataToWrite=dat a+sender+time+a ttachment+edata
getAttachmentIn fo(mailItem.Att achments)
file.write(getH TMLString(dataT oWrite))
#checkStringTyp e(dataToWrite)
def getAttachmentIn fo(atmts):
for kk in range(1,len(atm ts)):
atmt=atmts[kk]
#print "File Name="+atmt.Fil eName+'
DisplayName='+a tmt.DisplayName +' PathName='+atmt .PathName+' '
abc=os.path.isd ir(os.getcwd()+ '\email')
if(abc==True):
print 'directory exists'
else:
os.mkdir(os.get cwd()+'\email')
path=os.path.ab spath(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,st r):
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(mailIt em):
name="\\"+mailI tem.Subject+"__ "+str(mailItem. ReceivedTime)
print name
#global outlook_app
try:
mailItem.SaveAs (path+name+".tx t",OlSaveAsT ype['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,inb ox_obj
outlook_app=get AppRef()
#print outlook_app.OlS aveAsType.olMSG
print '============== ===='
print dir(outlook_app )
print '============== ===='
inbox_obj=getOu tLookFolders(ou tlook_app)
print dir(inbox_obj)
print (inbox_obj.Item s)
#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.