Python 3.x 在python中将msg转换为pdf
我真的需要你帮我解决一个问题!显然,我的知识不足以找到解决办法。 因此,我已经创建并保存了一些msg文件。现在我需要编写一个函数,可以帮助我从msg文件创建PDF(其中会有很多)。Python 3.x 在python中将msg转换为pdf,python-3.x,pdf,data-conversion,msg,Python 3.x,Pdf,Data Conversion,Msg,我真的需要你帮我解决一个问题!显然,我的知识不足以找到解决办法。 因此,我已经创建并保存了一些msg文件。现在我需要编写一个函数,可以帮助我从msg文件创建PDF(其中会有很多)。 我将非常感谢你的帮助 发布对我有效的解决方案(按照Amey p Naik的要求)。如前所述,我尝试了多个模块,但仅extract_msg适用于手头的案例。我创建了两个函数,用于将outlook邮件文本和附件作为数据框导入,第一个函数将为电子邮件分别创建一个文件夹,第二个函数将数据从邮件导入数据框。需要在父目录的子目录
我将非常感谢你的帮助 发布对我有效的解决方案(按照Amey p Naik的要求)。如前所述,我尝试了多个模块,但仅extract_msg适用于手头的案例。我创建了两个函数,用于将outlook邮件文本和附件作为数据框导入,第一个函数将为电子邮件分别创建一个文件夹,第二个函数将数据从邮件导入数据框。需要在父目录的子目录上使用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)
运行这两个函数后,您将在一个数据框中拥有几乎所有的信息,您可以根据需要使用这些信息。如果还需要从附件中提取内容,则需要为父目录中的所有子目录创建一个循环,以便按照其格式读取附件文件,就像我的例子中的格式是.pdf、.jpg、.png、,.csv等。从这些格式获取数据需要不同的技术,如从pdf获取数据,则需要PyteSeract OCR模块
如果您找到一种更简单的方法从附件中提取内容,请在此处发布您的解决方案以供将来参考,如果您有任何问题,请发表评论。此外,如果上述代码有任何改进的余地,请随时强调。发布对我有效的解决方案(如Amey p Naik所问)。如前所述,我尝试了多个模块,但仅extract_msg适用于手头的案例。我创建了两个函数,用于将outlook邮件文本和附件作为数据框导入,第一个函数将为电子邮件分别创建一个文件夹,第二个函数将数据从邮件导入数据框。需要在父目录的子目录上使用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)
运行这两个函数后,您将在一个数据框中拥有几乎所有的信息,您可以根据需要使用这些信息。如果还需要从附件中提取内容,则需要为父目录中的所有子目录创建一个循环,以便按照其格式读取附件文件,就像我的例子中的格式是.pdf、.jpg、.png、,.csv等。从这些格式获取数据需要不同的技术,如从pdf获取数据,则需要PyteSeract OCR模块
如果您找到一种更简单的方法从附件中提取内容,请在此处发布您的解决方案以供将来参考,如果您有任何问题,请发表评论。此外,如果上述代码中存在任何改进范围,请随时强调