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

Jupyter Notebook中nbconvert导出PDF失败及MiKTeX安装问题求助

问题:Jupyter nbconvert导出PDF失败(缺失pgf包+XeLaTeX执行错误)

我在Jupyter Notebook中运行代码,尝试通过nbconvert将生成的notebook导出为PDF,但操作失败。我认为问题并非直接源于代码,代码如下:

# Import the necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
from nbconvert import PDFExporter
from nbformat import v4 as nbf

# Read the Excel file using Pandas
excel_file = pd.ExcelFile('survey_responses.xlsx')

# Use a loop to iterate over the sheets and generate pie charts and PDF files for each one
for sheet_name in excel_file.sheet_names:
    # Read the sheet using Pandas
    df = excel_file.parse(sheet_name)
    
    # Create a new Jupyter notebook for the current sheet
    notebook = nbf.new_notebook()
    
    # Add a text cell to the Jupyter notebook
    text = 'The following pie charts show the results of the survey for Class {}.'.format(sheet_name)
    notebook['cells'].append(nbf.new_markdown_cell(text))
    
    # Use the subplots function to create a figure with multiple subplots
    fig, ax = plt.subplots(nrows=len(df.columns), ncols=1, figsize=(8, 6))
    
    # Use a loop to iterate over the columns and generate a pie chart for each one
    for i, question in enumerate(df.columns[1:], start=1):
        responses = df[question].value_counts()
    
    # Add a code cell to the Jupyter notebook
        code = 'ax[{}].pie(responses, labels=responses.index)\nax[{}].set_title("{}")'.format(i, i, question)
        notebook['cells'].append(nbf.new_code_cell(code))

    
    # Use nbconvert to convert the Jupyter notebook to a PDF file
    exporter = PDFExporter()
    pdf, _ = exporter.from_notebook_node(notebook)
    with open('{}.pdf'.format(sheet_name), 'wb') as f:
        f.write(pdf)

Jupyter Notebook弹出标题为“Package Installation”的弹窗,提示缺失文件tex\latex\pgf\basiclayer\pgf.sty,该文件属于pgf包,点击安装后出现xelatex执行失败的错误,日志指向xelatex.log。我尝试了GitHub相关教程但无效,还尝试在代码开头用pip安装pandoc和MiKTeX,发现pandoc已安装,但pip无法找到MiKTeX的适配版本。请问是MiKTeX过时了吗?


解决方案
  • MiKTeX并没有过时,问题出在自动包安装流程失败,以及环境配置未到位。以下是具体解决步骤:
  1. 手动安装pgf包
    打开MiKTeX Console,切换到「Packages」标签页,在搜索框输入pgf,找到对应包后点击「Install」完成手动安装,避免自动安装时的网络或权限问题。

  2. 检查XeLaTeX路径配置
    确保MiKTeX的bin目录(例如C:\Program Files\MiKTeX\miktex\bin\x64)已添加到系统环境变量PATH中,添加后重启Jupyter Notebook再执行导出操作。

  3. 备选PDF导出方案
    如果LaTeX路径问题始终无法解决,可以换一种导出方式:

    • 先将生成的notebook导出为HTML文件,再通过浏览器的「打印」功能直接保存为PDF;
    • 或者使用WeasyPrint库,将HTML转换为PDF,避免依赖LaTeX环境。
  4. 代码缩进修正(影响图表生成)
    原代码存在缩进错误,添加代码cell的逻辑不在列循环内部,会导致仅最后一列的图表代码被写入notebook。修正后的循环部分如下:

    for i, question in enumerate(df.columns[1:], start=1):
        responses = df[question].value_counts()
        # Add a code cell to the Jupyter notebook
        code = 'ax[{}].pie(responses, labels=responses.index)\nax[{}].set_title("{}")\nplt.show()'.format(i, i, question)
        notebook['cells'].append(nbf.new_code_cell(code))
    

    同时在代码中添加plt.show(),确保图表能在notebook中正常渲染,避免导出PDF时无图表内容。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 07:45:49