提取电子邮件附件python的文件名

时间:2019-07-16 09:08:48

标签: python

我希望能够使用附件文件名作为过滤器从电子邮件中下载电子邮件附件。我已经附上了我现在正在处理的代码,但看起来我的代码有问题。

我怀疑问题出在以下一行:如果m.filename中的'Star': 但是,我无法解决它。

from pandas.tseries.offsets import BDay

import pandas as pd

import win32com.client as win32

import datetime

import imaplib, email

import os.path

from datetime import date

import datetime as date

from win32com.client import Dispatch



outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder("6")

messages = inbox.Items

message = messages.GetFirst()

subject = message.Subject



for m in messages:

    if 'Star' in m.filename:

        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(os.path.join(r"X:\test\Schr",attachment.FileName))

        message = messages.GetNext()



    else:

        message = messages.GetNext()

1 个答案:

答案 0 :(得分:0)

您需要迭代每个附件,然后检查文件名

例如:

from win32com.client import Dispatch
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
messages = inbox.Items

for m in messages:
    if m.Attachments:
        for f in m.Attachments:
            if 'Star' in f.FileName:
                #Code....