Python:保存.msg文件中的附件

所以我有一个包含大量.msg文件的文件夹.我希望能够保存其中一个附件.我的想法是自动点击文件,然后以某种方式提取具有特定文件名的文件,但我还没有找到任何解决方案.

我该怎么做呢?还是更好的方法?

谢谢!

更新:
我有一个想法,使用os.startfile打开我想要打开的文件…
我怎么不在另一个窗口打开它?但就像它一样吗?如果那有意义的话 :/

最佳答案 这应该工作:

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
msg = outlook.OpenSharedItem(filename)        #filename including path
att=msg.Attachments
for i in att:
    i.SaveAsFile(os.path.join(Pathname, i.FileName))#Saves the file with the attachment name

既然你说你有一个文件夹,这将自动化整个文件夹:

import win32com.client
import os
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for file in files:
    if file.endswith(".msg"):
        outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
        msg = outlook.OpenSharedItem(file)        
        att=msg.Attachments
        for i in att:
            i.SaveAsFile(os.path.join(Pathname, i.FileName))#Saves the file with the attachment name
点赞