In Impress, how do I use Python to insert text content at the cursor position?

Suppose there’s a text box with some content, and my cursor is currently positioned in the middle of this content. How can I use Python to insert a chunk of text at the cursor position? My current code only inserts content at the end of the existing text.
`def insert_text_in_impress(txt):
doc = XSCRIPTCONTEXT.getDocument()
# Get the current controller
controller = doc.getCurrentController()
# Get the current selection
selection = controller.getSelection()
if selection is None or selection.getCount() == 0:
raise Exception(“No selection found.”)

shape = selection.getByIndex(0)
cursor = shape.createTextCursor()
shape.getText().insertString(cursor, txt, True)`

You could try dispatching an UNO command to insert text at the current cursor position. Example in basic:

document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:InsertHardHyphen", "", 0, Array())

I assume you could do the Python equivalent of that.

@vmiklos Your method is correct, but it’s inserting ‘-’. How can I insert custom text instead?

Hmm, Writer has .uno:InsertText, but that doesn’t seem to work in Impress shape text. Something like this?

document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Symbols"
args1(0).Value = "custom text"
dispatcher.executeDispatch(document, ".uno:InsertSymbol", "", 0, args1())

@vmiklos I have tried it, perfect. Thank you