我正在执行Outlook电子邮件自动化任务,其中将Outlook的.msg电子邮件文件存储在目录中。我的任务是从.msg文件中提取信息(电子邮件正文,附件文本等),然后运行NLP对其进行分类。到目前为止,我已经从https://pypi.org/project/extract-msg/和https://github.com/mattgwwalker/msg-extractor使用了extract_msg 。我能够提取邮件正文文本,但接下来的挑战是面临的是:
我在写自己的问题之前先从多个线程中读取了答案,但是大多数答案都与直接从Outlook.exe中提取电子邮件有关,但是我不需要从Outlook中提取信息,而是将Outlook邮件存储为.msg本地目录中文件。
到目前为止,我的进展是:
import extract_msg
import pandas as pd
import os
direct = os.getcwd() # directory object to be passed to the function for accessing emails
ext = '.msg' # type of files in the folder to be read
def DataImporter(directory, extension):
my_list = []
for i in os.listdir(direct):
if i.endswith(ext):
msg = extract_msg.Message(i)
my_list.append([msg.filename,msg.sender,msg.to, msg.date, msg.subject, msg.body])
global df
df = pd.DataFrame(my_list, columns = ['File Name','From','To','Date','Subject','MailBody Text'])
print(df.shape[0],' rows imported')
DataImporter(direct,ext)
需求是这样的:
邮件正文=“这是示例电子邮件正文”。
附件='Invoice123'
附件文本='您的发票已准备好进行处理'
像这样的东西,将不胜感激,如果需要进一步的信息,请告诉我。
编辑:如果您知道可以用于完成此任务的任何其他软件包,请发表评论。
发布对我有用的解决方案(按Amey P Naik的要求)。如前所述,我尝试了多个模块,但是只有extract_msg适用于这种情况。我创建了两个函数,用于将Outlook消息文本和附件作为Pandas DataFrame导入,第一个函数将为电子邮件创建一个文件夹,第二个函数将消息中的数据导入到数据帧。附件需要在父目录的子目录上使用for循环进行单独处理。以下是我创建的带有注释的两个函数:
# 1). Import the required modules and setup working directory
import extract_msg
import os
import pandas as pd
direct = os.getcwd() # directory object to be passed to the function for accessing emails, this is where you will store all .msg files
ext = '.msg' #type of files in the folder to be read
# 2). Create separate folder by email name and extract data
def content_extraction(directory,extension):
for mail in os.listdir(directory):
try:
if mail.endswith(extension):
msg = extract_msg.Message(mail) #This will create a local 'msg' object for each email in direcory
msg.save() #This will create a separate folder for each email inside the parent folder and save a text file with email body content, also it will download all attachments inside this folder.
except(UnicodeEncodeError,AttributeError,TypeError) as e:
pass # Using this as some emails are not processed due to different formats like, emails sent by mobile.
content_extraction(direct,ext)
#3).Import the data to Python DataFrame using the extract_msg module
#note this will not import data from the sub-folders inside the parent directory
#rather it will extract the information from .msg files, you can use a loop instead
#to directly import data from the files saved on sub-folders.
def DataImporter(directory, extension):
my_list = []
for i in os.listdir(direct):
try:
if i.endswith(ext):
msg = extract_msg.Message(i)
my_list.append([msg.filename,msg.sender,msg.to, msg.date, msg.subject, msg.body, msg.message_id]) #These are in-built features of '**extract_msg.Message**' class
global df
df = pd.DataFrame(my_list, columns = ['File Name','From','To','Date','Subject','MailBody Text','Message ID'])
print(df.shape[0],' rows imported')
except(UnicodeEncodeError,AttributeError,TypeError) as e:
pass
DataImporter(direct,ext)
运行这两个函数后,您将在Pandas DataFrame中拥有几乎所有信息,您可以根据需要使用它们。如果还需要从附件中提取内容,则需要为父目录中的所有子目录创建一个循环,以按照其格式读取附件文件,例如在我的情况下,格式为.pdf,.jpg,.png ,.csv等。从这些格式获取数据将需要不同的技术,例如从pdf获取数据,您将需要Pytesseract OCR模块。
如果您找到一种从附件中提取内容的简便方法,请在此处发布您的解决方案以备将来参考,如果您有任何疑问,请发表评论。另外,如果上述代码有任何改进的地方,请随时强调。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句