继续这个节目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()))
到这里还可以
现在,我尝试添加功能,仅保存从字典创建的枚举列表中选择的特定发件人的附件。我理解的过程是:我想检查所选的发送者字符串是否符合条件,是否在字典中。我尝试过使用相同的代码来搜索任何
########## 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语句的第一行有关
在你的第二个街区试试这个:
相关问题 更多 >
编程相关推荐