Python | Jupyter导出PDF,自定义脚本告别G安装包

2018 年 7 月 17 日 程序人生

点击上方“程序人生”,选择“置顶公众号”

第一时间关注程序猿(媛)身边的故事


作者

上海小胖

如需转载,请联系原作者授权。


这两天一直在做课件,我个人一直不太喜欢PPT这个东西……能不用就不用,我个人特别崇尚极简风。

谁让我们是程序员呢,所以就爱上了Jupyter写课件,讲道理markdown也是个非常不错的写书格式啊。


安装Jupyter其实非常简单,你会python就应该会用jupyter,起码简单的 pip install jupyter, jupyter notebook 要会对伐~

好那接下来就是使用jupyter了,启动jupyter后,使用浏览器访问相应IP:Port就可以使用了。没错,jupyter就是这么一个可以用网站来写python的地方。

但是发讲义给同学们看,ipynb格式的文件肯定不方便啊,别人还没上课呢,哪知道那么多?再者PDF传阅起来也随时随地能打开啊。所以我就想转换成PDF。

但是打开文件,点击下载,发现出现了Error

然后照着这个Error,就去谷歌了,发现说的最多的就是要装一个latex环境,mac下完整的安装包要将近3个G!我就为了一个PDF还不需要这么大一个包吧?所以寻思其他方法。

虽然jupyter对PDF支持的不是那么的友善,但是对于html是非常棒的,只不过html是一个html嘛(这不是废话)……不过我可以利用html转换到pdf上啊。

后来了解到python有一个包叫 pdfkit,专门用来转换pdf文件。那我现在只需要在mac上得到pdfkit的支持就好啦?所以接下来就是尝试的过程了。


1. 安装pdfkit,pip install pdfkit

2. 在这里下载对应系统的安装包http://wkhtmltopdf.org/,这个只有48MB。

3. 最后就是为什么说python是「多膜优秀」的原因了!直接看代码

'''
需要安装pdfkit, pip install pdfkit
自行下载并安装wkhtmltopdf-binary, http://wkhtmltopdf.org/
'''


import sys
import subprocess

import pdfkit

# 获得ipynb文件
inputfile = sys.argv[1].replace(" ", "\ ")

# 截取ipynb前面的名字,并保留一份html临时文件
# 这份文件会在转换过程中需要
# 因为我是利用jupyter对于html的支持,使用pdfkit对html文件进行转换
temp_html = inputfile[0:inputfile.rfind('.')]+'.html'


# 转换ipynb文件为html
# 调用了ipython接口
command = 'ipython nbconvert --to html ' + inputfile

# shell端执行command
subprocess.call(command, shell=True)
print('============success===========')

# 拼接一个pdf名字
output_file = inputfile[0:inputfile.rfind('.')]+'.pdf'


# 大杀器出场,pdfkit直接将html转换成pdf
pdfkit.from_file(temp_html, output_file)

# 删除html临时文件
subprocess.call('rm '+temp_html, shell=True)


操作起来也很简单:

至此,ipynb文件已成功转换成pdf文件,并且颜色、格式全都保留!


好啦,今天的内容就到这了,欢迎留言和大家交流沟通。


*注:所有代码都已上传至https://github.com/MiracleYoung/Tools/


- The End -

「若你有原创文章想与大家分享,欢迎投稿。」

加编辑微信ID,备注#投稿#:

程序 丨 druidlost  

小七 丨 duoshangshuang


上期精彩内容

登录查看更多
7

相关内容

Jupyter Notebook是以网页的形式打开,可以在网页页面中直接编写代码和运行代码,代码的运行结果也会直接在代码块下显示的程序。如在编程过程中需要编写说明文档,可在同一个页面中直接编写,便于作及时的说明和解释。
【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
190+阅读 · 2020年6月29日
一份简明有趣的Python学习教程,42页pdf
专知会员服务
76+阅读 · 2020年6月22日
【实用书】Python技术手册,第三版767页pdf
专知会员服务
229+阅读 · 2020年5月21日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
115+阅读 · 2020年5月10日
【干货书】流畅Python,766页pdf,中英文版
专知会员服务
223+阅读 · 2020年3月22日
用 Python 开发 Excel 宏脚本的神器
私募工场
26+阅读 · 2019年9月8日
免费!Google Colab现已支持英伟达T4 GPU
新智元
5+阅读 · 2019年4月25日
Python3.7中一种懒加载的方式
Python程序员
3+阅读 · 2018年4月27日
Python | 爬爬爬:爬百度云,爬百度贴吧,爬爱奇艺
计算机与网络安全
3+阅读 · 2018年3月30日
如何运用Python建一个聊天机器人?
七月在线实验室
17+阅读 · 2018年1月23日
教你用Python来玩跳一跳
七月在线实验室
6+阅读 · 2018年1月2日
Python NLP入门教程
Python开发者
8+阅读 · 2017年11月19日
Python NLP 入门教程
开源中国
14+阅读 · 2017年10月1日
Arxiv
5+阅读 · 2020年3月26日
A Survey on Deep Learning for Named Entity Recognition
Arxiv
25+阅读 · 2020年3月13日
Arxiv
34+阅读 · 2019年11月7日
Meta-Learning with Implicit Gradients
Arxiv
13+阅读 · 2019年9月10日
Monocular Plan View Networks for Autonomous Driving
Arxiv
6+阅读 · 2019年5月16日
Arxiv
135+阅读 · 2018年10月8日
Arxiv
8+阅读 · 2018年5月1日
VIP会员
相关VIP内容
【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
190+阅读 · 2020年6月29日
一份简明有趣的Python学习教程,42页pdf
专知会员服务
76+阅读 · 2020年6月22日
【实用书】Python技术手册,第三版767页pdf
专知会员服务
229+阅读 · 2020年5月21日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
115+阅读 · 2020年5月10日
【干货书】流畅Python,766页pdf,中英文版
专知会员服务
223+阅读 · 2020年3月22日
相关资讯
用 Python 开发 Excel 宏脚本的神器
私募工场
26+阅读 · 2019年9月8日
免费!Google Colab现已支持英伟达T4 GPU
新智元
5+阅读 · 2019年4月25日
Python3.7中一种懒加载的方式
Python程序员
3+阅读 · 2018年4月27日
Python | 爬爬爬:爬百度云,爬百度贴吧,爬爱奇艺
计算机与网络安全
3+阅读 · 2018年3月30日
如何运用Python建一个聊天机器人?
七月在线实验室
17+阅读 · 2018年1月23日
教你用Python来玩跳一跳
七月在线实验室
6+阅读 · 2018年1月2日
Python NLP入门教程
Python开发者
8+阅读 · 2017年11月19日
Python NLP 入门教程
开源中国
14+阅读 · 2017年10月1日
相关论文
Arxiv
5+阅读 · 2020年3月26日
A Survey on Deep Learning for Named Entity Recognition
Arxiv
25+阅读 · 2020年3月13日
Arxiv
34+阅读 · 2019年11月7日
Meta-Learning with Implicit Gradients
Arxiv
13+阅读 · 2019年9月10日
Monocular Plan View Networks for Autonomous Driving
Arxiv
6+阅读 · 2019年5月16日
Arxiv
135+阅读 · 2018年10月8日
Arxiv
8+阅读 · 2018年5月1日
Top
微信扫码咨询专知VIP会员