使用python将outlook中的电子邮件另存为html

2024-04-27 20:03:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从每天收到的电子邮件中提取一个表。 Table in the email

我们发现的方法是手动将电子邮件保存为HTML。并运行下面的python脚本来实现这一点:

import pandas as pd

table = pd.read_html('Type the file name: ')
table1 = table[1]
last_col = len(table1.columns) - 2
new_table = table1.iloc[:, [0, 1, 3, last_col]]
new_table.to_csv('evo banco test.csv')

不过,我们要做的是在python中自动化此任务:

  1. 将电子邮件另存为HTML
  2. 将电子邮件移动到其他文件夹
  3. 从最近创建的HTML文件中提取表
  4. 将创建的CSV文件保存到其他文件夹中

现在,我仍然停留在如何用HTML保存电子邮件上。这些电子邮件来自一个共享邮箱,我可以用下面的脚本找到这个文件夹:

import win32com.client as client
import pandas as pd

#Opening Outlook
outlook = client.Dispatch("Outlook.Application")
namespace = outlook.GetNameSpace('MAPI')
drafts = namespace.GetDefaultFolder(16)
inbox = namespace.GetDefaultFolder(6)

#Getting to Report PGT Folder
analytics = namespace.Folders['BPS Bionix Analytics']
print(analytics.Name)
analytics_inbox = analytics.Folders[12]
print(analytics_inbox.Name)
folder = analytics_inbox.Folders[0]
print(folder.name)
folder_evo = folder.Folders[0]
print(folder_evo.name)
report_pgt = folder_evo.Folders[1]
print(report_pgt.name)
print(report_pgt.Items.Count)
for item in report_pgt.Items:
    item.SaveAs("C:\\Music\\email.html", 3)

#Transform table into CSV - WHERE I'M GETTING AN ERROR
'''message = report_pgt.Items[140]
message_name = message.subject
message_content = message.body
message.SaveAs(os.getcwd() + message_name +'.html')
print(message_content)
table = pd.read_clipboard(message_content)
table.to_csv('test.csv')
table1 = table[1]
last_col = len(table1.columns) - 2
new_table = table1.iloc[:, [0, 1, 3, last_col]]
new_table.to_csv('evo banco test.csv')'''

如果有人能帮我,我会很高兴的。 提前谢谢


Tags: csvnamereportmessage电子邮件htmltablefolder