Skip to content

Instantly share code, notes, and snippets.

@benhg
Created May 11, 2018 01:36
Show Gist options
  • Save benhg/3cb45c8e3b1150fb2b3ed1f46f1542be to your computer and use it in GitHub Desktop.
Save benhg/3cb45c8e3b1150fb2b3ed1f46f1542be to your computer and use it in GitHub Desktop.
Get total word count of all markdown cells in all .ipynb files in a given directory
import io
import glob
from IPython.nbformat import current
word_count = 0
directory = "dirpath"
for direc in glob.glob("{}*.ipynb".format(directory)):
with io.open(direc, 'r', encoding='utf-8') as f:
nb = current.read(f, 'json')
for cell in nb.worksheets[0].cells:
if cell.cell_type == "markdown":
word_count += len(cell['source'].replace('#', '').lstrip().split(' '))
print(word_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment