本文介绍了如何使用Python从特定发件人和日期保存MS Outlook附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编码有点陌生,我试图了解如何获取Python来保存特定发件人的MS Outlook附件.目前,我每天都会收到来自同一个人的同一封电子邮件,其中涉及我需要保存到特定文件夹的数据.以下是我要满足的要求:

I am a bit new to coding and I am trying to understand how to get Python to save MS Outlook attachments from a specific sender. I currently receive the same email from the same person each day regarding data that I need to save to a specific folder. Below are the requirements I am trying to meet:

  1. 我要打开MS Outlook并搜索特定的发件人
  2. 我要确保从特定发件人处打开的电子邮件是最新日期
  3. 我想将此发件人的所有附件保存到桌面上的特定文件夹中

我已经看到了一些有关使用win32com.client的文章,但运气不佳,无法与MS Outlook一起使用.我将在下面附加一些我尝试过的代码.感谢您的反馈!

I have seen some posts on using win32com.client but have not had much luck getting it to work with MS Outlook. I will attach some code I have tried below. I appreciate any feedback!

import win32com.client
outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6)
messages=inbox.Items
for message in messages:
    attachments = message.attachments
    for attachment in attachments:
        pass

推荐答案

您几乎明白了,将过滤器添加到发件人电子邮件地址

import win32com.client

Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)

Filter = "[SenderEmailAddress] = '[email protected]'"

Items = Inbox.Items.Restrict(Filter)
Item = Items.GetFirst()

for attachment in Item.Attachments:
    print(attachment.FileName)
    attachment.SaveAsFile(r"C:\path\to\my\folder\Attachment.xlsx")

Windows上的Python 3.8

这篇关于如何使用Python从特定发件人和日期保存MS Outlook附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-10 02:34