如何用python从Outlook msg(.msg Files)重命名附件(.msg文件)。
我有一个文件夹,该文件夹具有将近12k(.msg)文件的文件夹,每个文件具有CSV附件。 我设法获得了一个代码来从每个.msg文件中提取附件。但是由于依恋和受试者,依恋不断克服书面!我尝试使用msg.subject重命名,但味精的主题相似
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
I have a folder that has nearly 12k (.msg) files each has a csv attachment.
I managed to get a code to extract the attachment from each .msg file. but due to attachment and subjects are similar the attachment keeps getting over written! I tried to rename with msg.subject but the subject of the msg is similar
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要找到允许唯一识别附件的算法 - 尝试将附件文件名与电子邮件数据相结合,例如
You need to find an algorithm which allows identifying attachments uniquely - try to combine the attachment file name with email data such as ReceivedTime and etc.
Don't forget to exclude forbidden symbols from the result file name before trying to save the attachment.