By calling the setString function of shape in python, you can replace the content of a shape. It seems that this function will not trigger document change events.So I wonder if it can be implemented directly through UNO commands.
Supplementary information on operation mode: My cursor is selected in the shape, but it does not focus on the text inside the shape.I tried to focus on the text inside a shape using UNO’s EnterGroup command, but it didn’t seem to work.
hii @gulixiang can you add a short snippet of your code how you are setting the string ?
Thanks,
Darshan
ok,
`def _GetSelected(doc=None):
if not doc:
doc = XSCRIPTCONTEXT.getDocument()
selectionTarget = doc.getCurrentController().Selection
try:
if hasattr(selectionTarget, ‘Count’) and selectionTarget.Count > 0:
selectionTarget = selectionTarget.getByIndex(0)
except Exception as e:
print(f’\GetSelected error: ', e)
return selectionTarget
control = _GetSelected(doc)
control.String=“xxxxx”
`
When you’re working with text inside a shape in LibreOffice Impress and you want to replace that text using a Python script, it’s important to make sure that your actions are triggering the necessary document change events. Simply using the setString
function to update the text might not always register as a change, which could cause issues down the line.
Here’s a more reliable method:
-
First, Access Your Shape: You need to make sure you’re grabbing the shape that’s currently selected. If your cursor is in the shape but not focused on the text itself, you can use a function to get that shape object.
-
Focus on the Text Inside: Use a text cursor to focus on the actual text content within the shape. This is crucial because it lets you precisely target and modify the text.
-
Replace the Text: When you’re ready to replace the text, go ahead and do it with a command that ensures all the text inside the shape is selected and updated.
-
Ensure Changes Are Recognized: Sometimes, the document might not recognize that a change has been made. To avoid this, you can manually trigger the document’s “modified” status. This step helps ensure that your changes are saved and recognized by LibreOffice.
Here’s a script that pulls it all together:
def _GetSelected(doc=None):
if not doc:
doc = XSCRIPTCONTEXT.getDocument()
selectionTarget = doc.getCurrentController().Selection
try:
if hasattr(selectionTarget, 'Count') and selectionTarget.Count > 0:
selectionTarget = selectionTarget.getByIndex(0)
except Exception as e:
print(f'GetSelected error: ', e)
return selectionTarget
def replace_shape_text(doc, new_text):
shape = _GetSelected(doc)
if shape is not None and hasattr(shape, 'Text'):
text = shape.Text
cursor = text.createTextCursor()
cursor.gotoStart(False)
cursor.gotoEnd(True)
text.setString(new_text)
doc.DocumentInfo.Modified = True
doc = XSCRIPTCONTEXT.getDocument()
replace_shape_text(doc, "New content inside the shape")
This method gives you control over the text inside the shape and ensures that the changes you make are properly recognized by the document. By using this approach, you avoid potential pitfalls where changes might not be saved or noticed by LibreOffice, making your script more robust and reliable.
Great, you can replace the text content of the shape. This is the line where the error occurs doc.DocumentInfo.Modified = True
I changed it to doc.Modified = True
That’s no problem,Thank you.
Glad it worked, Thank you too