How to apply a material to a selection in Text using Python
The following script will cycle through all text objects and for each word (separated by space) assign a material slot index to the word incrementing the material slot through all available slots resetting to index 0 if there are more words than material slots.
Written &amp; tested on blender 2.79

import bpy

texts = [obj for obj in bpy.data.objects if obj.type == 'FONT']

bpy.ops.mode_select
for txt in texts:
    me = txt.data.body
    mats = txt.material_slots
    ind = 0
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.font.move(type='LINE_BEGIN')
    for word in me.split():
        bpy.ops.font.move_select(type='NEXT_WORD')
        bpy.context.object.active_material_index = ind
        bpy.ops.object.material_slot_assign()
        if ind &lt; len(mats):
            ind += 1
        else:
            ind = 0
        bpy.ops.font.move(type='NEXT_CHARACTER')
    bpy.ops.object.mode_set(mode='OBJECT')