Convert polygons or ngons to quads using bmesh
Verts of non-boundary edge.

Assuming the white keys are the mesh, where there is an ngon is at the ends of a non boundary edge
Test script below, finds ngons, bisects using YZ (keys aligned along x axis) plane at boundary edge vert coordinates, removes doubles, and repeats until there are no more ngons.
An additional test to make sure the other two connected edges are at an angle of around 90 degrees has been added. (See below)
import bpy
import bmesh

context = bpy.context
ob = context.edit_object
me = ob.data
bm = bmesh.from_edit_mesh(me)


ngons = [f for f in bm.faces if len(f.edges) &gt; 4]

while ngons:
    f = ngons.pop()
    def test(v, edge):
        if len(v.link_edges) == 3:
            e0, e1 = [e for e in v.link_edges if e.is_boundary]
            ev0 = (e0.verts[0].co - e0.verts[1].co)
            ev1 = (e1.verts[1].co - e1.verts[0].co)
            return abs(ev0.dot(ev1)) &lt; 0.1
        return False
    # find verts of non boundary edge
    verts = [v for e in f.edges 
            if not e.is_boundary
            for v in e.verts if test(v, e)]
    print(len(verts))
    for v in verts:
        geom = bmesh.ops.bisect_plane(
                bm,
                geom=[f] + f.edges[:] + f.verts[:],
                plane_co=v.co,
                plane_no=((1, 0, 0)),
                )                
    bmesh.ops.remove_doubles(
        bm,
        verts=bm.verts,
        dist=1e-5
        ) 
    ngons = [f for f in bm.faces if len(f.edges) &gt; 4]          

bmesh.update_edit_mesh(me)