如何在Excel/CSV中保存数据帧值

发布于 2025-02-04 11:10:55 字数 1917 浏览 2 评论 0 原文

我试图将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

enter image description here

This is how it ends up getting a single column dataframe

enter image description here

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2025-02-11 11:10:55

您可以将 dataFrame 导出到excel

df.to_excel(“ output.xlsx”,index = false)

或csv -

df.to_excel(“ output.csv”) ,index = false)

You can export the dataFrame to excel

df.to_excel("output.xlsx", index=False)

or CSV -

df.to_excel("output.csv", index=False)

碍人泪离人颜 2025-02-11 11:10:55

Outlook对象模型支持自定义或获取消息主体的三种主要方法:

  1. 正文属性返回或设置一个代表Outlook项目的清晰文本主体的字符串。
  2. mailItem 类返回或设置代表指定项目的HTML主体的字符串。设置 HTMLBody 属性将始终立即更新 body 属性。例如:
     Sub CreateHTMLMail() 
       'Creates a new e-mail item and modifies its properties. 
       Dim objMail As Outlook.MailItem 
       'Create e-mail item 
       Set objMail = Application.CreateItem(olMailItem) 
       With objMail 
        'Set body format to HTML 
        .BodyFormat = olFormatHTML 
        .HTMLBody = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>" 
        .Display 
       End With 
     End Sub
  1. 一词对象模型可用于处理消息物体。参见第17章:与物体有关更多信息。因此,您可以使用“对象模型”一词获得所需的信息。

要选择哪种方式取决于您。但是我想“对象模型”一词可以帮助您提取所需的信息。

The Outlook object model supports three main ways of customizing or getting the message body:

  1. The Body property returns or sets a string representing the clear-text body of the Outlook item.
  2. The HTMLBody property of the MailItem class returns or sets a string representing the HTML body of the specified item. Setting the HTMLBody property will always update the Body property immediately. For example:
     Sub CreateHTMLMail() 
       'Creates a new e-mail item and modifies its properties. 
       Dim objMail As Outlook.MailItem 
       'Create e-mail item 
       Set objMail = Application.CreateItem(olMailItem) 
       With objMail 
        'Set body format to HTML 
        .BodyFormat = olFormatHTML 
        .HTMLBody = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>" 
        .Display 
       End With 
     End Sub
  1. The Word object model can be used for dealing with message bodies. See Chapter 17: Working with Item Bodies for more information. So, you can get the required information using the Word object model.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文