私は、受信トレイをスキャンして(逆に、最新のファイルを取得するように)、今日受信された件名が「Interest」の電子メールを探すために、以下に書き込みました。
for message in reversed(messages):
if message.SentOn.date() == today and 'Interest' in str(message.subject):
print("Found message")
attachments = message.Attachments
attachment = attachments.Item(1)
for attachment in message.Attachments:
attachment.SaveAsFile(os.path.join(path, str(attachment)))
else:
print("No interest to be booked")
これは機能しますが、件名が「interest」、つまり小文字の「i」でメールが送信される場合があることにも注意したいと思います。コードの最初の行を次のように変更してみました。
if message.SentOn.date() == today and ['Interest' in str(message.subject) or 'interest' in str(message.subject)]:
しかし、何らかの理由でこれは機能しません。私を混乱させるのは、それでもメッセージを見つけることですが、添付ファイルをプルすることはできません。これは私が得るエラーです:
Traceback (most recent call last):
File "h:/Excel/python/todaysdateandformat.py", line 20, in <module>
attachment = attachments.Item(1)
File "<COMObject <unknown>>", line 2, in Item
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Array index out of bounds.', None, 0, -2147352567), None)
角かっこを丸かっこに置き換える代わりに、より簡単で柔軟な方法として、対象を一致させる前に小文字にすることができます。
if message.SentOn.date() == today and 'interest' in str(message.subject).lower():
...
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加