ipynb轉換爲html,md,pdf等格式

jupyter notebook是一個非常棒的工具,關於jupyter的入門教程請參考我之前寫的兩篇博客:

Jupyter notebook入門教程(上)

Jupyter notebook入門教程(下)

那麼用jupyter notebook寫的後綴名是.ipynb的文件如何轉換成html,md,pdf等格式呢?本文將做簡單介紹。

ipynb轉爲html格式

在Ubuntu命令行輸入:

jupyter nbconvert --to html notebook.ipynb
  • 1

另外,jupyter提供了一些命令,可以對生成的html格式進行配置:

jupyter nbconvert --to html --template full notebook.ipynb
  • 1

這是默認配置,提供完整的靜態html格式,交互性更強。

jupyter nbconvert --to html --template basic notebook.ipynb
  • 1

簡化的html,用於嵌入網頁、博客等,這不包括html標題。

ipynb轉換爲md格式

在Ubuntu命令行輸入:

jupyter nbconvert --to md notebook.ipynb
  • 1

簡單的Markdown格式輸出,cell單元不受影響,代碼cell縮進4個空格。

ipynb轉換爲tex格式

在Ubuntu命令行輸入:

jupyter nbconvert --to letex notebook.ipynb
  • 1

Letex導出格式,生成後綴名爲NOTEBOOK_NAME.tex的文件。jupyter提供的額外模板配置爲:

jupyter nbconvert --to letex -template article notebook.ipynb
  • 1

這是默認配置,Latex文章。

jupyter nbconvert --to letex -template report notebook.ipynb
  • 1

Latex報告,提供目錄和章節。

jupyter nbconvert --to letex -template basic notebook.ipynb
  • 1

最基本的Latex輸出,經常用來自定義配置。

ipython轉換爲pdf格式

在Ubuntu命令行輸入:

jupyter nbconvert --to pdf notebook.ipynb
  • 1

轉換爲pdf格式分模板配置與latex配置是一樣的。但是直接轉換爲pdf格式經常會出現下列錯誤:

這裏寫圖片描述

該錯誤提示沒有安裝xelatex。所以,我們需要提前安裝xelatex,方法是安裝texLive套裝:

sudo apt-get install texlive-full
  • 1

texlive-full的安裝包有點大,約1G多。

簡單的轉換方法

ipynb轉換爲html、md、pdf等格式,還有另一種更簡單的方法:在jupyter notebook中,選擇File->Download as,直接選擇需要轉換的格式就可以了。需要注意的是,轉換爲pdf格式之前,同樣要保證已經安裝了xelatex。

這裏寫圖片描述

參考文獻:

Converting notebooks to other formats

Markdown+Pandoc 最佳寫作拍檔 (mailp.in)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章