How to add text in blender using python
The preferred way to perform the above operation would be to create the data blocks and link them to the scene using the &quot;low-level&quot; API - RNA methods and attributes - instead of operators to achieve better runtimes and avoid incorrect context issues.
The following code replaces the operator call above to achieve the same behavior:
font_curve = bpy.data.curves.new(type=&quot;FONT&quot;, name=&quot;Font Curve&quot;)
font_curve.body = &quot;my text&quot;
font_obj = bpy.data.objects.new(name=&quot;Font Object&quot;, object_data=font_curve)
bpy.context.scene.collection.objects.link(font_obj)

If you're simply looking to reduce the lines of code by combining the creation of the text and specifying the body, the following code works similarly:
bpy.data.curves.new(type=&quot;FONT&quot;, name=&quot;Font Curve&quot;).body = &quot;my text&quot;
font_obj = bpy.data.objects.new(name=&quot;Font Object&quot;, object_data=bpy.data.curves[&quot;Font Curve&quot;])
bpy.context.scene.collection.objects.link(font_obj)