Improve KDocs search matching

This commit is contained in:
2026-01-07 15:03:51 +08:00
parent 45cbdc51b4
commit 950af0efda

View File

@@ -5,6 +5,7 @@ from __future__ import annotations
import base64 import base64
import os import os
import queue import queue
import re
import threading import threading
import time import time
from io import BytesIO from io import BytesIO
@@ -822,17 +823,57 @@ class KDocsUploader:
pass pass
return "" return ""
def _normalize_text(self, value: str) -> str:
if value is None:
return ""
cleaned = str(value)
cleaned = cleaned.replace("\u00a0", "").replace("\u3000", "")
cleaned = re.sub(r"\s+", "", cleaned)
return cleaned.strip()
def _unit_matches(self, cell_value: str, expected_unit: str) -> bool:
if not cell_value or not expected_unit:
return False
norm_cell = self._normalize_text(cell_value)
norm_expected = self._normalize_text(expected_unit)
if not norm_cell or not norm_expected:
return False
if norm_cell == norm_expected:
return True
if norm_expected in norm_cell or norm_cell in norm_expected:
return True
return False
def _search_person(self, name: str) -> None: def _search_person(self, name: str) -> None:
self._page.keyboard.press("Control+f") self._page.keyboard.press("Control+f")
time.sleep(0.3) time.sleep(0.3)
search_input = None search_input = None
selectors = [
"input[placeholder*='查找']",
"input[placeholder*='搜索']",
"input[aria-label*='查找']",
"input[aria-label*='搜索']",
"input[type='search']",
]
try: try:
search_input = self._page.get_by_role("textbox").nth(3) search_input = self._page.get_by_role("textbox").nth(3)
search_input.fill(name) if search_input.is_visible(timeout=800):
except Exception:
try:
search_input = self._page.locator("input[placeholder*='查找']").first
search_input.fill(name) search_input.fill(name)
except Exception:
search_input = None
if not search_input:
for selector in selectors:
try:
candidate = self._page.locator(selector).first
if candidate.is_visible(timeout=800):
search_input = candidate
search_input.fill(name)
break
except Exception:
continue
if not search_input:
try:
self._page.keyboard.type(name)
except Exception: except Exception:
pass pass
time.sleep(0.2) time.sleep(0.2)
@@ -843,7 +884,10 @@ class KDocsUploader:
try: try:
self._page.get_by_role("button", name="查找").first.click() self._page.get_by_role("button", name="查找").first.click()
except Exception: except Exception:
pass try:
self._page.keyboard.press("Enter")
except Exception:
pass
time.sleep(0.3) time.sleep(0.3)
def _find_next(self) -> None: def _find_next(self) -> None:
@@ -854,7 +898,10 @@ class KDocsUploader:
try: try:
self._page.get_by_role("button", name="查找").first.click() self._page.get_by_role("button", name="查找").first.click()
except Exception: except Exception:
pass try:
self._page.keyboard.press("Enter")
except Exception:
pass
time.sleep(0.3) time.sleep(0.3)
def _close_search(self) -> None: def _close_search(self) -> None:
@@ -873,7 +920,7 @@ class KDocsUploader:
cell_address = f"{unit_col}{row_num}" cell_address = f"{unit_col}{row_num}"
cell_value = self._get_cell_value(cell_address) cell_value = self._get_cell_value(cell_address)
if cell_value: if cell_value:
return cell_value == unit return self._unit_matches(cell_value, unit)
return False return False
def _find_person_with_unit(self, unit: str, name: str, unit_col: str, max_attempts: int = 50) -> int: def _find_person_with_unit(self, unit: str, name: str, unit_col: str, max_attempts: int = 50) -> int: