Apply sentence case in Jupyter Notebook markdown cells

Workaround based on here, which traces back to nbclean’s brilliant use of nbformat as described here.

Code Updated: to fix a typo and add additional request from below.

def cap_letters_after_punc(s):
    '''
    Take a string and capitlize every letter after a end-of-sentence-marking 
    punctuation mark.

    Returns a string with those changes.
    based on https://stackoverflow.com/a/28639714/8508004
    Found https://stackoverflow.com/a/63192453/8508004 after wrote which looks 
    to do this in a different approach using regex.
    '''
    punctuation_marks = [". ","! ","? "]
    for pm in punctuation_marks:
        one_pass_result = ""
        for item in s.split(pm):
            one_pass_result += item[0].upper() + item[1:] + pm
        s = one_pass_result[:-2] # remove extra punctuation added to last item
    return s

def capitalize_line_starts(s):
    '''
    Take a string and capitalizes every letter starting a line.
    '''
    #s = s[0].upper() + s[1:] # Make sure first letter in first line capitalized
    one_pass_result = ""
    #print (s.split("\n"))
    for item in s.split("\n"):
        #print(item)
        if item:
            one_pass_result += item[0].upper() + item[1:] + "\n"
        else:
            one_pass_result += item + "\n"
    s = one_pass_result
    return s

nb_file = "name_of_notebook.ipynb" # REPLACE WITH YOUR NOTEBOOK NAME
import nbformat as nbf
ntbk = nbf.read(nb_file, nbf.NO_CONVERT)
cells_to_keep = []
for cell in ntbk.cells:
    if cell.cell_type == "markdown":
        cell['source'] = cell['source'].replace(" i "," I ") # capitalize `i` alone
        cell['source'] = cap_letters_after_punc(cell['source'])
        cell['source'] = capitalize_line_starts(cell['source'])
    cells_to_keep.append(cell)
new_ntbk = ntbk
new_ntbk.cells = cells_to_keep
nbf.write(new_ntbk, "fixed_"+nb_file, version=nbf.NO_CONVERT)

A notebook to test the code on is available here.