如何使用 python 保存 Outlook 中的电子邮件

发布于 2025-01-10 18:18:14 字数 790 浏览 0 评论 0原文

我正在尝试使用下面的 python 脚本保存子文件夹中的电子邮件,我正在尝试使用 days=1 进行限制,这意味着我只需要保存 1 天前的电子邮件。

from win32com.client import Dispatch
from datetime import date, timedelta
import datetime as dt


msg_location = r'C:\Users\rahul\Desktop\msg_files'



outlook = Dispatch("outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders['Email_snapper']

messages = inbox.Items
message = messages.GetFirst()
Body_content = message.Body
print(Body_content)


for msg in messages:
    lastWeekDateTime = dt.datetime.now() - dt.timedelta(days=1)
    lastWeekDateTime = lastWeekDateTime.strftime('%m/%d/%Y %H:%M %p')
    message = messages.Ryestrict("[ReceivedTime] >= '" + lastWeekDateTime + "'")
    #name = str(message)
message.SaveAs(msg+".msg")

I am trying to save email from sub-folder using the below python script, I am trying to restrict with days=1 means I only need to save emails which are 1 day old.

from win32com.client import Dispatch
from datetime import date, timedelta
import datetime as dt


msg_location = r'C:\Users\rahul\Desktop\msg_files'



outlook = Dispatch("outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6).Folders['Email_snapper']

messages = inbox.Items
message = messages.GetFirst()
Body_content = message.Body
print(Body_content)


for msg in messages:
    lastWeekDateTime = dt.datetime.now() - dt.timedelta(days=1)
    lastWeekDateTime = lastWeekDateTime.strftime('%m/%d/%Y %H:%M %p')
    message = messages.Ryestrict("[ReceivedTime] >= '" + lastWeekDateTime + "'")
    #name = str(message)
message.SaveAs(msg+".msg")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尝试像下面这样设置您的过滤器

示例

import re

import win32com.client
import datetime as dt
import os

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

lastWeekDateTime = dt.datetime.now() - dt.timedelta(days=1)
lastWeekDateTime = lastWeekDateTime.strftime('%m/%d/%Y %H:%M %p')
print(lastWeekDateTime)

Filter = ("@SQL=" + chr(34) + "urn:schemas:httpmail:datereceived" +
          chr(34) + " >= '" + lastWeekDateTime + "'")

Items = Inbox.Items.Restrict(Filter)
Items.Sort('[ReceivedTime]', False)

for Item in Items:
    print(Item.Subject)
    print(Item.ReceivedTime)
    save_name = re.sub('[^A-Za-z0-9]+', '', str(Item.Subject)) + '.msg'
    Item.SaveAs(os.getcwd() + '//' + save_name)
else:
    print("No Item")

Try setting your filter like the following

Example

import re

import win32com.client
import datetime as dt
import os

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

lastWeekDateTime = dt.datetime.now() - dt.timedelta(days=1)
lastWeekDateTime = lastWeekDateTime.strftime('%m/%d/%Y %H:%M %p')
print(lastWeekDateTime)

Filter = ("@SQL=" + chr(34) + "urn:schemas:httpmail:datereceived" +
          chr(34) + " >= '" + lastWeekDateTime + "'")

Items = Inbox.Items.Restrict(Filter)
Items.Sort('[ReceivedTime]', False)

for Item in Items:
    print(Item.Subject)
    print(Item.ReceivedTime)
    save_name = re.sub('[^A-Za-z0-9]+', '', str(Item.Subject)) + '.msg'
    Item.SaveAs(os.getcwd() + '//' + save_name)
else:
    print("No Item")
指尖上的星空 2025-01-17 18:18:14

首先,它是Restrict,而不是Ryestrict

其次,Restrict 返回Items 集合,而不是单个项目。您需要迭代该集合中的项目。如果您只需要单个项目,请使用Find 而不是Restrict

Firstly, it is Restrict, not Ryestrict.

Secondly, Restrict returns Items collection, not a single item. You need to iterate over the items in that collection. If you only expect a single item, use Find instead of Restrict.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文