如何在Eggplant中获取文本框内高亮文本的坐标?
readtext() Hey there! I get that the clipboard approach didn't work out because of that extra trailing newline—super frustrating. Let's focus on your plan to use readtext() by grabbing the coordinates of the highlighted text. Here's how you can pull this off:
Step 1: Capture the Bounding Box of the Highlighted Text
The key here is to get the exact rectangle enclosing your selected text. The method varies by your automation tool, but here are common approaches for popular libraries:
For PyAutoGUI (Python)
- First, retrieve the text box window's position and size using
pygetwindowto target the right window:import pygetwindow as gw import pyautogui # Replace with your text box window's actual title text_box_window = gw.getWindowsWithTitle("Your Text Box Window Title")[0] win_x, win_y, win_width, win_height = text_box_window.left, text_box_window.top, text_box_window.width, text_box_window.height - Text boxes often have internal padding, so adjust the coordinates to exclude borders (tweak the pixel values based on your UI):
adjusted_x = win_x + 5 adjusted_y = win_y + 5 adjusted_width = win_width - 10 adjusted_height = win_height - 10
For AutoHotkey
- Use
WinGetPosto get the window bounds, then adjust for padding:WinGetPos, winX, winY, winW, winH, Your Text Box Window Title ; Adjust values to match your text box's border size adjX := winX + 3 adjY := winY + 3 adjW := winW - 6 adjH := winH - 6
Step 2: Use readtext() on the Captured Coordinates
Once you have the adjusted bounding box, pass those coordinates to your OCR tool (we'll use Tesseract-based pytesseract as an example):
Python Example with Pytesseract
import pytesseract from PIL import ImageGrab # Define the region to capture (x1, y1, x2, y2) text_region = (adjusted_x, adjusted_y, adjusted_x + adjusted_width, adjusted_y + adjusted_height) screenshot = ImageGrab.grab(bbox=text_region) # Extract text and strip any extra whitespace/newlines extracted_text = pytesseract.image_to_string(screenshot).strip()
The .strip() here takes care of any unintended trailing newlines or spaces, just like you needed with the clipboard method.
Bonus: Quick Fix for the Clipboard Approach
If you ever want to circle back to the clipboard method, you can easily remove that annoying trailing newline when retrieving the text:
Python
import pyperclip clipboard_text = pyperclip.paste().rstrip('\n') # Now compare with original text—no extra newline!
AutoHotkey
; Remove only the last newline character Clipboard := StrReplace(Clipboard, "`n", "",, 1) ; Or trim all trailing whitespace/newlines Clipboard := Trim(Clipboard, " `t`r`n")
This should cover both your current readtext() plan and a quick fix for the original clipboard issue. Let me know if you need help adapting this to your specific toolset!
内容的提问来源于stack exchange,提问作者krishna

