-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDFtoPDFocr_2.py
More file actions
1285 lines (1095 loc) · 48.8 KB
/
Copy pathPDFtoPDFocr_2.py
File metadata and controls
1285 lines (1095 loc) · 48.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""PDFtoPDFocr - OCR processing GUI for PDF files.
A PySide6 application that processes PDF files using OCR (text recognition)
and saves the result as a new PDF with the suffix "_ocred.pdf" in the same folder.
Uses pdf2image + pytesseract + pikepdf (no PyMuPDF required).
Missing Tesseract language packs are automatically downloaded from GitHub.
"""
import io
import json
import logging
import os
from pathlib import Path
import shutil
import sys
import tempfile
from typing import List
import uuid
from datetime import datetime, timezone
import requests
# PySide6
from PySide6.QtCore import Qt, QThread, Signal
from PySide6.QtGui import QAction, QColor, QIcon
from PySide6.QtWidgets import (
QApplication,
QComboBox,
QFileDialog,
QHBoxLayout,
QLabel,
QListWidget,
QListWidgetItem,
QMenu,
QMessageBox,
QPushButton,
QVBoxLayout,
QWidget,
)
# OCR / PDF libs
import pytesseract
from PIL import Image, ImageOps
from pdf2image import convert_from_path
import pikepdf
# Utilities
ICON_OK, ICON_ERR, ICON_BROOM, ICON_TRASH = "✓", "⚠", "🧹", "🗑"
IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".tif", ".tiff"}
SUPPORTED_EXTS = {".pdf"} | IMAGE_EXTS
APP_NAME = "PDFtoPDFocr"
APP_VERSION = "1.1.3"
EXPORT_SCHEMA = "pdftopdfocr-job-v1"
DEFAULT_OCR_DPI = 300
MERGE_SUBFOLDER_NAME = "Einzeldateien"
def get_project_root() -> Path:
"""Liefert das Root-Verzeichnis des Projekts bzw. das PyInstaller-Bundle-Verzeichnis."""
if getattr(sys, "_MEIPASS", None):
return Path(sys._MEIPASS)
return Path(__file__).resolve().parent
def get_app_icon_path() -> Path:
"""Liefert den Pfad zum Standard-App-Icon (bevorzugt assets/icon.png oder PDFtoPDFocr.ico)."""
root = get_project_root()
candidates = [
Path(sys.executable).with_name("PDFtoPDFocr.ico") if getattr(sys, "frozen", False) else None,
Path(sys.executable).with_name("icon.png") if getattr(sys, "frozen", False) else None,
root / "assets" / "icon.png",
root / "assets" / "app_icon.ico",
root / "assets" / "icon.ico",
root / "PDFtoPDFocr.ico",
root / "PDFtoPDFocr.png",
root / "ICO.ico",
]
for candidate in candidates:
if candidate and candidate.exists():
return candidate
return root / "PDFtoPDFocr.ico"
def get_app_icon() -> QIcon:
"""Erzeugt ein QIcon aus den vorhandenen App-Icon-Pfaden."""
icon_path = get_app_icon_path()
if icon_path.exists():
return QIcon(str(icon_path))
return QIcon()
# ===== i18n (P-006 / Tier-2 Standard: DE, EN, ES, ZH, JA, RU) =====
SUPPORTED_LANGUAGES = ("de", "en", "es", "zh", "ja", "ru")
DEFAULT_LANGUAGE = "de"
_UI_LANGUAGES = SUPPORTED_LANGUAGES
LANGUAGE_NAMES = {
"de": "Deutsch",
"en": "English",
"es": "Español",
"zh": "简体中文",
"ja": "日本語",
"ru": "Русский",
}
def detect_system_language() -> str:
"""Erkennt die Systemsprache (Windows UI Language oder Locale) mit Fallback 'de'."""
try:
if sys.platform.startswith("win"):
import ctypes
lang_id = ctypes.windll.kernel32.GetUserDefaultUILanguage() & 0xFF
lang_map = {
0x07: "de", # German
0x09: "en", # English
0x0A: "es", # Spanish
0x04: "zh", # Chinese
0x11: "ja", # Japanese
0x19: "ru", # Russian
}
if lang_id in lang_map:
return lang_map[lang_id]
return "en"
except Exception:
pass
try:
import locale
loc = (locale.getdefaultlocale()[0] or "").lower()
for code in ("de", "es", "zh", "ja", "ru", "en"):
if loc.startswith(code):
return code
except Exception:
pass
return "de"
def _load_translations() -> dict:
"""Lädt translations.json aus dem Skript- oder Bundle-Verzeichnis."""
try:
base = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__)))
path = os.path.join(base, "translations.json")
with open(path, "r", encoding="utf-8") as f:
import json
return json.load(f)
except Exception:
return {}
_TRANSLATIONS = _load_translations()
_LANG = "de" # Standard: Deutsch; wechselbar via set_language()
def set_language(lang: str) -> None:
"""Setzt die aktive UI-Sprache (z.B. 'de', 'en', 'es', 'zh', 'ja', 'ru')."""
global _LANG
if lang in SUPPORTED_LANGUAGES:
_LANG = lang
def tr(key: str, **kwargs) -> str:
"""Gibt den übersetzten String für key in der aktuellen Sprache zurück.
Falls kein Eintrag für die Zielsprache vorhanden ist, greift die
4-stufige Fallback-Kette: _LANG -> en -> de -> key.
Unterstützt Platzhalter via str.format(**kwargs).
Args:
key: Schlüssel aus translations.json.
**kwargs: Optionale Platzhalter-Werte (z.B. filename="test.pdf").
Returns:
Übersetzter String.
"""
entry = _TRANSLATIONS.get(key, {})
if isinstance(entry, dict):
text = entry.get(_LANG) or entry.get("en") or entry.get("de") or key
else:
text = str(entry) if entry else key
if kwargs:
try:
text = text.format(**kwargs)
except (KeyError, ValueError, IndexError):
pass
return text
def get_language() -> str:
"""Gibt die aktuell aktive UI-Sprache zurück."""
return _LANG
# ===== UI-Sprache: Persistenz (Welle-1 U1) =====
def _ui_config_dir() -> Path:
"""Per-User-Konfigverzeichnis (auch bei read-only Store-Install schreibbar)."""
if sys.platform.startswith("win"):
base = os.environ.get("APPDATA") or os.path.expanduser("~")
else:
base = os.environ.get("XDG_CONFIG_HOME") or os.path.join(
os.path.expanduser("~"), ".config"
)
return Path(base) / "PDFtoPDFocr"
def _ui_config_path() -> Path:
return _ui_config_dir() / "config.json"
def load_ui_language() -> str:
"""Gespeicherte UI-Sprache ('de', 'en', 'es', 'zh', 'ja', 'ru'), Default 'de'."""
try:
with open(_ui_config_path(), "r", encoding="utf-8") as f:
data = json.load(f)
lang = data.get("ui_language", "de") if isinstance(data, dict) else "de"
except (OSError, ValueError):
lang = "de"
return lang if lang in _UI_LANGUAGES else "de"
def save_ui_language(lang: str) -> bool:
"""Persistiert die UI-Sprache in der App-Konfiguration; True bei Erfolg."""
if lang not in _UI_LANGUAGES:
return False
return _update_app_config({"ui_language": lang})
def _update_app_config(updates: dict) -> bool:
"""Liest die App-Konfiguration, mischt `updates` ein und schreibt sie zurueck.
Gemeinsamer Persistenzmechanismus fuer UI-Sprache (U6) und Exportordner (U4):
beide leben in derselben config.json, damit auch unter Store-Sandboxing
(read-only Installationsverzeichnis) nur EIN beschreibbarer Ort noetig ist.
"""
data = {}
try:
with open(_ui_config_path(), "r", encoding="utf-8") as f:
loaded = json.load(f)
if isinstance(loaded, dict):
data = loaded
except (OSError, ValueError):
data = {}
data.update(updates)
try:
_ui_config_dir().mkdir(parents=True, exist_ok=True)
with open(_ui_config_path(), "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
return True
except OSError:
return False
def load_export_folder() -> str | None:
"""Gespeicherter Exportordner (U4), oder None wenn nicht gesetzt."""
try:
with open(_ui_config_path(), "r", encoding="utf-8") as f:
data = json.load(f)
folder = data.get("export_folder") if isinstance(data, dict) else None
except (OSError, ValueError):
folder = None
return folder or None
def save_export_folder(folder: str | None) -> bool:
"""Persistiert den Exportordner (U4); None/"" setzt auf Standard zurueck."""
return _update_app_config({"export_folder": folder or ""})
# Defaults
DEFAULT_TESSDATA_CANDIDATES = [
r"C:\Program Files\Tesseract-OCR\tessdata",
r"C:\Program Files (x86)\Tesseract-OCR\tessdata",
"/usr/share/tesseract-ocr/4.00/tessdata",
"/usr/share/tesseract-ocr/tessdata",
"/usr/local/share/tessdata"
]
def _app_base_dir() -> Path:
"""Returns the script or PyInstaller extraction directory."""
return Path(getattr(sys, "_MEIPASS", Path(__file__).resolve().parent))
def _portable_tesseract_cmd() -> str | None:
"""Finds a bundled Tesseract executable if the portable runtime exists."""
exe_name = "tesseract.exe" if os.name == "nt" else "tesseract"
candidates = [
_app_base_dir() / "tesseract_portable" / exe_name,
_app_base_dir() / exe_name,
]
for candidate in candidates:
if candidate.is_file():
return str(candidate)
return None
def configure_tesseract() -> str | None:
"""Configures pytesseract to use portable Tesseract before PATH fallback."""
env_cmd = os.environ.get("TESSERACT_CMD")
candidates = [
env_cmd if env_cmd and os.path.isfile(env_cmd) else None,
_portable_tesseract_cmd(),
shutil.which("tesseract"),
]
for cmd in candidates:
if cmd:
pytesseract.pytesseract.tesseract_cmd = cmd
return cmd
return None
def get_tessdata_dir() -> str:
"""Locates the Tesseract tessdata directory, or creates a local fallback.
Returns:
Path to the tessdata directory as a string.
"""
env_dir = os.environ.get("TESSDATA_PREFIX")
if env_dir and os.path.isdir(env_dir):
return env_dir
base_dir = _app_base_dir()
bundled_tessdata = base_dir / "tesseract_portable" / "tessdata"
if bundled_tessdata.is_dir():
return str(bundled_tessdata)
local_tessdata = base_dir / "tessdata"
if local_tessdata.is_dir():
return str(local_tessdata)
for c in DEFAULT_TESSDATA_CANDIDATES:
if os.path.isdir(c):
return c
fallback = base_dir / "tessdata"
fallback.mkdir(exist_ok=True)
return str(fallback)
def ensure_tesseract(lang: str) -> bool:
"""Verifies that Tesseract and the requested language pack are available.
Downloads the language traineddata file from tesseract-ocr/tessdata_best if missing.
Args:
lang: Tesseract language code (e.g. "deu", "eng").
Returns:
True if Tesseract and the language pack are ready, False otherwise.
"""
if not configure_tesseract():
QMessageBox.critical(None, tr("error_title"),
tr("error_tesseract_not_found"))
return False
tessdata_dir = get_tessdata_dir()
os.makedirs(tessdata_dir, exist_ok=True)
os.environ["TESSDATA_PREFIX"] = tessdata_dir
target = os.path.join(tessdata_dir, f"{lang}.traineddata")
if not os.path.exists(target) or os.path.getsize(target) == 0:
if os.path.exists(target) and os.path.getsize(target) == 0:
try:
os.unlink(target)
except OSError:
pass
try:
url = f"https://github.com/tesseract-ocr/tessdata_best/raw/main/{lang}.traineddata"
r = requests.get(url, stream=True, timeout=30)
if r.status_code == 200:
# Write to a temp file first; rename atomically so a mid-stream
# network failure never leaves a truncated .traineddata on disk.
tmp_fd, tmp_name = tempfile.mkstemp(
dir=tessdata_dir, suffix=".traineddata.tmp"
)
try:
with os.fdopen(tmp_fd, "wb") as f:
if hasattr(r, "iter_content") and callable(r.iter_content):
for chunk in r.iter_content(chunk_size=65536):
if chunk:
f.write(chunk)
elif hasattr(r, "raw") and r.raw is not None:
shutil.copyfileobj(r.raw, f)
shutil.move(tmp_name, target)
except Exception:
try:
os.unlink(tmp_name)
except OSError:
pass
raise
QMessageBox.information(None, tr("info_download_title"),
tr("info_lang_downloaded", lang=lang))
else:
QMessageBox.critical(None, tr("error_title"),
tr("error_lang_download_failed", lang=lang, status=r.status_code))
return False
except Exception as e:
QMessageBox.critical(None, tr("error_title"),
tr("error_lang_load_failed", lang=lang, error=e))
return False
return True
def _utc_now_iso() -> str:
"""Returns a stable UTC timestamp for export payloads."""
return datetime.now(timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
def _manifest_path(raw_path: str) -> str:
"""Normalizes file paths for JSON manifests while preserving relativity."""
return Path(raw_path).as_posix()
def _export_status(status: str) -> str:
"""Maps internal UI states to the public export schema."""
return {
"done": "success",
"error": "failed",
"pending": "pending",
}.get(status, "pending")
def build_job_export_payload(
file_entries: list[dict],
ocr_language: str,
created_at: str | None = None,
) -> dict:
"""Builds the portable OCR job manifest without embedding PDF content."""
input_files = []
outputs = []
for entry in file_entries:
raw_path = entry["path"]
source_path = Path(raw_path)
output_path = source_path.with_name(f"{source_path.stem}_ocred.pdf")
source_exists = source_path.exists()
output_exists = output_path.exists()
input_files.append(
{
"name": source_path.name,
"local_path": _manifest_path(raw_path),
"size_bytes": source_path.stat().st_size if source_exists else None,
"missing": not source_exists,
}
)
outputs.append(
{
"input_name": source_path.name,
"input_local_path": _manifest_path(raw_path),
"output_name": output_path.name,
"status": _export_status(entry.get("status", "pending")),
"message": entry.get("message", ""),
"output_local_path": output_path.as_posix(),
"output_exists": output_exists,
}
)
return {
"schema": EXPORT_SCHEMA,
"app": APP_NAME,
"app_version": APP_VERSION,
"created_at": created_at or _utc_now_iso(),
"ocr_language": ocr_language,
"input_files": input_files,
"outputs": outputs,
"settings": {
"dpi": DEFAULT_OCR_DPI,
"preserve_original": True,
"download_missing_language_pack": True,
},
}
def write_job_export(target_path: str | Path, payload: dict) -> Path:
"""Writes the OCR job manifest as UTF-8 JSON without BOM."""
export_path = Path(target_path)
export_path.parent.mkdir(parents=True, exist_ok=True)
export_path.write_text(
json.dumps(payload, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
return export_path
# ===== Merge/Stapeln (Welle-1 U2/U3/U4/U5) =====
def resolve_export_folder(
source_path: str | Path, configured_folder: str | Path | None
) -> Path:
"""Resolves the target folder for a merge (U4): configured export folder if it
exists, otherwise the folder of the source file (default per U3).
Args:
source_path: A file whose parent folder serves as the fallback.
configured_folder: User-configured export folder, or None/empty.
Returns:
The resolved export folder path.
"""
if configured_folder:
candidate = Path(configured_folder)
if candidate.is_dir():
return candidate
return Path(source_path).parent
def merge_ocr_outputs(
output_paths: list[str],
merged_name: str,
export_folder: str | Path,
subfolder_name: str = MERGE_SUBFOLDER_NAME,
) -> Path:
"""Merges already-OCRed single-file result PDFs into one collective PDF (U2).
Per U3, the individual page PDFs are moved into `export_folder/subfolder_name`
and the collective PDF is written at the root of `export_folder`.
Args:
output_paths: OCR result PDFs, in the desired page/merge order.
merged_name: File name for the collective PDF.
export_folder: Target folder for the collective PDF (see resolve_export_folder).
subfolder_name: Name of the subfolder that receives the individual pages.
Returns:
Path to the written collective PDF.
Raises:
ValueError: If fewer than 2 output paths are given.
"""
if len(output_paths) < 2:
raise ValueError("merge_ocr_outputs benötigt mindestens 2 Dateien")
export_folder = Path(export_folder)
export_folder.mkdir(parents=True, exist_ok=True)
subfolder = export_folder / subfolder_name
subfolder.mkdir(parents=True, exist_ok=True)
merged = pikepdf.Pdf.new()
opened_sources: list[pikepdf.Pdf] = []
try:
for p in output_paths:
src_pdf = pikepdf.Pdf.open(p)
opened_sources.append(src_pdf)
merged.pages.extend(src_pdf.pages)
merged_path = export_folder / merged_name
merged.save(merged_path)
finally:
for src_pdf in opened_sources:
try:
src_pdf.close()
except Exception:
pass
merged.close()
# Einzelseiten erst NACH dem Speichern der Sammel-PDF verschieben, damit ein
# Fehlschlag beim Merge keine Dateien verwaist zurücklässt.
for p in output_paths:
src_path = Path(p)
if not src_path.exists():
continue
dest = subfolder / src_path.name
if dest.exists():
dest = subfolder / f"{src_path.stem}_{uuid.uuid4().hex[:8]}{src_path.suffix}"
shutil.move(str(src_path), str(dest))
return merged_path
def normalize_image_for_ocr(img: Image.Image) -> Image.Image:
"""Normalisiert ein PIL-Bild für die OCR-Verarbeitung:
1. Korrigiert die EXIF-Orientierung (z.B. bei Smartphone-Fotos / Scans).
2. Behandelt Transparenz / Alpha-Kanäle sauber (Compositing auf weißem Grund
statt Pillow-Standardumwandlung nach Schwarz, wodurch schwarzer Text unlesbar wird).
3. Konvertiert das Bild verlässlich in den RGB-Farbraum.
"""
try:
transposed = ImageOps.exif_transpose(img)
if transposed is not None:
img = transposed
except Exception:
pass
if img.mode in ("RGBA", "LA") or (img.mode == "P" and "transparency" in getattr(img, "info", {})):
try:
img_rgba = img.convert("RGBA")
background = Image.new("RGBA", img_rgba.size, (255, 255, 255, 255))
composite = Image.alpha_composite(background, img_rgba)
return composite.convert("RGB")
except Exception:
return img.convert("RGB")
elif img.mode != "RGB":
return img.convert("RGB")
return img
class OCRWorker(QThread):
"""Führt OCR-Verarbeitung im Hintergrund aus, sodass die GUI responsiv bleibt.
Signals:
file_done(str, bool): Pfad + Erfolgsstatus für jede fertige Datei.
progress(str): Statusmeldung für das Label.
finished_all(): Alle Dateien wurden verarbeitet.
"""
file_done = Signal(str, bool)
progress = Signal(str)
finished_all = Signal()
def __init__(self, pending_paths: list, lang: str, poppler_path: str = "", parent=None):
super().__init__(parent)
self.pending_paths = pending_paths
self.lang = lang
self.poppler_path = poppler_path
def run(self):
for path in self.pending_paths:
self.progress.emit(tr("status_processing", filename=os.path.basename(path)))
success = self._ocr_pdf(path, self.lang)
self.file_done.emit(path, success)
self.finished_all.emit()
def _load_source_images(self, src_path: str) -> List[Image.Image]:
"""Loads the page images for a source file: PDF pages via Poppler, or the
frame(s) of an image file directly (U1 -- multi-page TIFF yields one
image per frame, JPG/PNG yield a single image).
Args:
src_path: Path to a PDF or image (JPG/PNG/TIFF) file.
Returns:
List of PIL images, one per page/frame.
"""
ext = os.path.splitext(src_path)[1].lower()
if ext not in IMAGE_EXTS:
poppler_path = self.poppler_path or None
return convert_from_path(src_path, dpi=300, poppler_path=poppler_path)
images: List[Image.Image] = []
with Image.open(src_path) as im:
frame_count = getattr(im, "n_frames", 1)
for i in range(frame_count):
im.seek(i)
images.append(im.copy())
return images
def _ocr_pdf(self, src_path: str, lang: str) -> bool:
"""Führt OCR auf einer PDF- oder Bilddatei aus (läuft im Worker-Thread)."""
try:
images: List[Image.Image] = self._load_source_images(src_path)
out_pdf = pikepdf.Pdf.new()
# FIX: pikepdf kopiert Seiten LAZY -> die Quell-PDFs (und temp-Dateien)
# muessen bis NACH out_pdf.save() geoeffnet bleiben. Vorher wurde src_pdf
# im Loop VOR dem Speichern geschlossen (und tmp geloescht) -> korrupte/
# fehlende OCR-Seiten moeglich. Daher sammeln, erst im finally schliessen.
page_sources = [] # (pikepdf.Pdf, tmp_path_or_None)
try:
for img in images:
img = normalize_image_for_ocr(img)
pdf_bytes = pytesseract.image_to_pdf_or_hocr(img, lang=lang, extension='pdf')
if not pdf_bytes:
continue
try:
src_pdf = pikepdf.Pdf.open(io.BytesIO(pdf_bytes))
out_pdf.pages.extend(src_pdf.pages)
page_sources.append((src_pdf, None))
except Exception as e:
logging.warning(f"PDF operation failed: {e}")
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
tmp.write(pdf_bytes)
tmp.flush()
tmp.close()
src_pdf = pikepdf.Pdf.open(tmp.name)
out_pdf.pages.extend(src_pdf.pages)
page_sources.append((src_pdf, tmp.name))
if len(out_pdf.pages) == 0:
raise ValueError("OCR produced no pages — all pages yielded empty PDF bytes")
dst_path = os.path.splitext(src_path)[0] + "_ocred.pdf"
out_pdf.save(dst_path)
finally:
# Quell-PDFs + temp-Dateien erst NACH save() schliessen/aufraeumen.
for _src_pdf, _tmp in page_sources:
try:
_src_pdf.close()
except Exception:
pass
if _tmp:
try:
os.unlink(_tmp)
except OSError:
pass
out_pdf.close()
return True
except Exception as e:
# logging statt print: im windowed-PyInstaller ist sys.stdout None ->
# print() wuerde den Worker-Thread crashen (finished_all nie emittiert,
# GUI haengt mit dauerhaft deaktiviertem Start-Button).
logging.error("OCR-Fehler bei %s: %s", src_path, e)
return False
class PDFListWidget(QListWidget):
"""QListWidget with drag-and-drop support for PDF/image files and folders.
Supports two drag-and-drop modes (U2/U5):
- External drop (Explorer): files/folders are added to the list. A whole
folder is tagged with a batch id so it can be auto-merged later (U5).
- Internal drag (reordering rows): row order becomes the merge/page order
used by "Markierte mergen" (U2 -- "Stapel-Reihenfolge = Seitenreihenfolge").
"""
delete_requested = Signal()
folder_dropped = Signal(str, str) # batch_id, folder_path
def __init__(self):
super().__init__()
self.setAcceptDrops(True)
self.setSelectionMode(QListWidget.ExtendedSelection)
self.setDragEnabled(True)
self.setDragDropMode(QListWidget.DragDropMode.InternalMove)
self.setDefaultDropAction(Qt.MoveAction)
self.setContextMenuPolicy(Qt.CustomContextMenu)
def dragEnterEvent(self, e):
if e.source() is self or e.mimeData().hasUrls():
e.acceptProposedAction()
def dragMoveEvent(self, e):
if e.source() is self:
super().dragMoveEvent(e)
else:
e.acceptProposedAction()
def dropEvent(self, e):
if e.source() is self and not e.mimeData().hasUrls():
# Interne Umsortierung: neue Zeilenreihenfolge = Merge-/Stapelreihenfolge.
super().dropEvent(e)
return
for url in e.mimeData().urls():
path = url.toLocalFile()
if os.path.isdir(path):
batch_id = str(uuid.uuid4())
self.add_folder(path, batch_id=batch_id)
self.folder_dropped.emit(batch_id, path)
elif os.path.isfile(path):
self.add_file(path)
e.acceptProposedAction()
def keyPressEvent(self, event):
"""Supports keyboard removal for selected files (Del and Backspace keys) without changing the compact UI."""
if event.key() in (Qt.Key_Delete, Qt.Key_Backspace) and self.selectedItems():
self.delete_requested.emit()
event.accept()
return
super().keyPressEvent(event)
def add_folder(self, folder, batch_id=None):
"""Adds all supported files from a folder to the list.
Args:
folder: Path to the directory to scan.
batch_id: If set, tags all added items as belonging to this
folder-drop batch (U5 automatic merge).
"""
for fname in sorted(os.listdir(folder)):
full = os.path.join(folder, fname)
if os.path.isfile(full):
self.add_file(full, batch_id=batch_id)
def add_file(self, filepath, batch_id=None):
"""Adds a single file to the list if it has a supported extension and is not already listed.
Args:
filepath: Absolute path to the file to add.
batch_id: If set, tags the item as belonging to a folder-drop batch (U5).
"""
if os.path.splitext(filepath)[1].lower() not in SUPPORTED_EXTS:
return
for idx in range(self.count()):
if self.item(idx).data(Qt.UserRole) == filepath:
return
item = QListWidgetItem(os.path.basename(filepath))
item.setData(Qt.UserRole, filepath)
item.setData(Qt.UserRole + 1, 'pending')
item.setData(Qt.UserRole + 2, "")
item.setData(Qt.UserRole + 3, batch_id)
item.setToolTip(filepath)
self.addItem(item)
class OCRConverterGUI(QWidget):
"""Main application window for batch OCR processing of PDF files."""
def __init__(self):
super().__init__()
# UI-Sprache aus persistenter Konfiguration laden, bevor Widgets gebaut werden.
set_language(load_ui_language())
self.setWindowTitle(tr("window_title"))
self.resize(640, 520)
app_icon = get_app_icon()
if not app_icon.isNull():
self.setWindowIcon(app_icon)
self.layout = QVBoxLayout(self)
self._ocr_worker = None # Referenz auf laufenden QThread
# UI-Sprachumschaltung (6 Sprachen gemäß P-006 / Tier-2-Mehrsprachigkeit)
ui_lang_layout = QHBoxLayout()
self.ui_lang_label = QLabel(tr("label_ui_lang"))
self.ui_lang_combo = QComboBox()
for code in _UI_LANGUAGES:
self.ui_lang_combo.addItem(LANGUAGE_NAMES.get(code, code), code)
cur_lang = get_language()
cur_idx = _UI_LANGUAGES.index(cur_lang) if cur_lang in _UI_LANGUAGES else 0
self.ui_lang_combo.setCurrentIndex(cur_idx)
self.ui_lang_combo.currentIndexChanged.connect(self._on_ui_language_changed)
ui_lang_layout.addWidget(self.ui_lang_label)
ui_lang_layout.addWidget(self.ui_lang_combo)
ui_lang_layout.addStretch(1)
self.layout.addLayout(ui_lang_layout)
self.list_widget = PDFListWidget()
self.layout.addWidget(self.list_widget)
# Exportordner-Einstellung (U4): konfigurierbar, Fallback = Quellordner (U3-Default)
self.export_folder: str | None = load_export_folder()
self._batch_folders: dict[str, str] = {}
self._merged_batches: set[str] = set()
export_layout = QHBoxLayout()
self.export_folder_label = QLabel("")
self.btn_choose_export_folder = QPushButton(tr("btn_export_folder"))
self.btn_choose_export_folder.setShortcut("Ctrl+Shift+O")
self.btn_reset_export_folder = QPushButton(tr("btn_export_folder_reset"))
export_layout.addWidget(self.export_folder_label, 1)
export_layout.addWidget(self.btn_choose_export_folder)
export_layout.addWidget(self.btn_reset_export_folder)
self.layout.addLayout(export_layout)
# OCR-Spracheinstellung (Tesseract-Sprachpaket -- NICHT die UI-Sprache)
lang_layout = QHBoxLayout()
self.ocr_lang_label = QLabel(tr("label_ocr_lang"))
self.lang_combo = QComboBox()
self.lang_combo.addItems(["deu", "eng", "fra", "spa"])
lang_layout.addWidget(self.ocr_lang_label)
lang_layout.addWidget(self.lang_combo)
self.layout.addLayout(lang_layout)
# Buttons
btn_layout = QHBoxLayout()
self.btn_add_file = QPushButton(tr("btn_add_file"))
self.btn_add_file.setShortcut("Ctrl+O")
self.btn_start = QPushButton(tr("btn_start"))
self.btn_start.setShortcut("Ctrl+Return")
self.btn_export = QPushButton(tr("btn_export_job"))
self.btn_export.setShortcut("Ctrl+E")
self.btn_refresh = QPushButton(f"{ICON_BROOM} {tr('btn_refresh')}")
self.btn_refresh.setShortcut("F5")
self.btn_delete = QPushButton(f"{ICON_TRASH} {tr('btn_delete')}")
for b in (
self.btn_add_file,
self.btn_start,
self.btn_export,
self.btn_refresh,
self.btn_delete,
):
btn_layout.addWidget(b)
self.layout.addLayout(btn_layout)
self.status_label = QLabel("")
self.status_label.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.status_label)
# Events
self.btn_add_file.clicked.connect(self.open_file_dialog)
self.btn_start.clicked.connect(self.on_start)
self.btn_export.clicked.connect(self.export_job_manifest)
self.btn_refresh.clicked.connect(self.on_refresh)
self.btn_delete.clicked.connect(self.on_delete)
self.list_widget.delete_requested.connect(self.on_delete)
self.btn_choose_export_folder.clicked.connect(self.on_choose_export_folder)
self.btn_reset_export_folder.clicked.connect(self.on_reset_export_folder)
self.list_widget.customContextMenuRequested.connect(self._show_list_context_menu)
self.list_widget.folder_dropped.connect(self._register_batch_folder)
# Retranslate initialisieren (setzt alle Texte, A11y-Attribute und Tooltips)
self.retranslate_ui()
# Poppler-Pfad: leer = pdf2image nutzt System-Poppler
self.poppler_path = ""
def _on_ui_language_changed(self, index: int):
"""Wechselt die UI-Sprache, persistiert sie und stellt die Oberflaeche live um."""
lang = self.ui_lang_combo.itemData(index) or "de"
if lang in _UI_LANGUAGES:
set_language(lang)
save_ui_language(lang)
self.retranslate_ui()
def retranslate_ui(self):
"""Setzt alle sichtbaren, uebersetzten Texte, Barrierefreiheits-Metadaten und Tooltips in der aktuellen Sprache neu."""
self.setWindowTitle(tr("window_title"))
self.ui_lang_label.setText(tr("label_ui_lang"))
self.ui_lang_combo.setAccessibleName(tr("label_ui_lang"))
self.ui_lang_combo.setAccessibleDescription(tr("a11y_ui_lang_description"))
self.ui_lang_combo.setToolTip(tr("tooltip_ui_lang"))
self.ocr_lang_label.setText(tr("label_ocr_lang"))
self.lang_combo.setAccessibleName(tr("label_ocr_lang"))
self.lang_combo.setAccessibleDescription(tr("a11y_ocr_lang_description"))
self.lang_combo.setToolTip(tr("tooltip_ocr_lang"))
self.list_widget.setAccessibleName(tr("a11y_file_list_name"))
self.list_widget.setAccessibleDescription(tr("a11y_file_list_description"))
self.list_widget.setToolTip(tr("a11y_file_list_description"))
self.btn_add_file.setText(tr("btn_add_file"))
self.btn_add_file.setAccessibleName(tr("btn_add_file"))
self.btn_add_file.setAccessibleDescription(tr("a11y_add_file_description"))
self.btn_add_file.setToolTip(tr("tooltip_add_file"))
self.btn_start.setText(tr("btn_start"))
self.btn_start.setAccessibleName(tr("btn_start"))
self.btn_start.setAccessibleDescription(tr("a11y_start_description"))
self.btn_start.setToolTip(tr("tooltip_start"))
self.btn_export.setText(tr("btn_export_job"))
self.btn_export.setAccessibleName(tr("btn_export_job"))
self.btn_export.setAccessibleDescription(tr("a11y_export_job_description"))
self.btn_export.setToolTip(tr("tooltip_export_job"))
self.btn_refresh.setText(f"{ICON_BROOM} {tr('btn_refresh')}")
self.btn_refresh.setAccessibleName(tr("btn_refresh"))
self.btn_refresh.setAccessibleDescription(tr("a11y_refresh_description"))
self.btn_refresh.setToolTip(tr("tooltip_refresh"))
self.btn_delete.setText(f"{ICON_TRASH} {tr('btn_delete')}")
self.btn_delete.setAccessibleName(tr("btn_delete"))
self.btn_delete.setAccessibleDescription(tr("a11y_delete_description"))
self.btn_delete.setToolTip(tr("tooltip_delete"))
self.btn_choose_export_folder.setText(tr("btn_export_folder"))
self.btn_choose_export_folder.setAccessibleName(tr("btn_export_folder"))
self.btn_choose_export_folder.setAccessibleDescription(tr("a11y_export_folder_description"))
self.btn_choose_export_folder.setToolTip(tr("tooltip_export_folder"))
self.btn_reset_export_folder.setText(tr("btn_export_folder_reset"))
self.btn_reset_export_folder.setAccessibleName(tr("btn_export_folder_reset"))
self.btn_reset_export_folder.setAccessibleDescription(tr("a11y_export_folder_reset_description"))
self.btn_reset_export_folder.setToolTip(tr("tooltip_export_folder_reset"))
self.status_label.setAccessibleName(tr("a11y_status_label_name"))
self.export_folder_label.setAccessibleName(tr("a11y_export_folder_label_name"))
self._update_export_folder_label()
def add_paths_from_arguments(self, paths):
"""Nimmt beim Start uebergebene Dateipfade in die Liste auf.
Windows uebergibt den Dateipfad als Kommandozeilenargument, wenn eine
Datei per Doppelklick, ueber "Oeffnen mit" oder per Ablegen auf dem
Programmsymbol geoeffnet wird. Ohne diese Auswertung startet die
Anwendung zwar, zeigt die Datei aber nicht an - die Dateizuordnung im
AppxManifest allein genuegt also nicht.
Verzeichnisse werden flach aufgeloest. Nicht unterstuetzte Endungen und
Duplikate filtert add_file bereits selbst heraus.
Args:
paths: Liste von Pfaden, ueblicherweise sys.argv[1:].
Returns:
Anzahl der tatsaechlich aufgenommenen Dateien.
"""
added = 0
for raw in paths or []:
if not raw or str(raw).startswith("-"):
continue
try:
path = os.path.abspath(os.path.expanduser(str(raw)))
except (OSError, ValueError):
continue
candidates = []
if os.path.isdir(path):
try:
candidates = [os.path.join(path, e) for e in sorted(os.listdir(path))]
except OSError:
continue
elif os.path.isfile(path):
candidates = [path]
for candidate in candidates:
if not os.path.isfile(candidate):
continue
before = self.list_widget.count()
self.list_widget.add_file(candidate)
added += self.list_widget.count() - before