python - 从 .msg 文件中提取 .xlsx 附件

标签 python vba attachment data-extraction

我知道这个问题已经在这里被问过好几次了,而且我已经尝试过显然对其他人有效的方法...我有超过 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/

相关文章:

excel - 动态更改功能区的按钮标签 Excel

java - 在 Android 中将 PDF 作为电子邮件附件发送

python - 在python的unittest中,如何模拟其中包含伪造图像的伪造文件夹?

java - 从 Java 运行 Python 脚本并传递用户输入

vba - MS Access - 通过取消报告创建的 "phantom"进程

Excel函数从没有VBA的值构造数组

html - 在 VB 6.0 中将 HTML 文件附加为电子邮件

c# - 如何通过电子邮件发送 Excel 文件?

python - 如何在pymongo中使用isodate查询

python - 使用python将整个文本文件加载到数据库中