该程序的继续Reference list index to enumerate value

该程序将针对Outlook中的各种条件(msg.sender msg.subject msg.SenderEmailAddress等)搜索发件人词典,然后从与该词典和条件匹配的所有发件人中下载附件。这个工作正常,我已经使用了一段时间了。

#Code before this allows me to select a date range to search

senderDict = {sender email : sender name}
listSender = list(senderDict.values())
listSender.insert(0, "Search Any")

########### Choose option from listSender
while True:
        try:
            senderNum = int(input("Choose a sender: "))
            senderChoice = listSender[senderNum] #refer senderNum to list index

            #Search all
            if senderNum == 0:
                    print("Searching for any sender")
                    break


            elif senderNum <= len(listSender) and senderNum > 0:
                    print("you chose", senderChoice)
                    break

            elif senderNum < 0:
                    print("Cannot choose a negative number.")
                    continue


############# CODE TO CHECK SENDER ATTACHMENTS
for msg in reversed(itcontents): #reversed() will go from most recent to oldest email based on date

########## Search for ANY sender
    if msg.Class == 43: #only search mail items (class 43)
            try:
                    if ( senderNum == 0 and
                            (str(msg.SenderEmailAddress) or str(msg.Subject) or str(msg.Sender) or str(msg.SentOnBehalfOfName)) in senderDict
                            and
                             #msg.SentOn date is not older than searchDate or newer than startDate
                            (msg.SentOn.date() >= searchDate and msg.SentOn.date() <= startDate.date())
                        ):
                            check += 1
                            print(check, "messages from", msg.SenderEmailAddress, "on", msg.SentOn.date()) #keep count of checked messages

                            #Check attachment file format string, invoices are usually PDFs.
                            #x refers to the attachment. Not every message from a listed sender has an attachment.
                            for x in msg.Attachments:
                                    if str(".pdf").casefold() in str(x): #casfold() checks possible upper or lower case combinations e.g PdF or pDf
                                            x.SaveAsFile(r"C:\Users\camerona\Desktop\Invoices from Outlook\\" + str(msg.SentOn.date()) + str(msg.SenderEmailAddress) + x.FileName)
                                            print("Saved attachment", x, "from", str(msg.Sender()), "on", str(msg.SentOn.date()))


到这里为止一切正常。

现在,我正在尝试添加功能,以仅保存从从字典创建的枚举列表中选择的SPECIFIC发件人发送的附件。据我了解的过程:我想检查所选的发件人字符串是否符合条件并且在字典中。我尝试使用相同的代码搜索ANY。

########## Search for SPECIFIC sender
               elif ( str(senderChoice) in ( str(msg.SenderEmailAddress) or str(msg.Subject) or str(msg.Sender) or str(msg.SentOnBehalfOfName)) in senderDict
                        and
                        (msg.SentOn.date() >= searchDate and msg.SentOn.date() <= startDate.date())
                              ):
                                check += 1
                                print(check, "messages from", msg.SenderEmailAddress, "on", msg.SentOn.date()) #keep count of checked messages

                                #Check attachment file format, invoices are usually PDFs
                                #x refers to the attachment. Not every message from a listed sender has an attachment.
                                for x in msg.Attachments:
                                        if str(".pdf").casefold() in str(x): #casfold() cheks upper or lower case format
                                                x.SaveAsFile(r"C:\Users\camerona\Desktop\Invoices from Outlook\\" + str(msg.SentOn.date()) + str(msg.SenderEmailAddress) + x.FileName)
                                                print("Saved attachment", x, "from", str(msg.Sender()), "on", str(msg.SentOn.date()))


每个附件的输出(已编辑)如下所示:

Choose a sender: 0
Searching for any sender
1 messages from xxxxxxx.com on 2019-12-16
Saved attachment Invoice INV-001172.pdf from xxxxxxxxx on 2019-12-16


然后,选择该发件人没有任何作用:

Choose a sender: 1
you chose xxxxxx
(attachment sumamry should appear like above)


我不确定为什么这行不通。我觉得这一定与str(senderChoice)的elif语句的第一行有关。

最佳答案

在第二个块中尝试此操作,而不是配合:

########## Search for SPECIFIC sender
               elif ( senderNum != 0 and
                            (str(msg.SenderEmailAddress) or str(msg.Subject) or str(msg.Sender) or str(msg.SentOnBehalfOfName)) in senderDict
                            and
                             #msg.SentOn date is not older than searchDate or newer than startDate
                            (msg.SentOn.date() >= searchDate and msg.SentOn.date() <= startDate.date())
                            and
                             #NEW CODE TO CHECK IF THIS IS THE CHOSEN SPECIFIC USER
                            any([field for field in [str(msg.SenderEmailAddress), str(msg.Subject), str(msg.Sender), str(msg.SentOnBehalfOfName)] if str(senderChoice) in field])
                        ):
                                check += 1
                                print(check, "messages from", msg.SenderEmailAddress, "on", msg.SentOn.date()) #keep count of checked messages

                                #Check attachment file format, invoices are usually PDFs
                                #x refers to the attachment. Not every message from a listed sender has an attachment.
                                for x in msg.Attachments:
                                        if str(".pdf").casefold() in str(x): #casfold() cheks upper or lower case format
                                                x.SaveAsFile(r"C:\Users\camerona\Desktop\Invoices from Outlook\\" + str(msg.SentOn.date()) + str(msg.SenderEmailAddress) + x.FileName)
                                                print("Saved attachment", x, "from", str(msg.Sender()), "on", str(msg.SentOn.date()))

关于python - Outlook-搜索特定发件人的附件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59367008/

10-10 18:36