如何将 IPython 笔记本转换为 PDF 和 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15998491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:33:18  来源:igfitidea点击:

How to convert IPython notebooks to PDF and HTML?

pythonjupyter-notebookipython

提问by nunzio13n

I want to convert my ipython-notebooks to print them, or simply send them in html format. I have noticed that there exists a tool to do that already, nbconvert. Although I have downloaded it, I have no idea how to convert the notebook, with nbconvert2.py since nbconvert says that it is deprecated. nbconvert2.py says that I need a profile to convert the notebook, what is it? Does there exist a documentation about this tool?

我想转换我的 ipython-notebooks 来打印它们,或者只是以 html 格式发送它们。我注意到已经有一个工具可以做到这一点,nbconvert。虽然我已经下载了它,但我不知道如何使用 nbconvert2.py 转换笔记本,因为 nbconvert 说它已被弃用。nbconvert2.py 说我需要一个配置文件来转换笔记本,它是什么?是否存在有关此工具的文档?

回答by root

From the docs:

文档

If you want to provide others with a static HTML or PDF view of your notebook, use the Print button. This opens a static view of the document, which you can print to PDF using your operating system's facilities, or save to a file with your web browser's ‘Save' option (note that typically, this will create both an html file and a directory called notebook_name_files next to it that contains all the necessary style information, so if you intend to share this, you must send the directory along with the main html file).

如果要向其他人提供笔记本的静态 HTML 或 PDF 视图,请使用“打印”按钮。这将打开文档的静态视图,您可以使用操作系统的工具将其打印为 PDF,或使用 Web 浏览器的“保存”选项保存到文件中(请注意,这通常会创建一个 html 文件和一个名为旁边的 notebook_name_files 包含所有必要的样式信息,因此如果您打算共享它,则必须将目录与主 html 文件一起发送)。

回答by Matt

nbconvert is not yet fully replaced by nbconvert2, you can still use it if you wish, otherwise we would have removed the executable. It's just a warning that we do not bugfix nbconvert1 anymore.

nbconvert 还没有完全被 nbconvert2 取代,如果你愿意,你仍然可以使用它,否则我们会删除可执行文件。这只是一个警告,我们不再修复 nbconvert1 的错误。

The following should work :

以下应该工作:

./nbconvert.py --format=pdf yourfile.ipynb 

If you are on a IPython recent enough version, do not use print view, just use the the normal print dialog. Graph beeing cut in chrome is a known issue (Chrome does not respect some print css), and works much better with firefox, not all versions still.

如果您使用的 IPython 版本足够新,请不要使用打印视图,只需使用普通的打印对话框即可。chrome 中的 Graph beeing 是一个已知问题(Chrome 不尊重某些打印 css),并且在 Firefox 中效果更好,但并非所有版本都如此。

As for nbconvert2, it still highly dev and docs need to be written.

至于nbconvert2,它仍然是高度开发的,需要编写文档。

Nbvieweruse nbconvert2 so it's pretty decent with HTML.

Nbviewer使用 nbconvert2,所以它与 HTML 相当不错。

List of current available profiles:

当前可用的配置文件列表:

$ ls -l1 profile|cut -d. -f1

base_html
blogger_html
full_html
latex_base
latex_sphinx_base
latex_sphinx_howto
latex_sphinx_manual
markdown
python
reveal
rst

Give you the existing profiles. (You can create your own, cf future doc, ./nbconvert2.py --help-allshould give you some option you can use in your profile.)

为您提供现有的配置文件。(你可以创建你自己的,参见未来的文档,./nbconvert2.py --help-all应该会给你一些你可以在你的个人资料中使用的选项。)

then

然后

$ ./nbconvert2.py [profilename] --no-stdout --write=True <yourfile.ipynb>

And it should write your (tex) files as long as extracted figures in cwd. Yes I know this is not obvious, and it will probably change hence no doc...

只要在 cwd 中提取数字,它就应该编写您的 (tex) 文件。是的,我知道这并不明显,它可能会改变,因此没有文档...

The reason for that is that nbconvert2 will mainly be a python librarywhere in pseudo code you can do :

原因是 nbconvert2 将主要是一个 python,你可以在伪代码中执行以下操作:

 MyConverter = NBConverter(config=config)
 ipynb = read(ipynb_file)
 converted_files = MyConverter.convert(ipynb)
 for file in converted_files :
     write(file)

Entry point will come later, once the API is stabilized.

一旦 API 稳定下来,入口点将在稍后出现。

I'll just point out that @jdfreder (github profile)is working on tex/pdf/sphinx export and is the expert to generate PDF from ipynb file at the time of this writing.

我只想指出@jdfreder(github 配置文件)正在处理 tex/pdf/sphinx 导出,并且在撰写本文时是从 ipynb 文件生成 PDF 的专家。

回答by AnneTheAgile

I can't get pdf to work yet. The docs imply I should be able to get it to work with latex, so maybe my latex is not working. http://ipython.org/ipython-doc/rel-1.0.0/interactive/nbconvert.html$ ipython --version 1.1.0 $ ipython nbconvert --to latex --post PDF myfile.ipynb [NbConvertApp] ... raise child_exception OSError: [Errno 2] No such file or directory $ ipython nbconvert --to pdf myfile.ipynb [NbConvertApp] CRITICAL | Bad config encountered during initialization: [NbConvertApp] CRITICAL | The 'export_format' trait of a NbConvertApp instance must be any of ['custom', 'html', 'latex', 'markdown', 'python', 'rst', 'slides'] or None, but a value of u'pdf' was specified.

我还不能让 pdf 工作。文档暗示我应该能够让它与乳胶一起工作,所以也许我的乳胶不起作用。http://ipython.org/ipython-doc/rel-1.0.0/interactive/nbconvert.html$ ipython --version 1.1.0 $ ipython nbconvert --to latex --post PDF myfile.ipynb [NbConvertApp] ... raise child_exception OSError: [Errno 2] No such file or directory $ ipython nbconvert --to pdf myfile.ipynb [NbConvertApp] CRITICAL | Bad config encountered during initialization: [NbConvertApp] CRITICAL | The 'export_format' trait of a NbConvertApp instance must be any of ['custom', 'html', 'latex', 'markdown', 'python', 'rst', 'slides'] or None, but a value of u'pdf' was specified.

However, HTML works great using 'slides', and it is beautiful! $ ipython nbconvert --to slides myfile.ipynb ... [NbConvertApp] Writing 215220 bytes to myfile.slides.html

然而,HTML 使用“幻灯片”效果很好,而且很漂亮! $ ipython nbconvert --to slides myfile.ipynb ... [NbConvertApp] Writing 215220 bytes to myfile.slides.html

//Update 2014-11-07Fri.: The IPython v3 syntax differs, it is simpler; $ ipython nbconvert --to PDF myfile.ipynb In all cases, it appears that I was missing the library 'pdflatex'. I'm investigating that.

//Update 2014-11-07Fri.: IPython v3语法不同,更简单; $ ipython nbconvert --to PDF myfile.ipynb 在所有情况下,我似乎都缺少“pdflatex”库。我正在调查那个。

回答by karel

If you have LaTeX installed you can download as PDF directly from Jupyter notebook with File-> Download as-> PDF via LaTeX (.pdf). Otherwise follow these two steps.

如果您安装了 LaTeX,您可以使用File-> Download as-> PDF via LaTeX (.pdf)直接从 Jupyter notebook下载PDF。否则按照这两个步骤。

  1. For HTML output, you should now use Jupyter in place of IPython and select File-> Download as-> HTML (.html)or run the following command:

    jupyter nbconvert --to html notebook.ipynb  
    

    This will convert the Jupyter document file notebook.ipynb into the html output format.

    Google Colaboratory is Google's free Jupyter notebook environment that requires no setup and runs entirely in the cloud. If you are using Google Colab the commands are the same, but Google Colab only lets you download .ipynb or .py formats.

  2. Convert the html file notebook.html into a pdf file called notebook.pdf. In Windows, Mac or Linux, install wkhtmltopdf. wkhtmltopdf is a command line utility to convert html to pdf using WebKit. You can download wkhtmltopdf from the linked webpage, or in many Linux distros it can be found in their repositories.

    wkhtmltopdf notebook.html notebook.pdf   
    
  1. 对于 HTML 输出,您现在应该使用 Jupyter 代替 IPython 并选择File-> Download as-> HTML (.html)或运行以下命令:

    jupyter nbconvert --to html notebook.ipynb  
    

    这会将 Jupyter 文档文件 notebook.ipynb 转换为 html 输出格式。

    Google Colaboratory 是 Google 的免费 Jupyter Notebook 环境,无需设置并完全在云端运行。如果您使用的是 Google Colab,命令是相同的,但 Google Colab 只允许您下载 .ipynb 或 .py 格式。

  2. 将 html 文件 notebook.html 转换为名为 notebook.pdf 的 pdf 文件。在 Windows、Mac 或 Linux 中,安装wkhtmltopdf。wkhtmltopdf 是一个使用 WebKit 将 html 转换为 pdf 的命令行实用程序。您可以从链接的网页下载 wkhtmltopdf,或者在许多 Linux 发行版中可以在他们的存储库中找到它。

    wkhtmltopdf notebook.html notebook.pdf   
    


Original (now almost obsolete) revision: Convert the IPython notebook file to html.

原始(现在几乎已过时)修订版:将 IPython 笔记本文件转换为 html。

ipython nbconvert --to html notebook.ipynb

ipython nbconvert --to html notebook.ipynb

回答by Lefteris Xris

If you are using sagemathcloud version, you can simply go to the left corner,
select File → Download as → Pdf via LaTeX (.pdf)
Check the screenshot if you want.
Screenshot Convert ipynb to pdf

If it dosn't work for any reason, you can try another way.
select File → Print Preview and then on the preview
right click → Print and then select save as pdf.

如果您使用的是sagemath云版本,您可以简单地转到左角,
选择文件 → 下载为 → Pdf 通过 LaTeX (.pdf)
如果需要,请检查屏幕截图。
Screenshot Convert ipynb to pdf

如果由于某种原因它不起作用,您可以尝试其他方法。
选择文件→打印预览,然后在预览上
右键单击→打印,然后选择另存为pdf。

回答by semore_1267

I've been searching for a way to save notebooks as html, since whenever I try to download as html with my new Jupyter installation, I always get a 500 : Internal Server Error The error was: nbconvert failed: validate() got an unexpected keyword argument 'relax_add_props'error. Oddly enough, I've found that downloading as html is as simple as:

我一直在寻找一种将笔记本保存为 html 的方法,因为每当我尝试使用新的 Jupyter 安装将笔记本下载为 html 时,我总是遇到500 : Internal Server Error The error was: nbconvert failed: validate() got an unexpected keyword argument 'relax_add_props'错误。奇怪的是,我发现以 html 格式下载非常简单:

  1. Left click in the notebook
  2. Click 'Save As...' in the dropdown menu
  3. Save accordingly
  1. 在笔记本中左键单击
  2. 单击下拉菜单中的“另存为...”
  3. 相应地保存

No print preview, no print, no nbconvert. Using Jupyter Version: 1.0.0. Just a suggestion to try (obviously not all setups are the same).

没有打印预览,没有打印,没有 nbconvert。使用 Jupyter Version: 1.0.0。只是尝试的建议(显然并非所有设置都相同)。

回答by hlkw

I find that the easiest method to convert a notebook which is on the web to pdf is to first view it on the web service nbviewer. You can then print it to a pdf file. If the notebook is on your local drive, then upload it to a github repository first and use its url for nbviewer.

我发现将网络上的笔记本转换为 pdf 的最简单方法是首先在网络服务nbviewer上查看它。然后,您可以将其打印为 pdf 文件。如果笔记本在您的本地驱动器上,则首先将其上传到 github 存储库,并将其 url 用于 nbviewer。

回答by hlkw

  1. Save as HTML;
  2. Ctrl+ P;
  3. Save as PDF.
  1. 另存为HTML;
  2. Ctrl+ P;
  3. 另存为PDF

回答by Saurabh Jain

Other suggested approaches:

其他建议的方法:

  1. Using the 'Print and then select save as pdf.' from your HTML file will result in lossof border edges, highlighting of syntax, trimming of plots etc.

  2. Some other libraries have shown to be broken when it comes to using obsolete versions.

  1. 使用“打印然后选择另存为 pdf”。来自您的 HTML 文件将导致边框边缘丢失、语法突出显示、绘图修剪等。

  2. 当涉及到使用过时的版本时,其他一些库已经被破坏了。

Solution:A better, hassle-free option is to use an online converter https://www.sejda.com/html-to-pdfwhich will convert the *.html version of your *.ipynb to *.pdf.

解决方案:一个更好、更省心的选择是使用在线转换器https://www.sejda.com/html-to-pdf,它将您的 *.ipynb 的 *.html 版本转换为 *.pdf。

Steps:

脚步:

  1. First, from your Jupyter notebook interface convert your *.ipynb to *.html using
  1. 首先,从您的 Jupyter 笔记本界面使用将您的 *.ipynb 转换为 *.html

File > Download as > HTML(.html)

文件 > 下载为 > HTML(.html)

  1. Upload the newly created *.html file to https://www.sejda.com/html-to-pdfand then select the option HTML to PDF.

  2. Your pdf file is now ready for download.

  3. You now have .ipynb, .html and .pdf files

  1. 将新创建的 *.html 文件上传到https://www.sejda.com/html-to-pdf,然后选择 HTML to PDF 选项。

  2. 您的 pdf 文件现在可以下载了。

  3. 您现在拥有 .ipynb、.html 和 .pdf 文件

回答by Yogesh

You can do it by 1st converting the notebook into HTML and then into PDF format:

您可以先将笔记本转换为 HTML,然后再转换为 PDF 格式:

Following steps I have implemented on: OS: Ubuntu, Anaconda-Jupyter notebook, Python 3

我已在以下步骤上实施:操作系统:Ubuntu、Anaconda-Jupyter notebook、Python 3

1 Save Notebook in HTML format:

1 以 HTML 格式保存 Notebook:

  1. Start the jupyter notebook that you want to save in HTML format. First save the notebook properly so that HTML file will have a latest saved version of your code/notebook.
  2. Run the following command from the notebook itself:

    !jupyter nbconvert --to html your_notebook_name.ipynb

  1. 启动要以 HTML 格式保存的 jupyter 笔记本。首先正确保存笔记本,以便 HTML 文件具有您的代码/笔记本的最新保存版本。
  2. 从笔记本本身运行以下命令:

    !jupyter nbconvert --to html your_notebook_name.ipynb

After execution will create HTML version of your notebook and will save it in the current working directory. You will see one html file will be added into the current directory with your_notebook_name.htmlname

执行后将创建笔记本的 HTML 版本并将其保存在当前工作目录中。您将看到一个 html 文件将被添加到当前目录中,your_notebook_name.html名称为

(your_notebook_name.ipynb--> your_notebook_name.html).

( your_notebook_name.ipynb--> your_notebook_name.html)。

2 Save html as PDF:

2 将 html 保存为 PDF:

  1. Now open that your_notebook_name.htmlfile (click on it). It will be opened in a new tab of your browser.
  2. Now go to print option. From here you can save this file in pdf file format.
  1. 现在打开那个your_notebook_name.html文件(点击它)。它将在浏览器的新选项卡中打开。
  2. 现在去打印选项。从这里您可以将此文件保存为 pdf 文件格式。

Note that from print option we also have the flexibility of selecting a portion of a notebook to save in pdf format.

请注意,从打印选项中,我们还可以灵活地选择笔记本的一部分以 pdf 格式保存。