Extracting a text string info (character's indices) to use it as a material mask
The color of each character is determined by the material index on the text object. Therefore, to have each character use a different color from the color ramp requires a material for each color.

The steps involved:


A material for each color in the color ramp node.
The text object must have all the materials applied to it.
The characters must reference the index of the materials.


This script takes the colors from a color ramp node, makes new materials and then assign each character a material.

import bpy

txt = 'Text' # The name of text object and text curve.
materials = bpy.data.objects[txt].data.materials
i = 0

# This material holds the color ramp node, change 'material name' to the name of your material.
rampMat = 'material name'
colorRamp = bpy.data.materials[rampMat].node_tree.nodes['ColorRamp'].color_ramp.elements

# Convert colors from the ramp to materials and add them to the text object.
for element in colorRamp:
    color = element.color
    material = bpy.data.materials.new('color')
    material.diffuse_color = color[0], color[1], color[2]
    materials.append(material)

# Loop through the characters setting their indexes.
for character_format in bpy.data.curves[txt].body_format:
    character_format.material_index = i % len(materials)
    i += 1