From bb2bc60f5c00fb621ea51bd48b9cb89df39f3066 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 25 Jun 2026 14:12:25 +0200 Subject: [PATCH] paperless_to_openwebui.py aktualisiert --- paperless_to_openwebui.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/paperless_to_openwebui.py b/paperless_to_openwebui.py index 1d6af13..893c386 100644 --- a/paperless_to_openwebui.py +++ b/paperless_to_openwebui.py @@ -315,7 +315,9 @@ def clean_ocr_text(text: str): ch for ch in text if ch == "\n" or ch == "\t" or ord(ch) >= 32 ) - text = re.sub(r"\s+", " ", text) + # Nur horizontale Whitespaces zusammenfassen, Zeilenumbrüche erhalten! + text = re.sub(r"[^\S\n]+", " ", text) # ← geändert + text = re.sub(r"\n{3,}", "\n\n", text) # max 2 Leerzeilen if len(text) > MAX_TEXT_LENGTH: log.warning("Truncating text (%s chars)", len(text)) text = text[:MAX_TEXT_LENGTH] @@ -679,12 +681,28 @@ def process_single_doc(doc, i: int, total: int): with db_lock: existing = get_synced_document(doc_id) - text = download_document_text(doc_id) + text = doc.get("content", "") if not text.strip(): log.warning("Skipping empty doc=%s", doc_id) return "skipped" - - text = clean_ocr_text(text) + + text = clean_ocr_text(text) + + # ── OCR Fix ─────────────────────────────────────────────── + if OCR_FIX_ENABLED and needs_ocr_fix(text): + log.info("OCR fix needed for doc=%s '%s'", doc_id, title) + fixed_text = fix_ocr_chunked(text) + if fixed_text and fixed_text != text: + log.info("OCR fix applied for doc=%s (delta: %+d chars)", + doc_id, len(fixed_text) - len(text)) + update_paperless_content(doc_id, fixed_text) + text = fixed_text + else: + log.debug("OCR fix: no changes for doc=%s", doc_id) + else: + log.debug("OCR fix: not needed for doc=%s", doc_id) + # ────────────────────────────────────────────────────────── + chunk_docs = build_chunk_documents(doc, text) content_hash = hashlib.sha256( ("".join(chunk_docs)).encode("utf-8")