python script to windows exe

sandeep shiningsandy at gmail.com
Tue May 20 03:04:13 EDT 2008


hi all

thanks for ur replies. i have a bit closer look at my code and i am
able to fix the problem.now my exe is working fine.the code is bit
more cleaner now as i removed lot of unused function from it and try
to document it also.


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')
    emailData=""
    for kk in range(len(obj.Items)-1,0,-1):
        print 'writting file='+str(kk)
        mailItem=obj.Items[kk]
        emailData=emailData+getEmailData(mailItem)
        saveAttachments(mailItem.Attachments)
    txt_file.write(getHTMLString(emailData))
    txt_file.close()

#function which will return the emailItem data as form of String
def getEmailData(mailItem):
    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
    return dataToWrite

#function for saving the attachment.we are calling attachments
SaveAsFile to save the attachment.
#SaveAsFile is com method.for more info dig into msdn :-)
def saveAttachments(atmts):
    for kk in range(1,len(atmts)):
        atmt=atmts[kk]
        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

#bit of html stuff so that i can see my output on browsers.
def getHTMLString(emailData):
    a='<html><head><title>Your Email Data log is here</title></
head><body>'+emailData+'</body></html>'
    return a

#main entrance to the program
def main():
    global outlook_app,inbox_obj
    outlook_app=getAppRef()
    inbox_obj=getOutLookFolders(outlook_app)
    getMailContent(inbox_obj)

main()

once again thanks for your help.

thanks and regards
sandeep kumar sharma



More information about the Python-list mailing list