自从在Github上用Jupyter notebook写笔记和教程越用越顺手后,开始对Jupyter有了越来越多的期待。本篇介绍如何将Jupyter文件(.ipynb)转换成下面的格式:

  • html
  • latex
  • markdown
  • slides
  • rst
  • python


$ ipython nbconvert --to FORMAT notebook.ipynb

这里notebook.ipynb 就是待转换格式的jupyter notebook文件,--to FORMAT 代表转换后的格式,可以有以下选择:

  • --to html

    • --template full (default)

      A full static HTML render of the notebook. This looks very similar to the interactive view.

    • --template basic

      Simplified HTML, useful for embedding in webpages, blogs, etc. This excludes HTML headers.

  • --to latex

    Latex export. This generates NOTEBOOK_NAME.tex file, ready for export. You can automatically run latex on it to generate a PDF by adding --post PDF.

    • --template article (default)

      Latex article, derived from Sphinx’s howto template.

    • --template book

      Latex book, derived from Sphinx’s manual template.

    • --template basic

      Very basic latex output - mainly meant as a starting point for custom templates.

  • --to slides

    This generates a Reveal.js HTML slideshow. It must be served by an HTTP server. The easiest way to get this is to add --postserve on the command-line.

  • --to markdown

    Simple markdown output. Markdown cells are unaffected, and code cells are placed in triple-backtick (```) blocks.

  • --to rst

    Basic reStructuredText output. Useful as a starting point for embedding notebooks in Sphinx docs.

  • --to python

    Convert a notebook to an executable Python script. This is the simplest way to get a Python script out of a notebook. If there were any magics in the notebook, this may only be executable from an IPython session.



$ ipython nbconvert --to markdown test.ipynb

参考:Converting notebooks to other formats