gpt4 book ai didi

python - 从共享文件夹下载电子邮件附件 - Python

转载 作者:行者123 更新时间:2023-12-02 03:05:12 24 4
gpt4 key购买 nike

我有以下代码来根据发送日期和电子邮件主题标准下载电子邮件附件:

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 附件的标准吗?

此外,此代码之前用于公共(public)文件夹 - 这些文件夹现已更新为共享文件夹。自更新以来,我不得不将“最大”从 500 增加到 2500 才能找到指定的电子邮件。有什么办法可以加快速度吗?

谢谢

最佳答案

下面是指定您想要的文件类型的方法。

请在感兴趣的附件列表中输入文件结尾。

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()

关于python - 从共享文件夹下载电子邮件附件 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58874973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com