当前位置: 首页>后端>正文

01.如何在 Jupyter Notebook 中将 ipynb 转换为 PDF

01.参考文章:

01.https://saturncloud.io/blog/how-to-convert-ipynb-to-pdf-in-jupyter-notebook/
02.https://ecoagi.ai/topics/Python/ipynb-to-pdf
内容转自:https://saturncloud.io/blog/how-to-convert-ipynb-to-pdf-in-jupyter-notebook/

If you’re a data scientist or software engineer, you’re likely to have spent a considerable amount of time working with Jupyter Notebook. Jupyter Notebook is an open-source web application that allows you to create and share documents containing live code, equations, visualizations, and narrative text. 如果你是数据科学家或软件工程师,你可能已经花费了大量时间使用 Jupyter Notebook。Jupyter Notebook 是一个开源 Web 应用程序,可用于创建和共享包含实时代码、方程式、可视化效果和叙述性文本的文档。

One of the most common tasks data scientists and software engineers perform in Jupyter Notebook is exporting their notebooks to different formats, such as PDF. Converting your Jupyter Notebook to PDF format is useful when you need to share your work with others who don’t have access to Jupyter Notebook or when you want to create a static snapshot of your notebook. 数据科学家和软件工程师在 Jupyter Notebook 中执行的最常见任务之一是将其笔记本导出为不同的格式,例如 PDF。当您需要与无权访问 Jupyter Notebook 的其他人共享您的工作或想要创建笔记本的静态快照时,将 Jupyter Notebook 转换为 PDF 格式非常有用。

In this tutorial, we’ll cover the steps to convert your Jupyter Notebook to PDF format using different methods. 在本教程中,我们将介绍使用不同方法将 Jupyter Notebook 转换为 PDF 格式的步骤。

Get fully managed Jupyter, VS Code, Dask clusters, models, dashboards and jobs. 获取完全托管的 Jupyter、VS Code、Dask 群集、模型、仪表板和作业。

Saturn Cloud is one of the top DS/ML/AI platforms on G2. Join today and get 150 hours of free compute every month. Saturn Cloud 是 G2 上顶级的 DS/ML/AI 平台之一。立即加入,每月可获得 150 小时的免费计算。

Method 1: Using nbconvert 方法 1:使用 nbconvert

The first method we’ll cover is using nbconvert, a command-line tool that converts Jupyter Notebooks to various formats, including PDF. Follow the steps below to convert your Jupyter Notebook to PDF using nbconvert. 我们将介绍的第一种方法是使用 nbconvert,这是一个命令行工具,可将 Jupyter Notebook 转换为各种格式,包括 PDF。按照以下步骤使用 nbconvert 将 Jupyter Notebook 转换为 PDF。

Step 1: Install nbconvert 第 1 步:安装 nbconvert

If you haven’t installed nbconvert yet, you can install it using pip, which is a package manager for Python. Open your terminal and run the following command: 如果你还没有安装 nbconvert,你可以使用 pip 安装它,pip 是 Python 的包管理器。打开终端并运行以下命令:

pip install nbconvert

Step 2: Convert your Jupyter Notebook to PDF 步骤 2:将 Jupyter Notebook 转换为 PDF

Once you’ve installed nbconvert, you can convert your Jupyter Notebook to PDF. Open your terminal and navigate to the directory where your Jupyter Notebook is located. Then, run the following command: 安装 nbconvert 后,您可以将 Jupyter Notebook 转换为 PDF。打开终端并导航到 Jupyter Notebook 所在的目录。然后,运行以下命令:

jupyter nbconvert --to pdf saturnclou1.ipynb

01.如何在 Jupyter Notebook 中将 ipynb 转换为 PDF,第1张

Replace saturnclou1.ipynb with the name of your Jupyter Notebook file. This command will create a PDF file with the same name as your Jupyter Notebook file. 替换 saturnclou1.ipynb 为 Jupyter Notebook 文件的名称。此命令将创建一个与 Jupyter Notebook 文件同名的 PDF 文件。

附加:可以编写批处理文件,进行批量转换

jupyter nbconvert --to pdf agentchat_agentoptimizer.ipynb
timeout /t 3

jupyter nbconvert --to pdf agentchat_auto_feedback_from_code_execution.ipynb
timeout /t 3

jupyter nbconvert --to pdf agentchat_chess.ipynb
timeout /t 3

Step 3: View your PDF 第 3 步:查看您的 PDF

Once the conversion is complete, you can view your PDF by opening it in your PDF viewer of choice. 转换完成后,您可以通过在所选的 PDF 查看器中打开 PDF 来查看 PDF。


01.如何在 Jupyter Notebook 中将 ipynb 转换为 PDF,第2张

Method 2: Using LaTeX方法2:使用LaTeX

Another method you can use to convert your Jupyter Notebook to PDF is by using LaTeX, a document preparation system that is widely used in academia. Follow the steps below to convert your Jupyter Notebook to PDF using LaTeX. 可用于将 Jupyter Notebook 转换为 PDF 的另一种方法是使用 LaTeX,这是一种在学术界广泛使用的文档准备系统。请按照以下步骤使用 LaTeX 将 Jupyter Notebook 转换为 PDF。

Step 1: Install LaTeX第 1 步:安装 LaTeX

If you haven’t installed LaTeX yet, you can download it from the official website and install it on your computer. 如果你还没有安装LaTeX,你可以从官网下载并安装到你的电脑上。

Step 2: Export your Jupyter Notebook to LaTeX 第 2 步:将 Jupyter Notebook 导出到 LaTeX

In Jupyter Notebook, click on File > Download as > LaTeX (.tex). This will export your Jupyter Notebook to a LaTeX file.


01.如何在 Jupyter Notebook 中将 ipynb 转换为 PDF,第3张

Step 3: Compile your LaTeX file 第 3 步:编译 LaTeX 文件

Once you’ve exported your Jupyter Notebook to LaTeX, you can compile it to create a PDF. Open your terminal and navigate to the directory where your LaTeX file is located. Then, run the following command: 将 Jupyter Notebook 导出到 LaTeX 后,您可以对其进行编译以创建 PDF。打开终端并导航到 LaTeX 文件所在的目录。然后,运行以下命令:

pdflatex saturnclou1.ipynb.tex

01.如何在 Jupyter Notebook 中将 ipynb 转换为 PDF,第4张

Replace saturnclou1.tex with the name of your LaTeX file. This command will create a PDF file with the same name as your LaTeX file. 替换 saturnclou1.tex 为您的 LaTeX 文件的名称。此命令将创建一个与 LaTeX 文件同名的 PDF 文件。

Step 4: View your PDF 第 4 步:查看您的 PDF

Once the compilation is complete, you can view your PDF by opening it in your PDF viewer of choice. 编译完成后,您可以通过在所选的 PDF 查看器中打开 PDF 来查看 PDF。


01.如何在 Jupyter Notebook 中将 ipynb 转换为 PDF,第5张

Get fully managed Jupyter, VS Code, Dask clusters, models, dashboards and jobs. 获取完全托管的 Jupyter、VS Code、Dask 群集、模型、仪表板和作业。

Saturn Cloud is one of the top DS/ML/AI platforms on G2. Join today and get 150 hours of free compute every month. Saturn Cloud 是 G2 上顶级的 DS/ML/AI 平台之一。立即加入,每月可获得 150 小时的免费计算。

Conclusion结论

Converting your Jupyter Notebook to PDF format is a useful way to share your work with others who don’t have access to Jupyter Notebook or when you want to create a static snapshot of your notebook. In this tutorial, we covered two methods to convert your Jupyter Notebook to PDF format, using nbconvert and LaTeX. You can choose the method that works best for you depending on your preference and requirements.


https://www.xamrdz.com/backend/3r21926183.html

相关文章: