Align origin to face for large number of objects

I am trying to align the origin of a large number of planes to their faces. I have the script to perform this action once, but I can’t find a way to iterate the action for all of the objects. How can I change the active object in a group of selected objects? Or, is there another way? Maybe by name? The objects are named sequentially.

Here is what I have so far:

bpy.ops.object.editmode_toggle()
bpy.ops.mesh.select_all(action='SELECT')

area_type = 'VIEW_3D'
areas  = [area for area in bpy.context.window.screen.areas if area.type == area_type]
bpy.context.scene.tool_settings.use_transform_data_origin = True

with bpy.context.temp_override(area=areas[0]):
    bpy.ops.transform.create_orientation(use=True, overwrite=True)
    bpy.ops.object.editmode_toggle()
    transform_type = bpy.context.scene.transform_orientation_slots[0].type
    bpy.ops.transform.transform(mode='ALIGN', orient_type='Face', orient_matrix_type=transform_type, mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', use_proportional_connected=False, use_proportional_projected=False, snap=False, snap_elements={'INCREMENT'}, use_snap_project=False, snap_target='ACTIVE', use_snap_self=True, use_snap_edit=True, use_snap_nonedit=True, use_snap_selectable=False)
bpy.ops.transform.delete_orientation()

To set an object active you can do something like this:

To deselect all objects:
bpy.ops.object.select_all(action=‘DESELECT’)

Your target object:
obj = bpy.data.objects[‘Cube.001’]

Set it active:
bpy.context.view_layer.objects.active = obj

Also, select it:
bpy.context.view_layer.objects.active.select_set(True)

And if you are doing a for loop, you can set object mode instead of toggling it (which might be problematic sometimes):

Set mode to object:
bpy.ops.object.mode_set(mode = ‘OBJECT’)

Set mode to edit:
bpy.ops.object.mode_set(mode = ‘EDIT’)

So I do it like this. Get a list of objects. Loop that list. Deselect all current selected, set target object active and selected. Do some stuff. Repeat until all objects are processed. Especially if using ops.

Thank you! I followed your steps, but wound up using the object name.

import bpy

index = 0

for obj in bpy.data.objects:
     
        objectName = f"Leaf.{index}"
        index += 1
        obj = bpy.data.objects[objectName]
        
        bpy.ops.object.select_all(action='DESELECT')
        
        bpy.context.view_layer.objects.active = obj
        
        bpy.context.view_layer.objects.active.select_set(True)
        
        bpy.ops.object.mode_set(mode = 'EDIT')
        bpy.ops.mesh.select_all(action='SELECT')
        
        area_type = 'VIEW_3D'
        areas  = [area for area in bpy.context.window.screen.areas if area.type == area_type]
        bpy.context.scene.tool_settings.use_transform_data_origin = True
        
        with bpy.context.temp_override(area=areas[0]):
            bpy.ops.transform.create_orientation(use=True, overwrite=True)
            bpy.ops.object.editmode_toggle()
            transform_type = bpy.context.scene.transform_orientation_slots[0].type
            bpy.ops.transform.transform(mode='ALIGN', orient_type='Face', orient_matrix_type=transform_type, mirror=False, use_proportional_edit=False, proportional_edit_falloff='SMOOTH', use_proportional_connected=False, use_proportional_projected=False, snap=False, snap_elements={'INCREMENT'}, use_snap_project=False, snap_target='ACTIVE', use_snap_self=True, use_snap_edit=True, use_snap_nonedit=True, use_snap_selectable=False)
        bpy.ops.transform.delete_orientation()
        
        bpy.ops.object.mode_set(mode = 'OBJECT')