我知道这个问题已经在这里被问过好几次了,而且我已经尝试过显然对其他人有效的方法...我有超过 1000 个带有 .xlsx 文件附件的 Outlook .msg 文件存储在我桌面上的文件夹中,我只需要提取 .xlsx 文件以合并到单个数据帧中。
我已经尝试过VBA macro 、Python [Win32] ( Parsing outlook .msg files with python ) 和 msg-extractor 。我能做的最好的事情就是从单个 .msg 文件中提取单个附件
非常感谢任何建议。谢谢!
import argparse
import csv
import os as os
import pathlib
import sys
from datetime import date, datetime, timedelta, tzinfo
from enum import Enum, IntEnum
from tempfile import mkstemp
import dateutil.parser as duparser
from dateutil.rrule import rrulestr, rruleset
import pywintypes
import pytz
import win32com.client
path = r'C:\Users\Me\Desktop\MyFiles\feb_2018'
files = [f for f in os.listdir(path) if '.msg' in f]
print (files)
for file in files:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
msg = outlook.OpenSharedItem(os.path.join(path, file))
att=msg.Attachments
for i in att:
i.SaveAsFile(os.path.join(path, i.FileName))
最佳答案
我还没有尝试使用 win32com 保存附件,所以我不知道为什么只保存单个文件中的单个附件。但我能够使用 msg-extractor 保存多个附件
import extract_msg
for file in files:
msg = extract_msg.Message(file)
msg_attachment = msg.attachments
attach_path = "path where the files have to be saved."
for attachment in msg_attachment:
if not os.path.exists(attach_path):
os.makedirs(attach_path)
attachment.save(customPath=attach_path)
关于python - 从 .msg 文件中提取 .xlsx 附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58525541/