jupyter保存为pdf_如何将Jupyter笔记本转换为PDF

本文介绍了如何将Jupyter Notebook转换成PDF的详细步骤,帮助数据科学家和开发者将交互式笔记保存为静态文档。
摘要由CSDN通过智能技术生成

jupyter保存为pdf

If you’re one of many data scientists looking for a job, you might find yourself working on a data science take-home assignment. Instead of sharing your Jupyter Notebooks, it would be neater if you could convert the notebooks and submit the pdf version. In this blog, I want to share how you can turn Jupyter Notebooks into pdf format in a few lines!

如果您是正在寻找工作的众多数据科学家之一,那么您可能会发现自己从事数据科学实务工作。 如果您可以转换笔记本并提交pdf版本,而不是共享Jupyter笔记本,那就更整洁了。 在此博客中,我想分享如何将Jupyter Notebooks转换成pdf格式的几行!

Image for post
A previous take-home assignment got me to learn how to do this
以前的带回家作业让我学习了如何做

安装nbconvert和LaTeX (Install nbconvert and LaTeX)

nbconvert allows users to convert Notebooks to other formats. You would think after installing nbconvert, and you are good to go…right? If it’s that simple, why would I be writing this post?

nbconvert允许用户将笔记本转换为其他格式。 在安装nbconvert之后,您会认为,您很高兴…对吗? 如果这么简单,我为什么要写这篇文章?

After I installed nbconvert, I received an error saying “500: Internal Server Error.” The reason why you see this error is that you will need to install LaTeX or Pandoc as well. I decided to download LaTeX. The good thing about downloading LaTeX is that it makes your analysis look like a research paper, which is very legit.

安装nbconvert之后,我收到一条错误消息,提示“ 500:Internal Server Error”。 看到此错误的原因是,您还需要安装LaTeXPandoc 。 我决定下载LaTeX。 下载LaTeX的好处是,它使您的分析看起来像是一篇研究论文,非常合法。

从命令行运行nbconvert (Run nbconvert from the Command Line)

After you installed these two packages, you can try to convert your notebook into the format you want using this command line:

安装这两个软件包后,可以尝试使用以下命令行将笔记本转换为所需的格式:

$ jupyter nbconvert --to FORMAT notebook.ipynb

This command line will convert the Jupyter notebook file into the output format given by the FORMAT string. In this blog, we will convert it into pdf, but you can also convert it into HTML, Markdown, etc.

此命令行会将Jupyter笔记本文件转换为FORMAT字符串给定的输出格式。 在此博客中,我们将其转换为pdf,但您也可以将其转换为HTML,Markdown等。

$ jupyter nbconvert --to pdf notebook.ipynb

Depending on the packages you’ve previously installed, you may need to downgrade or upgrade some of them.

根据您先前安装的软件包,您可能需要降级或升级其中一些软件包。

For example, if you see this error message…

例如,如果您看到此错误消息…

spyder 3.3.6 has requirement pyqt5<5.13; python_version >= "3", but you'll have pyqt5 5.13.0 which is incompatible

You can resolve this error by uninstalling pyqt5 and install pyqt5==5.12.0 instead.

您可以通过卸载pyqt5并安装pyqt5 == 5.12.0来解决此错误。

安装nbconvert模板 (Install a nbconvert Template)

After I successfully converted the notebook into a pdf file, I realized that the margin of the file was off! Luckily I double-checked the file before I submitted it!

将笔记本成功转换为pdf文件后,我意识到该文件的边距已关闭! 幸运的是,我在提交文件之前仔细检查了该文件!

Here’s the comparison. By default, we can see that the margin is very off. The default pdf format cut off the side of the page.

这是比较。 默认情况下,我们可以看到边距非常差。 默认的pdf格式从页面的一侧切掉。

Image for post
Tyler Makaro on his Tyler Makaro在他的 Github Github上的照片

To resolve this problem, I found a template that was created by Tyler Makaro. I first install his nb_pdf_template by running a command line: pip install nb_pdf_template. Then I cd into the LaTeX folder and added the two templates that Tyler created: classic.tplx and classicm.tplx. Last but not least, don’t forget to delete the default article.tplx from the LaTeX folder.

为了解决此问题,我找到了由Tyler Makaro创建的模板 。 我首先通过运行命令行来安装他的nb_pdf_templatepip install nb_pdf_template. 然后,我cd到LaTeX的文件夹,并添加了两个模板创建泰勒:classic.tplx和classicm.tplx。 最后但并非最不重要的一点是,不要忘记从LaTeX文件夹中删除默认的article.tplx。

Once this is done, you will be able to get the nicely formatted pdf by running this command line:

完成此操作后,您可以通过运行以下命令行来获取格式正确的pdf:

$ jupyter nbconvert --to pdf notebook.ipynb --template classic

结论 (Conclusion)

Here are all the packages you’ll need:

这是您需要的所有软件包:

  1. Install nbconvert

    安装nbconvert

  2. Install LaTeX or Pandoc

    安装LaTeXPandoc

  3. Install a nbconvert template

    安装一个nbconvert 模板

There you have it! If you find this helpful, please share and follow my blog so you won’t miss out!

你有它! 如果您觉得这有帮助,请分享并关注我的博客,以免错过!

Until next time, happy learning! 👩🏻‍💻

直到下次,学习愉快! ‍💻

翻译自: https://towardsdatascience.com/how-to-convert-jupyter-notebooks-into-pdf-5accaef3758

jupyter保存为pdf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值