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

求助:Jupyter Notebook中Markdown插入图片并导出PDF的方法及相关问题解决

Alright, let's tackle your two issues one by one—first fixing that broken PDF export when using Markdown images, then tweaking the output style to feel more like a polished article.

1. Fixing Broken PDF Exports with Markdown Images

The problem here is that Jupyter stores images added via ![image.png](attachment:image.png) as internal notebook attachments, which the LaTeX-based nbconvert tool often struggles to parse correctly, leading to corrupted PDFs. Here are three reliable fixes:

  • Use absolute/relative file paths instead of attachments
    Move your image file to the same directory as your notebook (or a subfolder like ./images/), then update your Markdown to reference the file directly:

    ![Your Image Alt Text](image.png)
    

    For images in another folder, use a relative path like ./images/image.png or a full absolute path (note Windows paths need forward slashes or double backslashes: C:/Users/my_folder/image.png). This lets the LaTeX exporter locate the image directly without relying on internal attachments.

  • Adjust nbconvert export parameters
    If you want to stick with LaTeX export, run the export via command line with this flag to ensure proper handling of resources:

    jupyter nbconvert --to pdf your_notebook.ipynb --allow-chromium-download
    

    Alternatively, you can switch to exporting via Chromium (which converts the notebook to HTML first, then to PDF) by using:

    jupyter nbconvert --to pdf --webpdf your_notebook.ipynb
    

    This method often handles embedded content more reliably than pure LaTeX.

  • Clear notebook output and re-export
    Sometimes cached outputs or corrupted attachment data cause issues. Clear all outputs first:

    jupyter nbconvert --clear-output your_notebook.ipynb
    

    Reopen the notebook, re-run any necessary cells, then try exporting to PDF again.

2. Styling the Exported PDF to Resemble a Proper Article

To ditch the gray code cell boxes and get a more article-like layout, you have a few options depending on whether you prefer LaTeX or HTML-based exports:

Option 1: Customize the LaTeX Template (for LaTeX-based PDF)

  1. First, export the default LaTeX template to edit:
    jupyter nbconvert --show-template latex > custom_article_template.tplx
    
  2. Open custom_article_template.tplx in a text editor and make these changes:
    • Remove code cell gray backgrounds: Look for sections related to code blocks (usually using lstlisting or Verbatim environments). Find lines setting backgroundcolor and delete them, or set the color to white (\color{white}).
    • Adjust page layout: Add LaTeX packages to tweak margins, font, and spacing. For example, add this at the top of the template to set 1-inch margins:
      \usepackage{geometry}
      \geometry{margin=1in}
      
    • Remove code cell borders: If there's a border around code blocks, find the corresponding frame or border settings and disable them.
  3. Export your notebook using the custom template:
    jupyter nbconvert --to pdf your_notebook.ipynb --template custom_article_template.tplx
    

Option 2: Use HTML-to-PDF for More Flexible Styling

  1. Export your notebook to HTML first:
    jupyter nbconvert --to html your_notebook.ipynb
    
  2. Open the HTML file in a text editor and modify the CSS:
    • Find the .code_cell or .highlight classes and set background-color: transparent; and border: none; to remove gray boxes and borders.
    • Adjust other styles like font family, line spacing, or page margins to match article formatting.
  3. Convert the modified HTML to PDF:
    • Use Chrome's print function (Ctrl+P) and select "Save as PDF" for a quick option.
    • Or use pandoc for command-line conversion:
      pandoc your_notebook.html -o your_notebook.pdf
      

Option 3: Hide Code Cells Entirely (If You Don't Need Them)

If you only want to show your content (not code), export with the --no-input flag to hide all code cells:

jupyter nbconvert --to pdf your_notebook.ipynb --no-input

This instantly gives you a clean, article-like PDF focused on your text and images.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.01 00:22:46