【jupyter实用技能】jupyter notebook 导出pdf

jupyter notebook作业,支持markdown语法,但是直接导出pdf都会报错。

解决方法:pdfkit+wkhtmltopdf+自定义脚本来完成这个工作了。

基本思路是先把jupyter notebook文件转换成支持很好的html,然后用pdfkit把html转成pdf,会在同目录下生产对应的pdf。
 

1、安装pdfkitpdfkit
github: https://github.com/pdfkit/pdfkit.git

pip install pdfkit

2、安装wkhtmltopdf
http://wkhtmltopdf.org/

3、一个python脚本

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
作者:Nico_Zheng
链接:https://www.jianshu.com/p/49a0c9f74d59
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自www.cnblogs.com/hightech/p/12815763.html