Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.3 kB
0
Indexable
Never
import pandas as pd
import win32com.client as win32
import win32com
from openpyxl import load_workbook
from datetime import date, timedelta, datetime
import sys
import time
import os


outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts


def GetAttachment():
    global var, m
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    accounts= win32com.client.Dispatch("Outlook.Application").Session.Accounts
    for account in accounts:
        global inbox
        inbox = outlook.Folders(account.DeliveryStore.DisplayName)
        if str(account.DisplayName) == 'praveenkriitkgp@outlook.com':
            print ('found account')
            folders = inbox.Folders
            for folder in folders:
                if str(folder) == 'Inbox':
                    messages = folder.Items
                    print ('found inbox')
                    received_dt = datetime.today() - timedelta(days=1)
                    received_dt = received_dt.strftime('%m/%d/%Y %H:%M %p')
                    print( len( messages))
                    messages = messages.Restrict("[ReceivedTime] >= '" + received_dt + "'")
#                    messages = messages.Restrict("[SenderEmailAddress] = 'VPNA.SYSDUTY@VOLVO.COM'")
#                    messages = messages.Restrict("[SenderEmailAddress] = 'shan.peng@volvo.com'")
                    messages = messages.Restrict('[Subject] = New "Instant Query Helpdesk " Feature on DKM Portal')
                    print( len( messages))
                    for m in messages:

                    #    var = False
                    #    if not var:
                    #        print ('No OCC41 received today')    
                        print (m.Subject)
                        if  str(m.Attachments.Item(1))  == 'OCC41_RPT.CSV':
                            print ('found email')
                            print (str(m.Attachments.Item(1)))                    
                            attc = m.Attachments.Item(1)
                            attc.SaveAsFile(SRootPath+ str(attc))
                            print ('Downloaded email')

        

if __name__ == '__main__':
    GetAttachment()