.ipynb 文件 批量轉 HTML/MD


批量轉換 .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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM