通过Python进行数据帧样式设计

问题描述 投票:1回答:1

我试图通过python发送outlook邮件。我使用数据框从excel文件导入一些数据,经过一些过滤后,我将它们发送到邮件正文的位置:

    body = '<html><body>'+df.to_html()+'</body></html>'

我得到一个表,但细胞没有对齐。我不能使用justify='left/write',因为我希望细胞居中对齐,而不仅仅是列标题。我使用了样式,但它也没有用:

df.style.set_properties(subset=df.columns,**{'width':'10em', 'text-align':'center'})\

我也尝试了这个并没有用。

p=HTML(df.to_html(classes= 'table text-align:center'))

在浏览类似问题后,我找到了另一个解决方案

s = df.style.set_properties(**{'text-align': 'center'})
s.render()

然而它使表的border消失。所以我修改它:

s = df.style.set_properties(**{'text-align': 'center','border-color':'Black','border-width':'thin','border-style':'dotted'})

这给了细胞border。但它看起来像each cell在个人textbox,not内部像table。我怎么做到这一点?

最终结果如下:

python dataframe textbox styles cell
1个回答
0
投票

你可以在没有太多编辑的情况下完成这项工作,你只需要添加:

'border-collapse':'collapse'

html = df[['Column 1','Column 2',]].style.hide_index().set_properties(**{'font-size': '11pt','background-color': '#edeeef','border-color': 'black','border-style' :'solid' ,'border-width': '0px','border-collapse':'collapse'}).applymap(style1,subset = 'Column 1').applymap(Style2 ,subset = 'Column 2').render()

注意:你可以忽略上面这段代码片段的其他部分,只有用处是 - - 'border-collapse':'collapse'

所以现在边框内的边框将被解析为只有一个边框

A simple single line border

© www.soinside.com 2019 - 2024. All rights reserved.