Change object animation become cyclic for many objects, use Python I use Python for create many objects then give them animation key frame like this (no problem here):

object.location = (x, y , z)
object.keyframe_insert(data_path='location', frame=start, index=-1)
object.location = (x + changeX, y + changeY, z + changeZ)
object.keyframe_insert(data_path='location', frame=end, index=-1)


How change animation become cyclic? I already know graph editor can change it, but how change this use Python for each object when add their key frames? I try:

bpy.ops.graph.extrapolation_type(type='MAKE_CYCLIC')


but Blender tell 'failed, context is incorrect'
Get fcurve array for object

fc = object.animation_data.action.fcurves


From list find fcurve want repeat (example location X)

locationX = fc.find('location', index=0)


Create modifier type CYCLES and set mode_before and mode_after become REPEAT

modifier = locationX.modifiers.new(type='CYCLES')
modifier.mode_before = 'REPEAT'
modifier.mode_after = 'REPEAT'


One problem, this method cause two repeat loops between original two keyframes, object move 1/2 time and 1/2 distance between keyframes. Need double original distance and time between key frames.