簡體   English   中英

Python:下載 Outlook 附件並在保存時更改文件名

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

以下代碼在下面工作,但是我試圖添加一個參數,將 SaveAsFile 方法上的文件名更改為 (a) 我正在處理的消息的迭代。

例如,當前 output 是

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

所需的 output 是

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

目前這段代碼只是覆蓋了我文件夾中的同一個保存文件,但是我需要完成將同一個文件從不同的消息保存到一個新的文件名。

下面的代碼:

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()

不要在調用os.path.join時使用attachment.FileName ,而是將attachment.FileName存儲在一個變量中,然后替換最后一個"." "_" + x + "."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM