通过 Python 进行数据框样式设置

我正在尝试通过 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,不像一个table。我如何完成这项工作?

最终结果如下所示:

http://img.mukewang.com/60c0214400014e9203290150.jpg

jeck猫
浏览 89回答 2
2回答

米琪卡哇伊

您无需进行太多编辑即可完全做到这一点,您只需要添加:'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'所以现在边界内的边界将被解析为只有一个边界
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python