nbconvert

安装

nbconvert的安装主要分三部分,特别是要发挥其全部功能的话。

  • 首先是安装nuconvert的Python包
    conda install nbconvert
    
  • 安装Pandoc,这个不是Python包,是系统层面的软件,在Mac下有现成的安装包可以用,Ubuntu下也可以通过简单的命令实现
    sudo apt-get install pandoc
    
  • 接下来是安装Tex,同样在Mac下有MacTeX,非常大,但是很方便,下载后按照提示一步步装就好了,Ubuntu下似乎要联网安装,考虑到Mac下已经有了,就没去具体在Ubuntu下尝试

将nuconvert用作命令行工具

用起来非常简单,首先在命令行中cd进入到需要转换的.ipynb文件目录内,再运行一下命令:

jupyter nbconvert --to <Format> <notebook.ipynb>
  • 为需要转换成的格式,可选的有html, latex, pdf, slides, markdown, rst, script, notebook
  • 为需要被转换的文件名

一个例子

~/GitHub/TensorFlow-Tutorials下的01_Simple_Linear_Model.ipynb转换成PDF文件。

Last login: Fri Apr 14 09:59:22 on ttys000
bogon:~ iridium$ cd GitHub/TensorFlow-Tutorials/
bogon:TensorFlow-Tutorials iridium$ ls
01_Simple_Linear_Model.ipynb        15_Style_Transfer.ipynb
02_Convolutional_Neural_Network.ipynb    LICENSE
03_PrettyTensor.ipynb            README.md
04_Save_Restore.ipynb            cache.py
05_Ensemble_Learning.ipynb        cifar10.py
06_CIFAR-10.ipynb            convert.py
07_Inception_Model.ipynb        dataset.py
08_Transfer_Learning.ipynb        download.py
09_Video_Data.ipynb            images
11_Adversarial_Examples.ipynb        inception.py
12_Adversarial_Noise_MNIST.ipynb    inception5h.py
13_Visual_Analysis.ipynb        knifey.py
14_DeepDream.ipynb            vgg16.py
bogon:TensorFlow-Tutorials iridium$ jupyter nbconvert --to pdf 01_Simple_Linear_Model.ipynb 
[NbConvertApp] Converting notebook 01_Simple_Linear_Model.ipynb to pdf
[NbConvertApp] Support files will be in 01_Simple_Linear_Model_files/
[NbConvertApp] Making directory 01_Simple_Linear_Model_files
[NbConvertApp] Making directory 01_Simple_Linear_Model_files
[NbConvertApp] Making directory 01_Simple_Linear_Model_files
[NbConvertApp] Making directory 01_Simple_Linear_Model_files
[NbConvertApp] Making directory 01_Simple_Linear_Model_files
[NbConvertApp] Making directory 01_Simple_Linear_Model_files
[NbConvertApp] Making directory 01_Simple_Linear_Model_files
[NbConvertApp] Making directory 01_Simple_Linear_Model_files
[NbConvertApp] Making directory 01_Simple_Linear_Model_files
[NbConvertApp] Writing 62205 bytes to notebook.tex
[NbConvertApp] Building PDF
[NbConvertApp] Running pdflatex 3 times: [u'pdflatex', u'notebook.tex']
[NbConvertApp] Running bibtex 1 time: [u'bibtex', u'notebook']
[NbConvertApp] WARNING | bibtex had problems, most likely because there were no citations
[NbConvertApp] PDF successfully created
[NbConvertApp] Writing 664760 bytes to 01_Simple_Linear_Model.pdf
bogon:TensorFlow-Tutorials iridium$

注意,如果jupyter notebook中某些cell的代码很长的话,换成成PDF后就看不全了,如下图,这种时候可能选择Markdown可能会好些。

代码太长导致PDF格式无法全部显示

同时转化多个文件

jupyter nbconvert --to <Format> <notebook1.ipynb> <notebook2.ipynb>

这里,只写了两个文件,还有其它文件可以按照上述格式把文件名添加在后面。

results matching ""

    No results matching ""