使用python将DataTable导出到outlook mail
我很难找到从Dash WebApp导出预格式化数据表到outlook电子邮件正文的方法。 我使用条件格式设置DataTable的格式,并尝试向dash webapp添加一个按钮,该按钮带有一个回调,该回调将捕获DataTable并将其快照导出到outlook电子邮件正文中,以便我可以将其发送给需要接收电子邮件的任何人 下面的示例基于一个虚拟数据框,我通过pandas将该虚拟数据框从一个虚拟excel文件导入python,然后通过dash_table转换为一个Datatable,目的是为您提供一个示例,其中包含我迄今为止使用的代码: 数据帧:使用python将DataTable导出到outlook mail,python,email,datatable,email-attachments,plotly-dash,Python,Email,Datatable,Email Attachments,Plotly Dash,我很难找到从Dash WebApp导出预格式化数据表到outlook电子邮件正文的方法。 我使用条件格式设置DataTable的格式,并尝试向dash webapp添加一个按钮,该按钮带有一个回调,该回调将捕获DataTable并将其快照导出到outlook电子邮件正文中,以便我可以将其发送给需要接收电子邮件的任何人 下面的示例基于一个虚拟数据框,我通过pandas将该虚拟数据框从一个虚拟excel文件导入python,然后通过dash_table转换为一个Datatable,目的是为您提供一个
**Name** **Status** **Age**
John Working 31
Michael Not working 35
Luke Working 39
Steven Working 28
import pandas as pd
import dash_table
df = pd.read_excel ('PATH TO MY EXCEL FILE')
tbl = dash_table.DataTable(
id='table',
columns=[{'name':i,'id':i} for i in df.columns],
data=df.to_dict('records'),
sort_action='native',
filter_action="native",
sort_mode='multi',
page_action='native',
page_current=0,
page_size=5,
style_data={
'whiteSpace': 'normal',
'height': '1px',
'textAlign': 'center',
},
style_header={
'backgroundColor': 'DarkSlateGray',
'fontWeight': 'bold',
'color': 'white',
'textAlign': 'center',
'border': '2px solid grey',
'padding': '0.5rem 0rem 0rem 0.5rem',
},
style_cell={'border': '2px solid DarkSlateGray',
'overflow': 'hidden',
'textOverflow': 'ellipsis',
'height': '1rem',
'font-size': '12px',
'font-family': 'Arial, Helvetica, sans-serif',
'font-weight': 'bold'},
style_data_conditional = [
{
'if': {
'filter_query': '{Status} contains "Working"',
},
'border': '3px solid DarkSlateGray',
'backgroundColor': 'lightgreen'
},
],
)
下面的数据表生成代码:
**Name** **Status** **Age**
John Working 31
Michael Not working 35
Luke Working 39
Steven Working 28
import pandas as pd
import dash_table
df = pd.read_excel ('PATH TO MY EXCEL FILE')
tbl = dash_table.DataTable(
id='table',
columns=[{'name':i,'id':i} for i in df.columns],
data=df.to_dict('records'),
sort_action='native',
filter_action="native",
sort_mode='multi',
page_action='native',
page_current=0,
page_size=5,
style_data={
'whiteSpace': 'normal',
'height': '1px',
'textAlign': 'center',
},
style_header={
'backgroundColor': 'DarkSlateGray',
'fontWeight': 'bold',
'color': 'white',
'textAlign': 'center',
'border': '2px solid grey',
'padding': '0.5rem 0rem 0rem 0.5rem',
},
style_cell={'border': '2px solid DarkSlateGray',
'overflow': 'hidden',
'textOverflow': 'ellipsis',
'height': '1rem',
'font-size': '12px',
'font-family': 'Arial, Helvetica, sans-serif',
'font-weight': 'bold'},
style_data_conditional = [
{
'if': {
'filter_query': '{Status} contains "Working"',
},
'border': '3px solid DarkSlateGray',
'backgroundColor': 'lightgreen'
},
],
)
创建outlook项目并将DataTable粘贴到电子邮件正文中的代码:
import win32com.client as win32
import datetime
subject = "Email test: {}".format(datetime.datetime.now())
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'EMAIL ADRESS'
mail.CC = 'EMAIL ADDRESS'
mail.Subject = subject
mail.HtmlBody = tbl
mail.Display(False)
我得到的错误:
AttributeError:无法设置属性“CreateItem.HtmlBody”
有人知道这个问题的解决办法吗?
(我需要在电子邮件正文中使用与Dash DataTable相同的格式设置该表,该表将在实际脚本中包含多个条件格式设置指令,我只编写了1,以尽可能简化示例)
提前感谢您的帮助
干杯 有人能帮忙吗(您的错误来自您正在使用的
win32
软件包。您可能希望重新标记此问题,以包含该问题或某种与电子邮件相关的标记。@coralvanda但是,当我使用字符串而不是表运行同一脚本时,会生成电子邮件。那么,错误是如何来自win32软件包的呢?好的。那么,错误是误导性的。在在这种情况下,可以将表作为JSON对象转储吗?如果字符串有效,JSON也应该有效。