简体   繁体   English

如何使用 Python 从特定文件夹和时间保存 MS Outlook 附件

[英]How to save MS Outlook attachments from specific folder and hour using Python

I would like to have a Python script to save all the attachements (with their name_datestampfrom a specific folder and filtering for specific timestamp (23:00).我想要一个 Python 脚本来保存所有附件(带有来自特定文件夹的 name_datesamp 并过滤特定时间戳(23:00)。

Could you please help me?请你帮助我好吗?

I'm using the following code (found in another thread) but it allowes me only to save the attahchement of the most recent mail contained in the Inbox folder with a standard name:我正在使用以下代码(在另一个线程中找到),但它只允许我使用标准名称保存收件箱文件夹中包含的最新邮件的附件:

import win32com.client
import os.path


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

Filtermail = "[SenderEmailAddress] = 'xxx@yyy.com'"

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


for attachment in Items.Attachments:
    print(attachment.FileName)
    attachment.SaveAsFile(os.getcwd() + '\\Mail\\' + 'zzzz.xlsx')

First, you need to modify the search condition to get items for a specific time frame.首先,您需要修改搜索条件以获取特定时间范围内的项目。

DateToCheck = "[RecievedTime] >= """ & DateStart & """"  

Second, you need to iterate over all items found in the loop, not just get the first and process attachments (VBA syntax):其次,您需要遍历循环中找到的所有项目,而不仅仅是获取第一个并处理附件(VBA 语法):

Set myRestrictItems = myContacts.Restrict(DateToCheck)  
For Each myItem In myRestrictItems  
    If (myItem.Class = olMail) Then  
       MsgBox myItem.Subject & ": " & myItem.RecievedTime
    End If  
Next  

The MailItem.ReceivedTime property returns a Date indicating the date and time at which the item was received. MailItem.ReceivedTime属性返回一个Date ,指示接收项目的日期和时间。

Third, here is the search query for items with attachments (VBA syntax):第三,这里是带有附件的项目的搜索查询(VBA 语法):

query ="@SQL=" & chr(34) & "urn:schemas:httpmail:hasattachment" & chr(34) & "=True"

or或者

query ="@SQL=" & chr(34) & "urn:schemas:httpmail:hasattachment" & chr(34) & "=1"

The samples are in VBA because I am not familiar with Python syntax, but the Outlook object model is common for all kind of programming languages. The samples are in VBA because I am not familiar with Python syntax, but the Outlook object model is common for all kind of programming languages. So, hopefully that shouldn't be a problem.所以,希望这不应该是一个问题。

You can read more about Find / FindNext or Restrict methods in the following articles:您可以在以下文章中阅读有关Find / FindNextRestrict方法的更多信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM