I encountered an issue during the development process when setting text formatting for an Impress document. When using PyUNO to set multiple properties, such as font and size, for a portion of the text within a text box (not the entire text box), the changes do not take effect immediately after setting them. The changes only become effective after clicking on another control or interface to remove focus from the text box. Moreover, only the last property set appears to take effect, as if the last property overrides all the previous ones.
What I want to achieve is to use Python with PyUNO to set the formatting (such as font and size) for a portion of the text within a text box and have the changes take effect immediately. Is this possible, and how can it be implemented?
Hello @lucian_2024 welcome to forums.
Here is what i find after some R and D hope this helps.
To immediately apply text formatting to a portion of text in LibreOffice Impress using PyUNO, follow these steps:
-
Access the Text Document and Text Range:
import uno
context = uno.getComponentContext()
desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
model = desktop.getCurrentComponent()
if not model.supportsService("com.sun.star.drawing.DrawingDocument"):
raise Exception("The current document is not a Drawing Document")
slide = model.CurrentController.getCurrentPage()
shape = slide.getShapes().getByIndex(0) # Adjust as needed
text = shape.Text
-
Get the Text Range:
start = 0
end = 10 # Adjust as needed
cursor = text.createTextCursor()
cursor.setString(text.getString()[start:end])
cursor.gotoStart(False)
cursor.goRight(end - start, True)
-
Apply Text Formatting:
cursor.setPropertyValue("CharFontName", "Arial")
cursor.setPropertyValue("CharHeight", 12)
cursor.setPropertyValue("CharWeight", uno.getConstantByName("com.sun.star.awt.FontWeight.BOLD"))
cursor.setPropertyValue("CharStyleName", "Default")
-
Refresh the Document:
model.CurrentController.reload()
-
Verify the Changes:
Ensure the formatting is applied correctly. Check if other properties might be overriding your settings.
If above response not solves the issue, Please share a code snippet which is not working, it may help to find the root cause for the issue.
Thanks,
Darshan