簡體   English   中英

從共享文件夾下載 Email 附件 - Python

[英]Downloading Email Attachments from Shared Folder - Python

我有以下代碼根據發送日期和 email 主題標准下載 email 附件:

from datetime import date, timedelta
import os
import win32com.client


path = os.path.expanduser("C:\\Users\\xxxx\\Documents\\Projects\\VBA Projects\\VLOOKUP Automation\\Vlookup File Location")
today = date.today()

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders("xxx").Folders.Item("Inbox")
messages = inbox.Items
subject = "xxx"

dateHigh = date.today() - timedelta(days=1)
dateLow = date.today() - timedelta(days=-1)

max = 2500
for count, message in enumerate(messages):
    if count > max:
        break
    if subject in message.subject and message.senton.date() > dateLow and message.senton.date() < dateHigh:
            attachments = message.Attachments
            num_attach = len([x for x in attachments])
            for x in range(1, num_attach+1):
                attachment = attachments.Item(x)
                attachment.SaveASFile(path + '\\' + str(attachment))

有什么方法可以指定僅用於下載的.csv 附件的標准嗎?

此外,此代碼以前用於公共文件夾 - 這些文件夾現在已更新為共享文件夾。 自更新以來,我不得不將“最大值”從 500 增加到 2500 才能找到指定的電子郵件。 有什么辦法可以加快這個速度嗎?

謝謝

下面是一種指定所需文件類型的方法。

請在 attachments_of_interest 列表中輸入文件結尾。

from datetime import date, timedelta
import os
import win32com.client


path = os.path.expanduser("C:\\Users\\xxxx\\Documents\\Projects\\VBA Projects\\VLOOKUP Automation\\Vlookup File Location")
today = date.today()

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders("xxx").Folders.Item("Inbox")
messages = inbox.Items
subject = "xxx"

dateHigh = date.today() - timedelta(days=1)
dateLow = date.today() - timedelta(days=-1)

max_n = 2500
attachments_of_interest = ['.csv']

for count, message in enumerate(messages):
    if count > max_n:
        break
    if subject in message.subject and message.senton.date() > dateLow and message.senton.date() < dateHigh:
        attachments = message.Attachments
        num_attach = len([x for x in attachments])
        for x in range(1, num_attach+1):
            attachment = attachments.Item(x)
            attachment_fname = str(attachment)
            file_ending = attachment_fname.split('.')[-1]
            if not attachments_of_interest or file_ending in attachments_of_interest:
                attachment.SaveASFile(path + '\\' + attachment_fname)

至於加速,你可以使用一個池:

from multiprocessing.pool import ThreadPool as Pool
from datetime import date, timedelta
import os
import win32com.client


path = os.path.expanduser("C:\\Users\\xxxx\\Documents\\Projects\\VBA Projects\\VLOOKUP Automation\\Vlookup File Location")
today = date.today()

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders("xxx").Folders.Item("Inbox")
messages = inbox.Items
subject = "xxx"

max_n = 2500
attachments_of_interest = ['.csv']
pool_size = 5

# define worker function before a Pool is instantiated
def worker(message):
    dateHigh = date.today() - timedelta(days=1)
    dateLow = date.today() - timedelta(days=-1)
    if subject in message.subject and message.senton.date() > dateLow and message.senton.date() < dateHigh:
        attachments = message.Attachments
        num_attach = len([x for x in attachments])
        for x in range(1, num_attach+1):
            attachment = attachments.Item(x)
            attachment_fname = str(attachment)
            file_ending = attachment_fname.split('.')[-1]
            if not attachments_of_interest or file_ending in attachments_of_interest:
                attachment.SaveASFile(path + '\\' + attachment_fname)

pool = Pool(pool_size)

for count, message in enumerate(messages):
    if count > max_n:
        break
    pool.apply_async(worker, (message,))

pool.close()
pool.join()

我認為這是僅下載 csv 要求的一部分。 這個 outlook 組件有一些您可以使用的方法。 而不是 messages = inbox.Items 嘗試 messages = inbox.Items.GetFirst() 並獲取第一條消息然后使用

messages = inbox.Items.oItems.GetNext() 所以這樣你在 memory 中總是有一條消息,你可以繼續循環更長的時間。

確保您擁有 outlook Microsoft Outlook 16.0 Object 庫或高於 10 的庫,以便存在此方法。 GetFirst() 我使用的 c# 代碼

Outlook.MailItem oMsg = (Outlook.MailItem)oItems.GetFirst();

                    //Output some common properties.
                    Console.WriteLine(oMsg.Subject);
                    Console.WriteLine(oMsg.SenderName);
                    Console.WriteLine(oMsg.ReceivedTime);
                    Console.WriteLine(oMsg.Body);

                    //Check for attachments.
                    int AttachCnt = oMsg.Attachments.Count;
                    Console.WriteLine("Attachments: " + AttachCnt.ToString());
                Outlook.MailItem oMsg1 = (Outlook.MailItem)oItems.GetNext();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM