def write_gridlines_shapefile(filename, sr):
    try:
        import shapefile
    except Exception as e:
        raise Exception("io.to_shapefile(): error " +
                        "importing shapefile - try pip install pyshp")

    wr = shapefile.Writer(shapeType=shapefile.POLYLINE)
    wr.field("number", "N", 20, 0)
    for i, line in enumerate(sr.get_grid_lines()):
        wr.poly([line])
        wr.record(i)
    wr.save(filename)
Write a polyline shapefile of the grid lines - a lightweight alternative
dataset/ETHPy150Open modflowpy/flopy/flopy/export/shapefile_utils.py/write_gridlines_shapefile