[英]Python can't read latest Outlook attachments, only older ones
情況:
我每天都會收到一封帶有附件的電子郵件,但是我不想一直都需要手動保存它,因此我編寫了一個腳本來為我下載。
我正在使用Python庫win32com在后台運行Outlook:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
該腳本查找帶有附件的最新電子郵件並保存。
問題:
它不會保存最新的附件。 它始終停留在同一封電子郵件中,就像Outlook根本沒有更新一樣。 它唯一有效的時間是刪除我的Outlook配置文件並創建一個新的配置文件。 關於這種行為的原因是什么?
問候,
堂野
碼:
# -*- coding: utf-8 -*-
import datetime
import pandas as pd
import win32com.client
path = "C:\some\path"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
def getfoldernum():
i = 1
for x in outlook.Folders:
if ('bob@example.com' == str(x)):
print 'Found the Folder'
return i
else:
i += 1
def main():
foldernum = getfoldernum()
inbox = outlook.Folders.Item(foldernum).Folders('Inbox')
d = 0
w = 0
messages = inbox.Items
for msg in messages:
print msg.SentOn
if msg.Attachments:
attachments = msg.Attachments
for attachment in attachments:
if 'Attachment name' in str(attachment.FileName):
location = path + 'Archive\\Daily\\'+str(attachment.FileName)
attachment.SaveAsFile(location)
df = pd.read_excel(location)
if d == 0:
attachment.SaveAsFile(path+'filename.xlsx')
d = 1
else:
print 'Attachment not found or wrong name'
if __name__ == '__main__':
main()
您的腳本將對碰巧出現在集合Items中的第一個項目起作用,並且您沒有做任何事情來指定應該如何對這個集合進行排序。 因此,如果該集合碰巧排在最前,那么您的代碼就可以正常工作。 您應該使用Items.Sort來指定排序順序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.