Last active
October 14, 2024 14:31
-
-
Save akimach/4c72654b329b0cbe47df160d65eb0580 to your computer and use it in GitHub Desktop.
Convert .ipynb to .docx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
# Convert .ipynb to .docx | |
## Dependency | |
* Jupyter notebook | |
* Pandoc | |
## Usage | |
``` | |
$ python ipynb2docx.py hoge.ipynb | |
``` | |
""" | |
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import sys | |
import os.path | |
import subprocess | |
def jupyter2md(path_ipynb): | |
root, ext = os.path.splitext(path_ipynb) | |
path_md = root + '.md' | |
print(path_ipynb, ' -> ', path_md) | |
subprocess.check_call(['jupyter-nbconvert', path_ipynb, '--to', 'markdown']) | |
return path_md | |
def md2docx(path_md): | |
root, ext = os.path.splitext(path_md) | |
path_docx = root + '.docx' | |
print(path_md, ' -> ', path_docx) | |
subprocess.check_call(['pandoc', path_md, '-t', 'docx', '-o', path_docx]) | |
return path_docx | |
if __name__ == '__main__': | |
args = sys.argv | |
if len(args) != 2: | |
print('{}: no arguments'.format(args[0]), file=sys.stderr) | |
sys.exit(1) | |
path_ipynb = args[1] | |
try: | |
path_md = jupyter2md(path_ipynb) | |
path_docx = md2docx(path_md) | |
except subprocess.CalledProcessError as e: | |
print('{}'.format(e), file=sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment