Bake vertex color to texture with python

Hello, I’m trying a find a way to bake existent vertex color/UV of an object to texture file with python code.

can anyone help me? I’m not really good at blender nor python :confused:

I found this:

Ob = context.active_object
bpy.ops.object.mode_set(mode='EDIT')
UVLayer = Ob.data.uv_textures.new(name='MathTextureUV') if 'MathTextureUV' not in Ob.data.uv_textures.keys() else Ob.data.uv_textures['MathTextureUV']
UVLayer.active = UVLayer.active_render = True
bpy.ops.uv.unwrap()
Image = D.images.new('MathTextureImage', self.texture_size, self.texture_size)
for uv in UVLayer.data:
     uv.image = Image
VLayer = Ob.data.vertex_colors.new(name='MathTextureVColor') if 'MathTextureVColor' not in Ob.data.vertex_colors.keys() else Ob.data.vertex_colors['MathTextureVColor']
VLayer.active = VLayer.active_render = True
bpy.ops.object.mode_set(mode='VERTEX_PAINT')
#boring stuff
bpy.ops.object.mode_set(mode='EDIT')
context.scene.render.bake_type = 'VERTEX_COLORS'
bpy.ops.object.bake_image()

here: Baking textures with python

but this not exactly what I need because I already have UV and vertex color in my model — I just need to select objects and get textures in the given folder