2017-02-22 63 views
0

我想要一個python腳本來下載任何具有指定名稱但具有任何文件格式的文件(它可以是.txt,.csv,.pdf,.docx,.xlsx ,.味精等) 目前,我有以下Python代碼從Outlook 2013下載附件:使用Python腳本從Outlook 2013下載附件

import win32com.client 
from win32com.client import Dispatch 
import datetime as date 
import os.path 

def attach(subject,name): 
    outlook = Dispatch("Outlook.Application").GetNamespace("MAPI") 
    inbox = outlook.GetDefaultFolder("6") 
    all_inbox = inbox.Items 
    val_date = date.date.today() 
    sub_today = subject 
    att_today = name 
    for msg in all_inbox: 
     if msg.Subject == sub_today: 
      break 
    for att in msg.Attachments: 
     if att.FileName == att_today: 
      break 
    att.SaveASFile(os.getcwd() + '\\' + att.FileName) 
    print "Mail Successfully Extracted" 

如果我讓特定的某些類型的附件,它工作正常。

attach('Hi','cr.txt') 

,但我想做的事情是這樣的:

attach('Hi','cr.*') 

所以它可以下載附件名爲「CR」但是任何文件格式。

任何人都可以提出一個解決方法,這將有所幫助。

+0

此外,這隻適用於郵件代碼檢查今天收到'val_date = date.date.today()'我要檢查在過去的7天收到的郵件附件。我應該怎麼做?我搜索了** win32com **文檔,但是我無法在任何地方找到它。 –

回答

1

希望這有助於:)

import win32com.client, datetime 
from win32com.client import Dispatch 
import datetime as date 
import os.path 

def checkTime(current_message): 
    date_filter_unformated = datetime.date.today() - date.timedelta(days=7) 
    date_filter = date_filter_unformated.strftime("%m/%d/%y %I:%M:%S") 
    message_time = current_message.ReceivedTime 
    df_list = list(date_filter) 
    mt_list = list(str(message_time)) 
    df_month, mt_month = int(''.join([df_list[0],df_list[1]])), int(''.join([mt_list[0],mt_list[1]])) 
    df_day, mt_day = int(''.join([df_list[3],df_list[4]])), int(''.join([mt_list[3],mt_list[4]])) 
    df_year, mt_year = int(''.join([df_list[6],df_list[7]])), int(''.join([mt_list[6],mt_list[7]])) 
    if mt_year < df_year: 
     return "Old" 
    elif mt_year == df_year: 
     if mt_month < df_month: 
      return "Old" 
     elif mt_month == df_month: 
      if mt_day < df_day: 
       return "Old" 
      else: 
       CurrentMessage(current_message) 
       return "Pass" 
     elif mt_month > df_month: 
      CurrentMessage(current_message) 
      return "Pass" 

def CurrentMessage(cm): 
    print cm.Sender, cm.ReceivedTime 


def getAttachment(msg,subject,name): 
    val_date = date.date.today() 
    sub_today = subject 
    att_today = name#if you want to download 'test.*' then att_today='test' 
    for att in msg.Attachments: 
     if att.FileName.split('.')[0] == att_today: 
      att.SaveASFile(os.getcwd() + '\\' + att.FileName) 


def attach(subject,name): 
    outlook = Dispatch("Outlook.Application").GetNamespace("MAPI") 
    inbox = outlook.GetDefaultFolder("6") 
    all_inbox = inbox.Items 
    all_inbox = all_inbox.Sort("[ReceivedTime]", True) 
    sub_today=subject 

    for current_message in all_inbox: 
     if checkTime(current_message) == "Pass" and current_message.Subject == sub_today: 
      getAttachment(current_message,subject,name)  
    print "Mail Successfully Extracted" 
+0

是否會根據附件名稱下載附件,而不是基於文件擴展名? –

+0

我跑了,它提出了錯誤:'--------------------------------------- ------------------------------------ TypeError in () ----> 1連接( '修訂清單', 'metrices_V7') 在附着(主題,名稱) 48 sub_today =受試者 ---> 50,用於在all_inbox current_message: 51如果檢查時間(current_message)== 「通」 和current_message.Subject == sub_today: 52 getAttachment(current_message,主題,名稱) 類型錯誤: 'NoneType' 對象未iterable' –

+0

它將下載廣告附件名稱。 –

相關問題