重复代码执行或替代方案:Python,Outlook

发布于 2025-02-05 07:58:10 字数 511 浏览 3 评论 0 原文

当试图将电子邮件从收件箱移动到另一个文件夹时,我对执行质量的执行质量有问题。

inbox = inbox.Items
example_folder = inbox.Restrict("[SenderEmailAddress] = '[email protected]'")
for message in example_folder:
    message.Move(folder)

问题不是每个电子邮件在命令时都会移动,而是对sendername,主题和其他人也会发生同样的情况,因此这不是我要提取的电子邮件。它确实有效,但是我需要继续执行它才能捕获错过的那些。

代码有什么建议吗?如果没有,我是否有办法添加新代码,该代码只能自动重复该命令,直到我要停止? (一旦所有电子邮件都移动)。

I have an issue with the execution quality with the below code when trying to move emails from inbox to another folder.

inbox = inbox.Items
example_folder = inbox.Restrict("[SenderEmailAddress] = '[email protected]'")
for message in example_folder:
    message.Move(folder)

The problem is not every email will move when commanded, same happens for SenderName, Subject, and others so it's not what I'm extracting the emails by. It does work, but I need to keep executing it to catch the ones it missed.

Any suggestions to the code? If not, is there a way for me to add new code that just repeats the command automatically until i want it to stop? (once all emails have been moved).

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

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

发布评论

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

评论(3

烟织青萝梦 2025-02-12 07:58:10

首先,我建议您改用反向循环,请参见“ nofollow noreferrer”>向后迭代python 有关此的更多信息。

其次,我建议检查是否使用限制方法检查所有必需的项目。您的收集可能不包含您期望将所有项目移至文件夹中的所有项目。因此,在调试器下运行代码并比较发现多少项目并将其移至文件夹是有意义的。

First of all, I'd recommend using the reversed for loop instead, see Backward iteration in Python for more information on that.

Second, I'd suggest checking whether all the required items are found using the Restrict method. Your collection may not contain all items you expect to be moved to the folder. So, it makes sense to run the code under the debugger and compare how many items are found and moved to the folder.

痴情 2025-02-12 07:58:10

您正在修改(通过调用移动)您要迭代的收集,这可能会导致某些项目被跳过。从 itemss.count 向后使用向后进行循环,向下循环。

You are modifying (by calling Move) the very collection you are iterating over, this can result in some items being skipped. Use a backward for loop from Items.Count down to 1.

无所谓啦 2025-02-12 07:58:10

您在代码中有两个问题,当项目从收集最佳选项移动时是从结束开始,然后向后转到索引0

例如使用reversed() function with the range()函数,在reversed()方法 内包装range()方法

for i in reversed(range(5)):
    print(i)

# or reverse using the manual way = range(start, stop, step)
for i in range(5, 0, -1):
    print(f"{i}")

第二个问题是您的过滤器, [senderemailAddress] 似乎可以正常工作 emailType =“ smtp” ,但对于 Exchangeuser 尝试使用不同的过滤器

示例

from win32com.client import Dispatch


def move_items(inbox_folder):
    filter_by_sender = ("@SQL=" + chr(34) + "urn:schemas:httpmail:fromemail"
                        + chr(34) + " Like '%0m3r 0m3r%'")  # update 0m3r 0m3r

    items = inbox_folder.Items.Restrict(filter_by_sender)
    print(items.Count)

    if items.Count > 0:
        for i in range(items.Count, 0, -1):
            # check item class, 43 = MailItem
            if items[i].Class == 43:
                print(items[i].Subject)
                items[i].Move(inbox_folder.Folders["temp"])  # update "temp"


if __name__ == '__main__':
    outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.GetDefaultFolder(6)
    move_items(inbox)

You have two issues in your code, When moving items from collection best option is to start at the end and go backwards to index 0

for example use the reversed() function with the range() function, Wrap the range() method inside the reversed() method

for i in reversed(range(5)):
    print(i)

# or reverse using the manual way = range(start, stop, step)
for i in range(5, 0, -1):
    print(f"{i}")

The second issue is your filter, [SenderEmailAddress] it seems to work EmailType = "SMTP" but not for ExchangeUser try using different filter

Example

from win32com.client import Dispatch


def move_items(inbox_folder):
    filter_by_sender = ("@SQL=" + chr(34) + "urn:schemas:httpmail:fromemail"
                        + chr(34) + " Like '%0m3r 0m3r%'")  # update 0m3r 0m3r

    items = inbox_folder.Items.Restrict(filter_by_sender)
    print(items.Count)

    if items.Count > 0:
        for i in range(items.Count, 0, -1):
            # check item class, 43 = MailItem
            if items[i].Class == 43:
                print(items[i].Subject)
                items[i].Move(inbox_folder.Folders["temp"])  # update "temp"


if __name__ == '__main__':
    outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.GetDefaultFolder(6)
    move_items(inbox)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文