批量转换 .ipynb 为指定格式
import os
path = input("请输入路径:")
for root,dirs,files in os.walk(path):
# for dir in dirs:
# print(os.path.join(root,dir))
for file in files:
# print(os.path.join(root,file))
if file.endswith(".ipynb"):
os.system("jupyter nbconvert --to html " + os.path.join(root,file))
用jupyter notebook写的后缀名是.ipynb的文件如何转换成html,md,pdf等格式呢?本文将做简单介绍。
os.walk() 使用
>>> import os
>>> help(os.walk)
Help on function walk in module os:
walk(top, topdown=True, onerror=None, followlinks=False)
Directory tree generator.
For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), yields a 3-tuple
dirpath, dirnames, filenames
dirpath is a string, the path to the directory. dirnames is a list of
the names of the subdirectories in dirpath (excluding '.' and '..').
filenames is a list of the names of the non-directory files in dirpath.
Note that the names in the lists are just names, with no path components.
To get a full path (which begins with top) to a file or directory in
dirpath, do os.path.join(dirpath, name).
If optional arg 'topdown' is true or not specified, the triple for a
directory is generated before the triples for any of its subdirectories
(directories are generated top down). If topdown is false, the triple
for a directory is generated after the triples for all of its
subdirectories (directories are generated bottom up).
When topdown is true, the caller can modify the dirnames list in-place
(e.g., via del or slice assignment), and walk will only recurse into the
subdirectories whose names remain in dirnames; this can be used to prune the
search, or to impose a specific order of visiting. Modifying dirnames when
topdown is false is ineffective, since the directories in dirnames have
already been generated by the time dirnames itself is generated. No matter
the value of topdown, the list of subdirectories is retrieved before the
tuples for the directory and its subdirectories are generated.
By default errors from the os.scandir() call are ignored. If
optional arg 'onerror' is specified, it should be a function; it
will be called with one argument, an OSError instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename attribute of the exception object.
By default, os.walk does not follow symbolic links to subdirectories on
systems that support them. In order to get this functionality, set the
optional argument 'followlinks' to true.
Caution: if you pass a relative pathname for top, don't change the
current working directory between resumptions of walk. walk never
changes the current directory, and assumes that the client doesn't
either.
Example:
import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print(root, "consumes", end="")
print(sum([getsize(join(root, name)) for name in files]), end="")
print("bytes in", len(files), "non-directory files")
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
单个 ipynb 文件转换
ipynb转为html格式
在Ubuntu命令行输入:
jupyter nbconvert --to html notebook.ipynb
另外,jupyter提供了一些命令,可以对生成的html格式进行配置:
jupyter nbconvert --to html --template full notebook.ipynb
这是默认配置,提供完整的静态html格式,交互性更强。
jupyter nbconvert --to html --template basic notebook.ipynb
简化的html,用于嵌入网页、博客等,这不包括html标题。
ipynb转换为md格式
在Ubuntu命令行输入:
jupyter nbconvert --to md notebook.ipynb
简单的Markdown格式输出,cell单元不受影响,代码cell缩进4个空格。
ipynb转换为tex格式
在Ubuntu命令行输入:
jupyter nbconvert --to letex notebook.ipynb
Letex导出格式,生成后缀名为NOTEBOOK_NAME.tex的文件。jupyter提供的额外模板配置为:
jupyter nbconvert --to letex -template article notebook.ipynb
这是默认配置,Latex文章。
jupyter nbconvert --to letex -template report notebook.ipynb
Latex报告,提供目录和章节。
jupyter nbconvert --to letex -template basic notebook.ipynb
最基本的Latex输出,经常用来自定义配置。
ipython转换为pdf格式
在Ubuntu命令行输入:
jupyter nbconvert --to pdf notebook.ipynb
转换为pdf格式分模板配置与latex配置是一样的。但是直接转换为pdf格式经常会出现下列错误:
该错误提示没有安装xelatex。所以,我们需要提前安装xelatex,方法是安装texLive套装:
sudo apt-get install texlive-full
texlive-full的安装包有点大,约1G多。
简单的转换方法
ipynb转换为html、md、pdf等格式,还有另一种更简单的方法:在jupyter notebook中,选择File->Download as,直接选择需要转换的格式就可以了。需要注意的是,转换为pdf格式之前,同样要保证已经安装了xelatex。