Placing cursor for a given position using PyUNO
Placing cursor for a given position using PyUNO
I am having Libreoffice 7 installed with custom python 3.8.6 in Windows.
I want to use PyUNO to navigate the view cursor to a specific position based on the X, Y coordinates(of instance com.sun.star.awt.Point) in a writer document.
Is there a way by which this can be achieved? I am unable to find any resource or documentation that helps.
Please let me know if any other info or clarifications needed.
Thank you
I want to use PyUNO to navigate the view cursor to a specific position based on the X, Y coordinates(of instance com.sun.star.awt.Point) in a writer document.
Is there a way by which this can be achieved? I am unable to find any resource or documentation that helps.
Please let me know if any other info or clarifications needed.
Thank you
Libreoffice 7.2.6 on windows 10
Re: Placing cursor for a given position using PyUNO
Why/what are you really trying to do? The view cursor isn't positioned like that... unless your co-ordinates were exactly on a line and exactly the position between two letters you couldn't put it there. To get the nearest place you can use the accessible context of the edit window.
You can explore that with MRI. In BASIC which is all I use, you call.
You need to go through the child windows till you find the one which supports XAccessibleText.
You can explore that with MRI. In BASIC which is all I use, you call.
Code: Select all
MRI thiscomponent.currentcontroller.componentwindow.accessiblecontext
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Placing cursor for a given position using PyUNO
If where you wanted to put it was on screen then you could use the Windows API to simulate a mouseclick which would put it to the nearest location in the document to those screen co-ordinates.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Placing cursor for a given position using PyUNO
Or I guess you could keep moving the viewcursor about by line, page etc until getposition returns the nearest match to where you want it to be.
Edit: though I vaguely recollect problems with the getposition not returning the updated position when moved, so that may not work.
Edit: though I vaguely recollect problems with the getposition not returning the updated position when moved, so that may not work.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Placing cursor for a given position using PyUNO
Thanks for the reply. Here the requirement is like in a PDF, we can navigate to any part of the document based on the positional information (x,y coordinates). Similarly, I need to achieve it for the word/writer document. For that, I thought of exploring UNO approach if it can help for a given spatial information.
Libreoffice 7.2.6 on windows 10
Re: Placing cursor for a given position using PyUNO
Do you just mean scroll the window so a certain part of the document is on screen? You can do that via the accessible context of the window's scrollbars. That's different from setting the view cursor position though.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Placing cursor for a given position using PyUNO
No, I need to perform below steps-
1. Place cursor in a certain point(based on awt.Point coordinates) in the document
2. Select respective word or paragraph where the cursor is positioned at and update/replace the text
if 1st step is feasible and can be performed, I am well aware of how the 2nd step can be carried out.
1. Place cursor in a certain point(based on awt.Point coordinates) in the document
2. Select respective word or paragraph where the cursor is positioned at and update/replace the text
if 1st step is feasible and can be performed, I am well aware of how the 2nd step can be carried out.
Libreoffice 7.2.6 on windows 10
Re: Placing cursor for a given position using PyUNO
Well, I've given you a couple of methods to try.
You can do some binary search type operation where you keep moving the viewcursor and see what position it returns till its where you want it. Or keep moving it a line down from the start till it reaches the nearest y coordinate then right a character at a time till you get to the x. (as said, may not work).
Or you can use the accessible context.
You can do some binary search type operation where you keep moving the viewcursor and see what position it returns till its where you want it. Or keep moving it a line down from the start till it reaches the nearest y coordinate then right a character at a time till you get to the x. (as said, may not work).
Or you can use the accessible context.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Placing cursor for a given position using PyUNO
Here's a start for you, written in Basic, getting the line of text at the y position using the accessible context. It works for me because I picked some convenient values for a test document where there is a line of text at that position. It might be a footnote, or a picture or blank page. What you're trying to do is likely a can of worms. Maybe you should convert the document to pdf if that does what you want...?
Code: Select all
Sub Main
dim pt as new com.sun.star.awt.Point
wantedy= 12000
wantedx = 400
'find the edit window's scrollbar and set the position to wantedy
'scrollbar coords are document coords
comp =thiscomponent.currentcontroller.frame.getcomponentwindow
compchild=comp.getAccessibleContext.getAccessibleChild(0) 'may be a different index on your machine
on error resume next
for j = 0 to compchild.getAccessibleContext.getAccessibleChildcount -1
with compchild.getAccessibleContext.getAccessibleChild(j)
if .getAccessibleContext.getAccessibleName = "Vertical scroll bar" then found = true
if found then
.getAccessibleContext.setcurrentvalue wantedy
exit for
end if
end with
next
with compchild.getaccessiblecontext.getaccessiblechild(0).getaccessiblecontext 'again may not be 0 for you
'accessiblecontext coords are pixels/screen coords
'try a couple of values hoping we get the first line
pt.x= 60
pt.y=0
'examine mri to see the text
mri .getaccessibleatpoint(pt)
end with
exit sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Placing cursor for a given position using PyUNO
If you are replacing text, is it not better to Find and Select the text to be replaced (a macro mechanism that is well understood), rather than be navigating to a position onscreen to do so, a position that might change depending on scale of display of he document and resolution of the display monitor?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Re: Placing cursor for a given position using PyUNO
@JeJe, thank you, will try to take your suggestions forward. For your point on converting to PDF, we cant do that as the final document(post processing) should still be in the word/writer format.
Libreoffice 7.2.6 on windows 10