Google Colab、Python、Pandas、带样式的DataFrame、dataframe-image、字体使用

前端开发 2026-07-09

当我在单元格输出中显示带样式的数据框表格时,看起来正是我想要的样子;但把它们转换成图像时,使用了不同的字体。我想我理解原因:我的机器上有这种字体,但用于生成图像以创建表格的无头浏览器没有该字体。我的搜索以及在Colab实例上安装字体的尝试似乎有效,但我还没能让字体被实际使用。任何帮助都将不胜感激!

解决方案

在DataFrame的 style.set_table_styles 方法中直接添加一个 @import 规则:

# Replace 'Roboto' with your desired Google Font
df.style.set_table_styles([
    {'selector': '', 'props': [('font-family', 'Roboto')]},
    {'selector': 'th, td', 'props': [('font-family', 'Roboto')]},
    {'selector': 'html', 'props': [('@import', 'url("googleapis.com")')]}
])
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章