You need to [override the context][1] for the operator to work. [similar to this answer on baking fluid or smoke][2], you can bake the cloth cache for every object in the file which has a cloth modifier like this:

    import bpy
    
    for scene in bpy.data.scenes:
        for object in scene.objects:
            for modifier in object.modifiers:
                if modifier.type == 'CLOTH':
                    with bpy.context.temp_override(scene=scene,
                                                   active_object=object,
                                                   point_cache=modifier.point_cache):
                        bpy.ops.ptcache.bake(bake=True)


  [1]: https://blender.stackexchange.com/a/248275/599
  [2]: https://blender.stackexchange.com/q/4963/599
  [3]: https://blender.stackexchange.com/a/728/599
  [4]:
http://www.blender.org/documentation/blender_python_api_2_68_release/bpy.ops.html?#overriding-context
  [5]: https://blender.stackexchange.com/a/728/599