You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Colab中Pandas Styled Dataframe转图片字体不一致求助

解决Colab中Dataframe样式转图片字体不生效的问题
  • 确认字体安装状态
    安装字体后,通过Linux命令验证是否真的安装成功。比如目标字体是Noto Sans SC,运行:

    fc-list | grep "Noto Sans SC"
    

    若有输出则字体已安装;无输出说明安装步骤有误,需重新执行字体安装命令(比如通过apt安装或手动复制字体文件到/usr/share/fonts目录)。

  • 刷新系统字体缓存
    安装字体后必须刷新缓存,否则系统和浏览器无法识别新字体:

    fc-cache -fv
    
  • 确保浏览器实例加载新字体
    若使用pandas自带的df.style.to_image()(底层依赖Playwright),需重启Playwright的浏览器实例。可以先卸载再重新安装Playwright的浏览器:

    from playwright.sync_api import sync_playwright
    with sync_playwright() as p:
        p.chromium.uninstall()
        p.chromium.install()
    

    之后再调用to_image()时,浏览器会重新加载系统字体列表。

  • 精准指定字体名称
    样式中使用的字体名称必须与系统中注册的完全一致(通过fc-list查询到的名称)。比如:

    styled_df = df.style.set_table_styles([
        {'selector': 'td, th', 'props': [('font-family', '"Noto Sans SC", sans-serif')]}
    ])
    styled_df.to_image('output.png')
    

    注意字体名包含空格时,需要用双引号包裹。

  • 禁用浏览器字体替换
    无头浏览器可能会自动替换缺失字体,需添加参数禁用该行为。如果使用Selenium,启动Chrome时添加:

    from selenium import webdriver
    options = webdriver.ChromeOptions()
    options.add_argument('--headless=new')
    options.add_argument('--disable-font-substitution')
    driver = webdriver.Chrome(options=options)
    

内容的提问来源于stack exchange,提问作者user1304598

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.02 00:44:53