It is possible repeat a set of commands for multiple objects?
Using bpy.ops is really not recommended, as it needs context. The context will be wrong.
I suggest this instead:
If you want to create only 1 data block of a material:
import bpy

scene = bpy.context.scene

mat = bpy.data.materials.new(&quot;My Material&quot;)
mat.use_nodes = True
nodes = mat.node_tree.nodes
node_attribute = nodes.new(type=&quot;ShaderNodeAttribute&quot;)
node_attribute.location = (-1405.21, -1784.65)
node_attribute.attribute_name = &quot;Col&quot;

for o in scene.objects:
    if o.type =='MESH':
        o.data.materials.append(mat)

If you want to create a new material for each object instead:
import bpy

scene = bpy.context.scene

for idx ,o in enumerate(scene.objects):
    if o.type == 'MESH': 
        mat = bpy.data.materials.new(&quot;My Material_&quot;+str(idx)) #to make your personal index into name (if you want)
        mat.use_nodes = True
        nodes = mat.node_tree.nodes
        node_attribute = nodes.new(type=&quot;ShaderNodeAttribute&quot;)
        node_attribute.location = (-1405.21, -1784.65)
        node_attribute.attribute_name = &quot;Col&quot;
        
        o.data.materials.append(mat)

Note:
If you want work only on selected objects , you have to replace for o in scene.objects:with for o in bpy.context.selected_objects: