簡體   English   中英

如何使用 Python 使用自身的 msg 文件名(.msg 之前的字符串)重命名 Outlook msg(.msg 文件)中的附件

[英]How to Rename Attachments from outlook msg (.msg files) with the msg file name it self (string before .msg) with Python

我有一個包含近 12k (.msg) 文件的文件夾,每個文件都有一個 csv 附件。 我設法獲得了從每個 .msg 文件中提取附件的代碼。 但由於附件和主題相似,附件不斷寫滿! 我試圖用 msg.subject 重命名,但 msg 的主題是相似的

import win32com.client
import os
inputFolder = r'directory with my msg' ## Change here the input folder
outputFolder = r'directiry for attachments' ## Change here the attachments output folder

for file in os.listdir(inputFolder):
    if file.endswith(".msg"):
        outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
        filePath = inputFolder  + '\\' + file
        msg = outlook.OpenSharedItem(filePath)
        att = msg.Attachments
        for i in att:
            i.SaveAsFile(os.path.join(outputFolder, str(msg.subject + ".csv")))
#Saves the file with the attachment name 

您需要找到一種允許唯一識別附件的算法 - 嘗試將附件文件名與電子郵件數據(如ReceivedTime等)結合起來。

在嘗試保存附件之前,不要忘記從結果文件名中排除禁用符號。

暫無
暫無

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

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