Skip to content

Instantly share code, notes, and snippets.

@njanakiev
Created January 15, 2019 13:09
Show Gist options
  • Save njanakiev/f8c819e25dec02f5339dfac71d4520b9 to your computer and use it in GitHub Desktop.
Save njanakiev/f8c819e25dec02f5339dfac71d4520b9 to your computer and use it in GitHub Desktop.
Jupyter Word Count
import nbformat
filepath = 'notebbook.ipynb'
with open(filepath, 'r', encoding='utf-8') as f:
nb = nbformat.read(f, as_version=4)
wc_markdown, wc_heading, wc_code = 0, 0, 0
for cell in nb.cells:
if cell.cell_type == "markdown":
wc_markdown += len(cell['source'].replace('#', '').lstrip().split(' '))
elif cell.cell_type == "heading":
wc_heading += len(cell['source'].replace('#', '').lstrip().split(' '))
elif cell.cell_type == "code":
wc_code += len(cell['source'].replace('#', '').lstrip().split(' '))
print("Words in markdown : {}" .format(wc_markdown))
print("Words in heading : {}" .format(wc_heading))
print("Words in code : {}" .format(wc_code))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment