简体   繁体   English

Python:下载 Outlook 附件并在保存时更改文件名

[英]Python: Downloading Outlook Attachments and Change Filename on Save

the following code works below, however i am trying to add a parameter that changes the filename on the SaveAsFile method to the iteration of (a) the message that i am on.以下代码在下面工作,但是我试图添加一个参数,将 SaveAsFile 方法上的文件名更改为 (a) 我正在处理的消息的迭代。

As an Example the Current output is例如,当前 output 是

Returned mail see transcript for details
Returned mail see transcript for details

The Desired output is所需的 output 是

Returned mail see transcript for details1
Returned mail see transcript for details2
Returned mail see transcript for details3

Currently this code just overwrites the same save file in my folder, however i need to accomplish saving that same file from different messages to a new file name.目前这段代码只是覆盖了我文件夹中的同一个保存文件,但是我需要完成将同一个文件从不同的消息保存到一个新的文件名。

Code Below:下面的代码:

import win32com.client
import os
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case the inbox. You can change that number to reference
messages = inbox.Items
message = messages.GetFirst()
subject = message.Subject
i = 0
#
get_path = r'S:\Corporate Shared\Contracting Shared\DATA_PROJECTS\James\Email Extraction\Undeliverable Items'

for m in messages:
    i = i + 1 #numeration
    a = str(i) #Creates i as a string
    if m.Subject == ("Returned mail: see transcript for details"):
        #print(message)
        attachments = message.Attachments
        num_attach = len([x for x in attachments])
        for x in range(1, num_attach + 1):
            attachment = attachments.Item(x)
            attachment.SaveASFile(os.path.join(get_path,attachment.FileName))
            print(attachment)
            #print(a)
        message = messages.GetNext()

    else:
        message = messages.GetNext()

Instead of using attachment.FileName in the call to os.path.join , store attachment.FileName in a variable, then replace the last "."不要在调用os.path.join时使用attachment.FileName ,而是将attachment.FileName存储在一个变量中,然后替换最后一个"." with "_" + x + ".""_" + x + "."

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

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