简体   繁体   English

如何在Python和Outlook中使用for循环在电子邮件中保存所有附件?

[英]How to save all attachments in email using for loop in Python and Outlook?

This is the code I have which is functioning perfectly fine, if I had 1 attachment in the email. 如果我的电子邮件中有1个附件,这就是我的代码,可以很好地运行。 The issue becomes when there are multiple attachments in the email, all of which I would like to perform functions on. 问题出现在电子邮件中有多个附件时,我希望对所有附件执行功能。

def get_email():
    import win32com.client
    import os
    import time
    import datetime as dt

    date_time = time.strftime('%m-%d-%Y')

    outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")

    inbox = outlook.GetDefaultFolder(6)

    messages = inbox.Items
    message = messages.GetFirst()  # any time calling GetFirst(), you can get GetNext()....
    # body_content = message.body

    try:
        attachments = message.Attachments
        attachment = attachments.Item(1)
        report_name = date_time + '_' + attachment.FileName

        attachment.SaveASFile(os.getcwd() + '\\' + report_name)
        print('Attachment saved: ' + report_name)

    except: #***********add error logging here**************
        print('No attachment found.')

How would I put this into a for loop and say - for every x, attachment = attachments.Item(x) - save that attachment, and run another function based on the attachment that just saved. 我如何将其放入for循环并说-对于每个x, attachment = attachments.Item(x) -保存该附件,并基于刚刚保存的附件运行另一个函数。 Is there a way I can define that x variable to give me the amount of attachments in the email and then run it through the for loop? 有没有一种方法可以定义x变量,以便给我电子邮件中的附件数量,然后通过for循环运行它? Or is there a way to run the for loop and - without producing any errors - stop once the last attachment is found? 还是有一种方法可以运行for循环,并且-在不产生任何错误的情况下-找到最后一个附件后停止运行?

It looks like an Attachments collection can work in a for loop, like a normal list. 看起来Attachments集合可以像普通列表一样在for循环中工作。 I got the following to save each attachment in the email and not throw errors on an email with no attachments: 我得到以下内容来将每个附件保存在电子邮件中,并且不会在没有附件的电子邮件中引发错误:

import win32com.client
import os
import time
import datetime as dt

date_time = time.strftime('%m-%d-%Y')

outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")

inbox = outlook.GetDefaultFolder(6)

messages = inbox.Items
message = messages.GetFirst()

attachments = message.Attachments

# The following line works, so you can see how many attachments are on an item
print len(attachments)
# Here's another way to get the number of attachments
print attachments.Count

# since attachments works similar to a list, a for loop works
# if there are no attachments, it won't enter the for loop, and won't throw an error
for attachment in attachments:
    report_name = date_time + '_' + attachment.FileName

    attachment.SaveASFile(os.getcwd() + '\\' + report_name)
    print('Attachment saved: ' + report_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM