# How to switch texts in Text Editor by script

Cannot find proper bpy.ops command to switch texts in Text Editor(normally you do that from area header by clicking and selecting text block).

bpy.context.space_data.text is read only.

Is there a way?

Edit: the property is writable, my mistake, everything is fine.

You can change it via bpy.types.SpaceTextEditor.text

So if you run an operator from the text editor you can change it in the execute method like this:

def execute(self, context):
context.space_data.text = bpy.data.texts['your_text']


Otherwise you have to find the text area in screen.areas (given there is a text editor in the screen):

for area in context.screen.areas:
if area.type == "TEXT_EDITOR":
break
else:
raise RuntimeError("Nothing found")

area.spaces[0].text = bpy.data.texts['your_text']

• My statement that bpy.context.space_data.text is read-only is false, thanks. It was greyed out in outliner datablocks and that confused me, it makes sense its greyed out. – Jaroslav Jerryno Novotny Dec 3 '14 at 16:46