我试图将Outlook电子邮件的Body内容保存到DataFrame,然后将其保存到CSV/Excel,我们通常以表格格式的不同索引从供应商那里获得价格,我尝试使用Body_Content = message.htmlbody,但无意使用。
因此,我可以使用body_content = message.body和print(df.to_string())。现在,下一步是将这些值存储在Excel表中,即使其以低于格式的态度打开。
我们不断从不同的供应商那里获得指数价格,如果这个供应商运行得很好,我也会遵循其他人的方法。
output
这就是它最终获得单列DataFrame
import pandas as pd
import win32com.client
import re
import os
import pandas
import datetime
from datetime import date
EMAIL_ACCOUNT = '[email protected]'
EMAIL_SUBJ_SEARCH_STRING = 'MSBPSCSP Index Price'
EMAIL_CONTNT = {'Ticker': [], 'TickerLevel': [], 'DATE': []}
out_app = win32com.client.gencache.EnsureDispatch("Outlook.Application")
out_namespace = out_app.GetNamespace("MAPI")
#lastWeekDateTime = dt.datetime.now() - dt.timedelta(days=1)
#lastWeekDateTime = lastWeekDateTime.strftime('%m/%d/%Y %H:%M %p')
root_folder = out_namespace.GetDefaultFolder(6)
out_iter_folder = root_folder.Folders['Email_Snapper']
#char_length_of_search_substring = len(EMAIL_SUBJ_SEARCH_STRING)
item_count = out_iter_folder.Items.Count
Flag = False
cnt = 1
if item_count > 0:
for i in range(item_count, 0, -1):
message = out_iter_folder.Items[i]
if EMAIL_SUBJ_SEARCH_STRING in message.Subject:
#message = message.Restrict("[ReceivedTime] >= '" + lastWeekDateTime + "'")
Body_content = message.Body
Body_content = Body_content[:Body_content.find("Disclaimer")].strip()
df = pd.DataFrame([Body_content])
print(df.to_string())
在Oracle数据库中,要么首先将其保存在Excel中,也可以直接从数据框架中保存(甚至更好)
i am trying to save outlook email body content to dataframe then to csv/excel, we usually get prices from vendor for different indices in tabular format, i tried using Body_content = message.HTMLBody but didnt work as intented.
Thus i am ok with using Body_content = message.Body and print (df.To_string()). Now the next step is to store those values in excel sheet even if its open by an anlyst in below format.
we keep getting index prices from different vendors, if this one works perfectly, i will follow the same method for others as well.
Output

This is how it ends up getting a single column dataframe

import pandas as pd
import win32com.client
import re
import os
import pandas
import datetime
from datetime import date
EMAIL_ACCOUNT = '[email protected]'
EMAIL_SUBJ_SEARCH_STRING = 'MSBPSCSP Index Price'
EMAIL_CONTNT = {'Ticker': [], 'TickerLevel': [], 'DATE': []}
out_app = win32com.client.gencache.EnsureDispatch("Outlook.Application")
out_namespace = out_app.GetNamespace("MAPI")
#lastWeekDateTime = dt.datetime.now() - dt.timedelta(days=1)
#lastWeekDateTime = lastWeekDateTime.strftime('%m/%d/%Y %H:%M %p')
root_folder = out_namespace.GetDefaultFolder(6)
out_iter_folder = root_folder.Folders['Email_Snapper']
#char_length_of_search_substring = len(EMAIL_SUBJ_SEARCH_STRING)
item_count = out_iter_folder.Items.Count
Flag = False
cnt = 1
if item_count > 0:
for i in range(item_count, 0, -1):
message = out_iter_folder.Items[i]
if EMAIL_SUBJ_SEARCH_STRING in message.Subject:
#message = message.Restrict("[ReceivedTime] >= '" + lastWeekDateTime + "'")
Body_content = message.Body
Body_content = Body_content[:Body_content.find("Disclaimer")].strip()
df = pd.DataFrame([Body_content])
print(df.to_string())
The final objective is to insert those prices in oracle database either through saving them in excel first or if possible directly from the dataframe (that's even better)
发布评论
评论(2)
您可以将
dataFrame
导出到exceldf.to_excel(“ output.xlsx”,index = false)
或csv -
df.to_excel(“ output.csv”) ,index = false)
You can export the
dataFrame
to exceldf.to_excel("output.xlsx", index=False)
or CSV -
df.to_excel("output.csv", index=False)
Outlook对象模型支持自定义或获取消息主体的三种主要方法:
HTMLBody
属性将始终立即更新body
属性。例如:要选择哪种方式取决于您。但是我想“对象模型”一词可以帮助您提取所需的信息。
The Outlook object model supports three main ways of customizing or getting the message body:
MailItem
class returns or sets a string representing the HTML body of the specified item. Setting theHTMLBody
property will always update theBody
property immediately. For example:It is up to you which way is to choose. But I suppose the Word object model can help you with extracting the required information.