Python win32com Outlook 不断删除错误的附件

问题描述 投票:0回答:1

我有一个脚本,应该根据特定类型附件的存在转发某些电子邮件,比方说.PDF。 我已经让它正确地对电子邮件进行排序,并且转发非常完美。唯一的问题是,当我尝试删除上述附件时,它会删除另一个附件。 我使用相同的 for 循环来识别附件,它确实识别了我想要删除的附件,但由于某种原因,索引 (???) 不再相同,并且它删除了另一个附件。 另外,我已经发现,当它删除时,它会自动更新附件索引

new_mail = message.Forward()
new_mail.To = '[email protected]'
attach_index=1

for attachment in attachments:        
        
        print("i can print the index correctly, and all the filename characteristic to select the one i want)

        if ***condition****:
            print('to remove...')
            new_mail.Attachments.Remove(attach_index)
        else:
            attach_index = attach_index+1

new_mail.Send()

在这种特殊情况下,我有一封电子邮件,签名中包含 .pdf 、 .txt 和图像,python 也将其识别为附件。我不断在正确的一个上输出“删除......”,但删除了另一个。 我究竟做错了什么?请帮忙

python outlook attachment win32com
1个回答
0
投票

使用

Attachment.Delete
而不是
Attachments.Remove(index)
- 这样您就不必处理任何索引。

© www.soinside.com 2019 - 2024. All rights reserved.