ipynb to pdf
Q: 如何把jupyter notebook 转为 pdf 文档?
A: 尝试了几种python包, 结果都没有成功. 包括: xhtml2pdf,
查看官方的介绍说用pandoc也是一种方法, 但是觉得安装一个可怕的Latex和pandoc太麻烦了.
还好, 找到了一个开源方法: 用wkhtmltopdf 程序.
用python写一个脚本, 调用wkhtmltopdf, 运行命令行指令, 得以实现. 非常符合我的预期. 简明, 优雅.
wkhtml2pdf 简介
wkhtmltopdf,一个集成好了的exe文件(C++编写),
基本的调用方法是:
"c:\Program Files\bin\wkhtmltopdf.exe" https://github.com/mementum/backtrader/blob/master/docs2/signal_strategy/signal_strategy.rst signal_strategy.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
C:\Documents and Settings\Administrator\duanqs\strategy_study>dir *.pdf
驱动器 C 中的卷是 160GB_XP
卷的序列号是 EC5F-C44B
C:\Documents and Settings\Administrator\duanqs\strategy_study 的目录
2017-04-17 14:47 120,295 signal_strategy.pdf
2017-04-17 13:32 597,111 backtest.pdf
2 个文件 717,406 字节
0 个目录 19,999,031,296 可用字节
可以先在命令行测试一下,有其他的需要, 可以在命令行通过wkhtmltopdf --help查询,
如果是超长页的话,可以用命令:
wkhtmltopdf.exe http://passport.yupsky.com/ac/register e:\yupskyreg.pdf -H --outline
Here:
-H 是显示扩展帮助
--outline 是添加pdf的左侧概要!(缺省设置)
而且可以批量生成哦,中间用空格隔开
python 脚本: (封装了运行wkhtml2pdf.exe 命令行的py脚本)
# code:utf-8
'''
IPython/Jupyter Problems saving notebook as PDF - Stack Overflow
http://stackoverflow.com/questions/29156653/ipython-jupyter-problems-saving-notebook-as-pdf
This Python script has GUI to select with explorer a Ipython Notebook you want to convert to pdf.
The approach with wkhtmltopdf is the only approach I found works well and provides high quality pdfs.
Other approaches described here are problematic, syntax highlighting does not work or graphs are messed up.
You'll need to install wkhtmltopdf: http://wkhtmltopdf.org/downloads.html
and Nbconvert
pip install nbconvert
# OR
conda install nbconvert
'''
# Script adapted from CloudCray
# Original Source: https://gist.github.com/CloudCray/994dd361dece0463f64a
# 2016--06-29
# This will create both an HTML and a PDF file
import subprocess
import os
from Tkinter import Tk
from tkFileDialog import askopenfilename
WKHTMLTOPDF_PATH = "C:/Program Files/wkhtmltopdf/bin/wkhtmltopdf" # or wherever you keep it
def export_to_html(filename):
cmd = 'ipython nbconvert --to html "{0}"'
subprocess.call(cmd.format(filename), shell=True)
return filename.replace(".ipynb", ".html")
def convert_to_pdf(filename):
cmd = '"{0}" "{1}" "{2}"'.format(WKHTMLTOPDF_PATH, filename, filename.replace(".html", ".pdf"))
subprocess.call(cmd, shell=True)
return filename.replace(".html", ".pdf")
def export_to_pdf(filename):
fn = export_to_html(filename)
return convert_to_pdf(fn)
def main():
print("Export IPython notebook to PDF")
print(" Please select a notebook:")
Tk().withdraw() # Starts in folder from which it is started, keep the root window from appearing
x = askopenfilename() # show an "Open" dialog box and return the path to the selected file
x = str(x.split("/")[-1])
print(x)
if not x:
print("No notebook selected.")
return 0
else:
fn = export_to_pdf(x)
print("File exported as:\n\t{0}".format(fn))
return 1
main()
这里也记录一下尝试xhtml2pdf的经过.
安装完了以后, 编写脚本, 运行时主要是: 卡在了html5lib这个包里:
异常是:
inputstream
CSS parser
等等.
搞定不了, 所以放弃之.
install xhtml2pdf and update html5lib from old vertion to new version (1.0b8)
Here is the logging:
C:\Documents and Settings\Administrator>pip install xhtml2pdf
Collecting xhtml2pdf
Downloading xhtml2pdf-0.0.6.zip (120kB)
100% |████████████████████████████████| 122kB 467kB/s
Collecting html5lib (from xhtml2pdf)
Using cached html5lib-0.999999999-py2.py3-none-any.whl
Collecting pyPdf2 (from xhtml2pdf)
Downloading PyPDF2-1.26.0.tar.gz (77kB)
100% |████████████████████████████████| 81kB 10kB/s
Requirement already satisfied: Pillow in d:\anaconda2\lib\site-packages (from xhtml2pdf)
Collecting reportlab>=2.2 (from xhtml2pdf)
Downloading reportlab-3.4.0-cp27-cp27m-win32.whl (2.1MB)
100% |████████████████████████████████| 2.1MB 261kB/s
Collecting webencodings (from html5lib->xhtml2pdf)
Downloading webencodings-0.5.1-py2.py3-none-any.whl
Requirement already satisfied: setuptools>=18.5 in d:\anaconda2\lib\site-packages (from html5lib->xhtml2pdf)
Requirement already satisfied: six in d:\anaconda2\lib\site-packages (from html5lib->xhtml2pdf)
Requirement already satisfied: pip>=1.4.1 in d:\anaconda2\lib\site-packages (from reportlab>=2.2->xhtml2pdf)
Requirement already satisfied: packaging>=16.8 in d:\anaconda2\lib\site-packages (from setuptools>=18.5->html5lib->xhtml
2pdf)
Requirement already satisfied: appdirs>=1.4.0 in d:\anaconda2\lib\site-packages (from setuptools>=18.5->html5lib->xhtml2
pdf)
Requirement already satisfied: pyparsing in d:\anaconda2\lib\site-packages (from packaging>=16.8->setuptools>=18.5->html
5lib->xhtml2pdf)
Building wheels for collected packages: xhtml2pdf, pyPdf2
Running setup.py bdist_wheel for xhtml2pdf ... done
Stored in directory: C:\Documents and Settings\Administrator\Local Settings\Application Data\pip\Cache\wheels\ec\eb\db
\13a2be9c15f492c65086709a69042924ebfb7aa4c4cc7284f1
Running setup.py bdist_wheel for pyPdf2 ... done
Stored in directory: C:\Documents and Settings\Administrator\Local Settings\Application Data\pip\Cache\wheels\86\6a\6a
\1ce004a5996894d33d93e1fb1b67c30973dc945cc5875a1dd0
Successfully built xhtml2pdf pyPdf2
Installing collected packages: webencodings, html5lib, pyPdf2, reportlab, xhtml2pdf
Successfully installed html5lib-0.999999999 pyPdf2-1.26.0 reportlab-3.4.0 webencodings-0.5.1 xhtml2pdf-0.0.6
C:\Documents and Settings\Administrator>pip install html5lib==1.0b8
Collecting html5lib==1.0b8
Downloading html5lib-1.0b8.tar.gz (889kB)
100% |████████████████████████████████| 890kB 311kB/s
Requirement already satisfied: six in d:\anaconda2\lib\site-packages (from html5lib==1.0b8)
Building wheels for collected packages: html5lib
Running setup.py bdist_wheel for html5lib ... done
Stored in directory: C:\Documents and Settings\Administrator\Local Settings\Application Data\pip\Cache\wheels\d4\d1\0b
\a6b6f9f204af55c9bb8c97eae2a78b690b7150a7b850bb9403
Successfully built html5lib
Installing collected packages: html5lib
Found existing installation: html5lib 0.999999999
Uninstalling html5lib-0.999999999:
Successfully uninstalled html5lib-0.999999999
Successfully installed html5lib-1.0b8
C:\Documents and Settings\Administrator>
ipynb to pdf的更多相关文章
- Windows7下Jupyter Notebook使用入门
目录 一.Jupyter简介 二.Jupyter安装 2.1 python 3安装 2.2 Jupyter 安装 三.Jupyter使用示例 四.Jupyter常用命令 五.其他说明 一.Jupyte ...
- 简单python脚本,将jupter notebook的ipynb文件转为pdf(包含中文)
直接执行的python代码ipynb2pdf.py 主要思路.将ipynb文件转成tex文件,然后使用latex编译成pdf.由于latex默认转换不显示中文,需要向tex文件中添加相关中文包. 依赖 ...
- windows jupyter lab中.ipynb转中文PDF
在jupyter lab中,File-Export Notebook as-Export Notebook to PDF,可以导出成PDF格式的文档,但在操作前需要安装些程序.1. 安装pandocA ...
- Jupyter Notebook PDF输出的中文支持
Jupyter Notebook是什么 Jupyter Notebook是ipython Notebook 的升级.Jupyter能够将实时代码,公式,可视化图表以Cell的方式组织在一起,形成一个对 ...
- Jupyter Notebook通过latex输出pdf
主要步骤 1.将ipynb编译成tex ipython nbconvert --to latex Example.ipynb 2. 修改tex,增加中文支持 在\documentclass{artic ...
- 是程序员,就用python导出pdf
这两天一直在做课件,我个人一直不太喜欢PPT这个东西--能不用就不用,我个人特别崇尚极简风. 谁让我们是程序员呢,所以就爱上了Jupyter写课件,讲道理markdown也是个非常不错的写书格式啊. ...
- Python学习笔记——jupyter notebook 入门和中文pdf输出方案
简单粗暴的安装 对于懒人而言,我还是喜欢直接安装python的集成开发环境 anaconda 多个内核控制 jupyter官网 1). 同时支持python2 和python 3 conda crea ...
- 【原创】JavaFx程序解决Jupyter Notebook导出PDF不显示中文
0.ATTENTION!!! JavaFx里是通过Java调用控制台执行的的jupyter和xelatex指令, 这些个指令需要在本地安装Jupyter和MikTeX之后才能正常在电脑上运行 1.[问 ...
- C#给PDF文档添加文本和图片页眉
页眉常用于显示文档的附加信息,我们可以在页眉中插入文本或者图形,例如,页码.日期.公司徽标.文档标题.文件名或作者名等等.那么我们如何以编程的方式添加页眉呢?今天,这篇文章向大家分享如何使用了免费组件 ...
随机推荐
- YFCC 100M数据集分析笔记
--从YFCC 100M数据集中筛选出Geo信息位于中国的数据集 1.YFCC 100M简介 YFCC 100M数据库是2014年来基于雅虎Flickr的影像数据库.该库由1亿条产生于2004年至20 ...
- 累计进度条 PSP 饼图
累计进度条 PSP 饼图 每周例行报告 本周PSP 类别 任务 开始时间 结束时间 被打断时间 总计工作时间 2016年9月24日 读书 构建之法-6.7章 19:00 20:00 2 58m ...
- SVM (support vector machine)
简单原理流程转自:http://wenku.baidu.com/link?url=57aywD0Q6WTnl7XKbIHuEwWENnSuPS32QO8X0a0gHpOOzdnNt_K0mK2cucV ...
- windows版本 rac 报错信息
原因 - 安装程序无法在一个或多个节点上执行指定的脚本.这可能是由于在节点上执行脚本时出现异常错误. 操作 - 有关详细信息, 请查看日志文件 'C:\Users\ADMINI~1\AppData\L ...
- [转帖] 知乎: 为什么品牌机器里面的VTX都是关闭的..
为何品牌机BIOS中的硬件虚拟化都是默认关闭的? 知乎老狼原创: https://www.zhihu.com/question/40381254/answer/499617881 谢邀.先说结论, ...
- java 数据结构与算法---树
一.树的概念 除根节点外,其余节点有且只有一个父节点. 1.度 节点的度:每个节点的子节点个数. 树的度:树内各个节点的度的最大值. 树的高度(深度):树中节点的最大层次称为树的深度. 节点路径:一 ...
- python3+selenium3+requests爬取我的博客粉丝的名称
爬取目标 1.本次代码是在python3上运行通过的 selenium3 +firefox59.0.1(最新) BeautifulSoup requests 2.爬取目标网站,我的博客:https:/ ...
- 【刷题】HDU 1853 Cyclic Tour
Problem Description There are N cities in our country, and M one-way roads connecting them. Now Litt ...
- 洛谷 P1199 三国游戏 解题报告
P1199 三国游戏 题目描述 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有\(N\)位武将(\(N\)为 ...
- JAVA代码保护从入门到放弃
java语言开发的产品,需要部署到客户现场服务器.产生了对代码进行保护的需求,开始研究代码加密方式. 经过研究分析后有两种思路,混淆和加密.两者各自适应不同的情况. 由于大量spring注解功能,并且 ...