Select first and last edge loop
Contextual Create operator

For the case given, simply finding all the boundary edges and spitting into contextual create operator ngon fills the edge loops

The edges of the returned faces are the original edge loops Use to  fill other than ngon.

import bpy
import bmesh

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

ret = bmesh.ops.contextual_create(bm,
        geom=[e for e in bm.edges if e.is_boundary])


# the seperate edge loops
edge_loops = [f.edges[:] for f in ret["faces"]]           
# if don't like the ngon fill then do something here
# eg remove face 
# grid fill edge loops, 
# poke face for tri fan.             


bmesh.update_edit_mesh(me)


Similarly with the edge loop fill operator

ret = bmesh.ops.edgeloop_fill(bm,
        edges=[e for e in bm.edges if e.is_boundary])