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

为Sphinx添加谷歌字体时遇AttributeError报错,求解决方法

解决Sphinx添加谷歌字体的报错问题

错误原因分析

  • AttributeError: 'Sphinx' object has no attribute 'add_stylesheet':该报错是因为add_stylesheet是Sphinx旧版本的API,Sphinx 1.8及后续版本已弃用此方法,替代API为add_css_file。
  • CSS导入地址错误:你使用的是谷歌字体的展示页面链接,而非实际的字体CSS资源链接,无法正确加载字体文件。

修正步骤

  1. 修改styles.css的字体导入代码
    打开styles.css,替换为从谷歌字体页面"Embed"选项卡复制的正确导入代码,同时可添加字体应用规则:

    @import url('https://fonts.googleapis.com/css2?family=Sorts+Mill+Goudy&display=swap');
    
    /* 可选:设置页面整体字体 */
    body {
        font-family: 'Sorts Mill Goudy', serif;
    }
    
  2. 更新conf.py中的注册代码
    将原setup函数替换为使用新版本API的代码:

    def setup(app):
        app.add_css_file('styles.css')
    
  3. 重新构建文档
    执行make html命令重新构建Sphinx文档,此时报错应消失,字体可正常加载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 18:39:54