2017-02-22 3 views
0

指定された名前でファイル形式(.txt、.csv、.pdf、.docx、.xlsx、.xlsxなど)のファイルをダウンロードするPythonスクリプトが必要です。 MSG、など)現在 、私は見通し2013年から添付ファイルをダウンロードするには、次のPythonコードを持っている: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 =被写体を取り付ける 49 ---> all_inboxでcurrent_message 50: 51 checkTime(current_message)== "パス" とcurrent_message.Subject == sub_today場合: 52 getAttachment(current_message、件名、名前) はTypeError: 'NoneType' オブジェクトが –

+0

Itをiterable'されていませんwill downlo添付ファイル名の広告。 –

関連する問題