Jarvis' Blog (总有美丽的风景让人流连) 总有美丽的风景让人流连

Jupyter Notebook转pdf的脚本

2017-10-16
Jarvis
Post

使用环境

  1. Win10 64位系统 (在windows系统上进行了测试)
  2. texlive2015 (这里主要需要的是xelatex工具, 保证电脑可以正常使用xelatex编译tex文档)
  3. Anaconda (主要使用了jupyter的nbconvert工具)

脚本

主程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
import os

def change(name):
    with open(name, "r", encoding="utf-8") as f:
        lines = f.readlines()
    
    with open(name, "w", encoding="utf-8") as f:
        for line in lines:
            if "Colors for the hyperref package" in line:
                line = "\t% ======================= Added by Jarvis =====================\n\t\\usepackage{framed}\n\t\\usepackage{color}\n\t\\definecolor{shadecolor}{rgb}{0.97,0.97,0.97}\n\t% ======================= Added by Jarvis =====================" + line
            if "\\begin{Verbatim}" in line:
                line = "\\begin{shaded}\n" + line
            elif "\\end{Verbatim}" in line:
                line = line + "\\end{shaded}"
            f.write(line)

def main():
    fname = sys.argv[1]
    
    # 把ipynb转为tex
    os.system("jupyter nbconvert --to latex \"{}\"".format(fname))
    name = fname[0:-6]
    os.system("echo Compailing tex to pdf......")
    
    # 为代码块添加背景
    change(name + '.tex')

    # 编译tex文件得到pdf
    os.system("xelatex.exe -synctex=1 -interaction=nonstopmode \"{}\".tex > nul".format(name))

    # 删除多余的中间文件
    os.remove("{}.tex".format(name))
    os.remove("{}.aux".format(name))
    os.remove("{}.log".format(name))
    os.remove("{}.out".format(name))
    os.remove("{}.synctex.gz".format(name))
    os.system("echo Finished!")

解释: main函数中首先使用jupyter nbconvert --to latex xxx.ipynb命令把.ipynb文件转化为.tex文件; 然后再调用xelatexlatex文件进行编译. change函数对nbconvert转换得到的latex中代码环境做了一点修改, 即给代码块添加了统一的背景, 更容易与文档内容区分. 最后把latex生成的多余文件删除(扫尾了).

安装程序 (便于使用)

1
2
3
4
5
6
7
8
9
from setuptools import setup

setup(
    name = 'nb2pdf',
    py_modules = ['nb2pdf'],
    entry_points = {
        'console_scripts':['nb2pdf=nb2pdf:main']
    }
)

简单的python模块安装程序, 就不解释了.

使用方法

安装完毕后在待转换的notebook文档的目录下打开命令行, 执行命令nb2pdf xxx.ipynb后即可得到pdf文件. 注意: 文件名包含空格时要加双引号, 即nb2pdf "xxx.ipynb".


上一篇 Hello Jekyll!

Content