Jupyter導出pdf解決辦法

方法一:利用markdown中轉

1)jupyter裏file -> download as -> markdown(.md)
2)利用本地markdown程序導出pdf這裏推薦一個輕量級軟件:Typora

https://www.typora.io/

方法二:html轉成pdf

把jupyter notebook文件轉換成Python支持的html,然後用pdfkit把html轉成pdf,會在同目錄下生產對應的pdf。
1)python有一個包叫pdfkit,專門用來轉換pdf文件

pip install pdfkit

2)下載對應系統的安裝包http://wkhtmltopdf.org/

2)編寫script腳本

import sys
import subprocess
import pdfkit
inputfile = sys.argv[1].replace(" ","\ ")
temp_html = inputfile[0:inputfile.rfind('.')]+'.html'
command = 'ipython nbconvert --to html ' + inputfile
subprocess.call(command,shell=True)
print '============success==========='
output_file =inputfile[0:inputfile.rfind('.')]+'.pdf'
pdfkit.from_file(temp_html,output_file)
subprocess.call('rm '+temp_html,shell=True)

4)使用:在terminal輸入以下代碼

python script.py yourfile.ipynb

參考:
1)https://www.jianshu.com/p/49a0c9f74d59
2)https://kuaibao.qq.com/s/20180717A0QNJK00?refer=cp_1026

發佈了6 篇原創文章 · 獲贊 4 · 訪問量 2143
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章