Handler-script updates in viewport but not in render
Don't use operators in handlers

For the most part don't use operators in handlers. When rendering for example, the context needed will not be there.  I imagine edit mode toggle means nada to the renderer.
Use API methods and the passed argument scene
In example below, displays a line of bpy.data.texts[&quot;Text&quot;] as the body of text object scene.objects[&quot;Text&quot;]
import bpy

# pass arguments to build a handler
def text_body_handler(obname, textname):
    # the handler method
    def text_body(scene):
        text = bpy.data.texts.get(textname)
        text_ob = scene.objects.get(obname)
        if not text and text_ob:
            print(&quot;Error: check names&quot;)
            return 
        f = scene.frame_current
        lines = len(text.lines)
        if text_ob and f &lt;= lines:
            text_ob.data.body = text.lines[f-1].body
 
    return text_body

bpy.app.handlers.frame_change_post.clear()           
bpy.app.handlers.frame_change_post.append(text_body_handler(&quot;Text&quot;, &quot;Text&quot;))

Textblock bpy.data.texts[&quot;Text&quot;]
I had written him a letter 
which I had, 
for want of better Knowledge, 
sent to where I met him 
down the Lachlan, years ago,
He was shearing when I knew him, 
so I sent the letter to him,
Just ‘on spec’, addressed as follows,
‘Clancy, of The Overflow’.