The code below scans an .ipynb file where the first two and fifth cells were run (named example_nb_saved_first_two_cells_run_plus_last.ipynb) and then makes a new notebook out of the executed cells:
import nbformat as nbf
ntbk = nbf.read("example_nb_saved_first_two_cells_run_plus_last.ipynb", nbf.NO_CONVERT)
cells_executed_previously = []
for cell in ntbk.cells:
if cell.execution_count != None:
cells_executed_previously.append(cell)
new_ntbk = ntbk