From 950af0efdaf9672a5289e294afef6740f4c9e631 Mon Sep 17 00:00:00 2001 From: yuyx <237899745@qq.com> Date: Wed, 7 Jan 2026 15:03:51 +0800 Subject: [PATCH] Improve KDocs search matching --- services/kdocs_uploader.py | 61 +++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/services/kdocs_uploader.py b/services/kdocs_uploader.py index 346b147..1a98c40 100644 --- a/services/kdocs_uploader.py +++ b/services/kdocs_uploader.py @@ -5,6 +5,7 @@ from __future__ import annotations import base64 import os import queue +import re import threading import time from io import BytesIO @@ -822,17 +823,57 @@ class KDocsUploader: pass 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: self._page.keyboard.press("Control+f") time.sleep(0.3) search_input = None + selectors = [ + "input[placeholder*='查找']", + "input[placeholder*='搜索']", + "input[aria-label*='查找']", + "input[aria-label*='搜索']", + "input[type='search']", + ] try: search_input = self._page.get_by_role("textbox").nth(3) - search_input.fill(name) - except Exception: - try: - search_input = self._page.locator("input[placeholder*='查找']").first + if search_input.is_visible(timeout=800): 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: pass time.sleep(0.2) @@ -843,7 +884,10 @@ class KDocsUploader: try: self._page.get_by_role("button", name="查找").first.click() except Exception: - pass + try: + self._page.keyboard.press("Enter") + except Exception: + pass time.sleep(0.3) def _find_next(self) -> None: @@ -854,7 +898,10 @@ class KDocsUploader: try: self._page.get_by_role("button", name="查找").first.click() except Exception: - pass + try: + self._page.keyboard.press("Enter") + except Exception: + pass time.sleep(0.3) def _close_search(self) -> None: @@ -873,7 +920,7 @@ class KDocsUploader: cell_address = f"{unit_col}{row_num}" cell_value = self._get_cell_value(cell_address) if cell_value: - return cell_value == unit + return self._unit_matches(cell_value, unit) return False def _find_person_with_unit(self, unit: str, name: str, unit_col: str, max_attempts: int = 50) -> int: