-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_text_extractor.py
More file actions
975 lines (813 loc) · 34.7 KB
/
Copy pathpdf_text_extractor.py
File metadata and controls
975 lines (813 loc) · 34.7 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
"""
PDF Text Extractor
==================
A beautiful, fully offline macOS desktop app for extracting text from PDFs.
Features:
• Drag-and-drop PDF files directly onto the window
• Multi-file sidebar — open as many PDFs as you want
• Full-text search with match highlighting (⌘F)
• Copy extracted text to clipboard
• Save extracted text as .txt file
• Page-by-page breakdown with page markers
• Native macOS look with automatic Dark/Light mode
Requirements (installed automatically by run.sh):
pip install PyMuPDF PyQt6
"""
import sys
import os
from pathlib import Path
# ── Dependency check ────────────────────────────────────────────────────────
def _check_deps():
missing = []
try:
import fitz # noqa: F401
except ImportError:
missing.append("PyMuPDF")
try:
import PyQt6 # noqa: F401
except ImportError:
missing.append("PyQt6")
if missing:
print("─" * 60)
print("Missing dependencies:", ", ".join(missing))
print("Please run: pip install " + " ".join(missing))
print(" or simply: ./run.sh")
print("─" * 60)
sys.exit(1)
_check_deps()
# ── Imports ──────────────────────────────────────────────────────────────────
import fitz # PyMuPDF
from PyQt6.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QSplitter, QListWidget, QListWidgetItem, QTextEdit, QLabel,
QPushButton, QToolBar, QFileDialog, QFrame, QLineEdit,
QSizePolicy, QMessageBox, QScrollArea, QAbstractItemView,
)
from PyQt6.QtCore import (
Qt, QUrl, QTimer, QSize, pyqtSignal, QThread, QObject,
)
from PyQt6.QtGui import (
QFont, QColor, QDragEnterEvent, QDropEvent, QKeySequence,
QTextCharFormat, QTextCursor, QTextDocument, QAction,
QPainter, QPixmap, QFontDatabase, QIcon, QShortcut,
QPalette,
)
# ── Constants ────────────────────────────────────────────────────────────────
APP_NAME = "PDF Text Extractor"
APP_VERSION = "1.0"
# Detect macOS system font name
if sys.platform == "darwin":
BODY_FONT = "SF Pro Text"
MONO_FONT = "SF Mono"
UI_FONT = ".AppleSystemUIFont"
else:
BODY_FONT = "Segoe UI"
MONO_FONT = "Consolas"
UI_FONT = "Segoe UI"
SIDEBAR_WIDTH = 220
MIN_WINDOW_W = 900
MIN_WINDOW_H = 600
DEFAULT_WINDOW_W = 1240
DEFAULT_WINDOW_H = 820
# ── Worker thread for PDF loading ────────────────────────────────────────────
class PDFWorker(QObject):
"""Loads a PDF on a background thread to keep the UI responsive."""
finished = pyqtSignal(str, str, int) # path, text, page_count
error = pyqtSignal(str, str) # path, error_message
def __init__(self, path: str):
super().__init__()
self.path = path
def run(self):
try:
doc = fitz.open(self.path)
pages = doc.page_count
parts = []
for i, page in enumerate(doc):
text = page.get_text("text")
if text.strip():
parts.append(f"── Page {i + 1} of {pages} ──\n\n{text.rstrip()}")
doc.close()
full_text = "\n\n\n".join(parts) if parts else "(No extractable text found in this PDF.)"
self.finished.emit(self.path, full_text, pages)
except Exception as exc:
self.error.emit(self.path, str(exc))
# ── Data model ───────────────────────────────────────────────────────────────
class PDFDocument:
"""Holds all data for one loaded PDF."""
def __init__(self, path: str):
self.path : str = path
self.name : str = Path(path).name
self.stem : str = Path(path).stem
self.text : str = ""
self.page_count : int = 0
self.loading : bool = True
self.error : str | None = None
@property
def status_line(self) -> str:
if self.loading:
return f"{self.name} · Loading…"
if self.error:
return f"{self.name} · ⚠ {self.error}"
pages = f"{self.page_count} page{'s' if self.page_count != 1 else ''}"
chars = f"{len(self.text):,} chars"
return f"{self.name} · {pages} · {chars}"
# ── Custom sidebar item ───────────────────────────────────────────────────────
class FileListItem(QListWidgetItem):
def __init__(self, doc: PDFDocument):
super().__init__()
self.doc = doc
self._refresh()
def _refresh(self):
self.setText(self.doc.name)
self.setToolTip(self.doc.path)
if self.doc.error:
self.setForeground(QColor("#E05252"))
elif self.doc.loading:
self.setForeground(QColor("#888888"))
else:
self.setForeground(QColor()) # reset to default
# ── Drop zone (shown when no files are loaded) ────────────────────────────────
class DropZoneWidget(QWidget):
"""Full-window drag-and-drop landing page."""
filesDropped = pyqtSignal(list) # list[str] of PDF paths
def __init__(self, parent=None):
super().__init__(parent)
self.setAcceptDrops(True)
self._dragging_over = False
self._build_ui()
def _build_ui(self):
layout = QVBoxLayout(self)
layout.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.setSpacing(16)
# PDF icon (drawn with emoji / Unicode)
icon_label = QLabel("📄")
icon_font = QFont(UI_FONT, 56)
icon_label.setFont(icon_font)
icon_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
title = QLabel("Drop PDF files here")
title_font = QFont(UI_FONT, 22, QFont.Weight.Medium)
title.setFont(title_font)
title.setAlignment(Qt.AlignmentFlag.AlignCenter)
subtitle = QLabel("…or click Open PDF in the toolbar above")
sub_font = QFont(UI_FONT, 13)
subtitle.setFont(sub_font)
subtitle.setAlignment(Qt.AlignmentFlag.AlignCenter)
subtitle.setObjectName("subtitle")
layout.addStretch(2)
layout.addWidget(icon_label)
layout.addWidget(title)
layout.addWidget(subtitle)
layout.addStretch(3)
def dragEnterEvent(self, event: QDragEnterEvent):
if self._has_pdfs(event):
event.acceptProposedAction()
self._dragging_over = True
self.update()
def dragLeaveEvent(self, event):
self._dragging_over = False
self.update()
def dropEvent(self, event: QDropEvent):
self._dragging_over = False
self.update()
paths = self._pdf_paths(event)
if paths:
self.filesDropped.emit(paths)
def paintEvent(self, event):
super().paintEvent(event)
if self._dragging_over:
painter = QPainter(self)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
color = QColor(0, 120, 215, 40)
painter.fillRect(self.rect(), color)
@staticmethod
def _has_pdfs(event) -> bool:
if not event.mimeData().hasUrls():
return False
return any(u.toLocalFile().lower().endswith(".pdf")
for u in event.mimeData().urls())
@staticmethod
def _pdf_paths(event) -> list:
return [u.toLocalFile() for u in event.mimeData().urls()
if u.toLocalFile().lower().endswith(".pdf")]
# ── Text viewer ───────────────────────────────────────────────────────────────
class TextViewer(QTextEdit):
"""Read-only text display with search-highlight support."""
def __init__(self, parent=None):
super().__init__(parent)
self.setReadOnly(True)
self.setAcceptDrops(False)
self.setLineWrapMode(QTextEdit.LineWrapMode.WidgetWidth)
font = QFont(BODY_FONT, 13)
font.setStyleHint(QFont.StyleHint.SansSerif)
self.setFont(font)
self.setStyleSheet("""
QTextEdit {
border: none;
padding: 20px 28px;
background: transparent;
}
""")
def highlight_search(self, query: str) -> int:
"""Highlight all occurrences of *query*; return match count."""
# First, clear all existing highlights
cursor = self.textCursor()
cursor.select(QTextCursor.SelectionType.Document)
clear_fmt = QTextCharFormat()
cursor.setCharFormat(clear_fmt)
if not query:
return 0
hi_fmt = QTextCharFormat()
hi_fmt.setBackground(QColor("#FFD60A")) # iOS/macOS yellow highlight
hi_fmt.setForeground(QColor("#1C1C1E"))
count = 0
cursor = QTextCursor(self.document())
flags = QTextDocument.FindFlag(0) # case-insensitive by default
while True:
cursor = self.document().find(query, cursor, flags)
if cursor.isNull():
break
cursor.mergeCharFormat(hi_fmt)
count += 1
# Scroll to first match
if count:
c2 = QTextCursor(self.document())
c2 = self.document().find(query, c2, flags)
if not c2.isNull():
self.setTextCursor(c2)
self.ensureCursorVisible()
return count
# ── Search bar widget ─────────────────────────────────────────────────────────
class SearchBar(QFrame):
"""Slide-down search bar attached above the text viewer."""
queryChanged = pyqtSignal(str)
closeClicked = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self.setFrameShape(QFrame.Shape.NoFrame)
self.setObjectName("SearchBar")
layout = QHBoxLayout(self)
layout.setContentsMargins(12, 6, 12, 6)
layout.setSpacing(8)
search_icon = QLabel("🔍")
search_icon.setFont(QFont(UI_FONT, 13))
self.input = QLineEdit()
self.input.setPlaceholderText("Search in extracted text…")
self.input.setFont(QFont(UI_FONT, 13))
self.input.textChanged.connect(self.queryChanged)
self.input.setMinimumWidth(260)
self.match_label = QLabel("")
self.match_label.setFont(QFont(UI_FONT, 12))
self.match_label.setFixedWidth(90)
self.match_label.setAlignment(Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
self.match_label.setObjectName("matchLabel")
close_btn = QPushButton("✕")
close_btn.setFixedSize(22, 22)
close_btn.setFont(QFont(UI_FONT, 11))
close_btn.setCursor(Qt.CursorShape.PointingHandCursor)
close_btn.setObjectName("closeSearchBtn")
close_btn.clicked.connect(self.closeClicked)
layout.addWidget(search_icon)
layout.addWidget(self.input)
layout.addWidget(self.match_label)
layout.addWidget(close_btn)
def focus(self):
self.input.setFocus()
self.input.selectAll()
def clear(self):
self.input.clear()
self.match_label.setText("")
def set_match_count(self, count: int, query: str):
if not query:
self.match_label.setText("")
elif count == 0:
self.match_label.setText("No matches")
else:
self.match_label.setText(f"{count} match{'es' if count != 1 else ''}")
# ── App icon ──────────────────────────────────────────────────────────────────
def _make_app_icon() -> QIcon:
"""Draw a crisp PDF document icon programmatically."""
sz = 256
px = QPixmap(sz, sz)
px.fill(Qt.GlobalColor.transparent)
p = QPainter(px)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
# Shadow
from PyQt6.QtCore import QRectF
shadow_rect = QRectF(10, 12, sz - 16, sz - 16)
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor(0, 0, 0, 40))
p.drawRoundedRect(shadow_rect, 18, 18)
# Page body — white with slight warm tint
body_rect = QRectF(6, 4, sz - 16, sz - 16)
p.setBrush(QColor("#FAFAFA"))
p.setPen(Qt.PenStyle.NoPen)
p.drawRoundedRect(body_rect, 16, 16)
# Folded corner (top-right)
fold = 40
bx, by = int(body_rect.x()), int(body_rect.y())
bw = int(body_rect.width())
from PyQt6.QtGui import QPolygon
from PyQt6.QtCore import QPoint
fold_poly = QPolygon([
QPoint(bx + bw - fold, by),
QPoint(bx + bw, by + fold),
QPoint(bx + bw - fold, by + fold),
])
p.setBrush(QColor("#E0E0E0"))
p.drawPolygon(fold_poly)
# Fold crease line
p.setPen(QColor("#CCCCCC"))
from PyQt6.QtCore import QLine
p.drawLine(QLine(bx + bw - fold, by, bx + bw - fold, by + fold))
p.drawLine(QLine(bx + bw - fold, by + fold, bx + bw, by + fold))
# Red badge at bottom
badge_rect = QRectF(bx + 14, by + sz - 80, bw - 28, 52)
from PyQt6.QtGui import QLinearGradient
grad = QLinearGradient(badge_rect.topLeft(), badge_rect.bottomLeft())
grad.setColorAt(0, QColor("#FF3B30"))
grad.setColorAt(1, QColor("#C0271E"))
p.setBrush(grad)
p.setPen(Qt.PenStyle.NoPen)
p.drawRoundedRect(badge_rect, 10, 10)
# "PDF" text in badge
font = QFont("SF Pro Display", 52, QFont.Weight.Bold)
font.setLetterSpacing(QFont.SpacingType.AbsoluteSpacing, 2)
p.setFont(font)
p.setPen(QColor("#FFFFFF"))
p.drawText(badge_rect.toRect(), Qt.AlignmentFlag.AlignCenter, "PDF")
# Faint lines suggesting text content
p.setPen(QColor("#DDDDDD"))
line_x = bx + 20
line_w = bw - 60
for ly in [by + 60, by + 82, by + 104, by + 126, by + 148]:
w = line_w if ly != by + 148 else int(line_w * 0.6)
p.drawLine(line_x, ly, line_x + w, ly)
p.end()
return QIcon(px)
# ── Main window ───────────────────────────────────────────────────────────────
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.documents : list[PDFDocument] = []
self.current_doc: PDFDocument | None = None
self._workers : list[tuple] = [] # keep (thread, worker) alive
self.setWindowTitle(APP_NAME)
self.setMinimumSize(MIN_WINDOW_W, MIN_WINDOW_H)
self.resize(DEFAULT_WINDOW_W, DEFAULT_WINDOW_H)
self.setAcceptDrops(True)
self._build_ui()
self._build_toolbar()
self._apply_stylesheet()
self._bind_shortcuts()
# ── UI construction ──────────────────────────────────────────────────────
def _build_ui(self):
central = QWidget()
self.setCentralWidget(central)
root = QVBoxLayout(central)
root.setContentsMargins(0, 0, 0, 0)
root.setSpacing(0)
# ── Search bar (hidden by default) ─────
self.search_bar = SearchBar()
self.search_bar.setVisible(False)
self.search_bar.queryChanged.connect(self._on_search_changed)
self.search_bar.closeClicked.connect(self._hide_search)
# ── Main splitter ──────────────────────
self.splitter = QSplitter(Qt.Orientation.Horizontal)
self.splitter.setHandleWidth(1)
self.splitter.setChildrenCollapsible(False)
# Sidebar
self.sidebar = QListWidget()
self.sidebar.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
self.sidebar.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
self.sidebar.setObjectName("Sidebar")
self.sidebar.currentItemChanged.connect(self._on_sidebar_changed)
self.sidebar.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.sidebar.customContextMenuRequested.connect(self._sidebar_context_menu)
# Right panel (content area)
right_panel = QWidget()
right_panel.setObjectName("RightPanel")
right_layout = QVBoxLayout(right_panel)
right_layout.setContentsMargins(0, 0, 0, 0)
right_layout.setSpacing(0)
# Drop zone (shown when no docs loaded)
self.drop_zone = DropZoneWidget()
self.drop_zone.filesDropped.connect(self._load_pdfs)
# Text viewer (shown when doc is selected)
self.text_viewer = TextViewer()
self.text_viewer.setVisible(False)
right_layout.addWidget(self.search_bar)
right_layout.addWidget(self.drop_zone)
right_layout.addWidget(self.text_viewer)
# Status bar
self.status_label = QLabel("Open a PDF to get started")
self.status_label.setObjectName("StatusLabel")
self.status_label.setContentsMargins(14, 4, 14, 5)
right_layout.addWidget(self.status_label)
# Sidebar container (list + button below)
sidebar_container = QWidget()
sidebar_container.setObjectName("SidebarContainer")
sc_layout = QVBoxLayout(sidebar_container)
sc_layout.setContentsMargins(0, 0, 0, 0)
sc_layout.setSpacing(0)
sc_layout.addWidget(self.sidebar)
self.new_btn = QPushButton("+ Open PDF")
self.new_btn.setObjectName("NewFileBtn")
self.new_btn.setCursor(Qt.CursorShape.PointingHandCursor)
self.new_btn.clicked.connect(self._open_files)
self.new_btn.setFixedHeight(38)
sc_layout.addWidget(self.new_btn)
sidebar_container.setMinimumWidth(160)
sidebar_container.setMaximumWidth(320)
sidebar_container.setFixedWidth(SIDEBAR_WIDTH)
self.splitter.addWidget(sidebar_container)
self.splitter.addWidget(right_panel)
self.splitter.setSizes([SIDEBAR_WIDTH, DEFAULT_WINDOW_W - SIDEBAR_WIDTH])
root.addWidget(self.splitter)
def _build_toolbar(self):
tb = QToolBar("Main Toolbar")
tb.setMovable(False)
tb.setFloatable(False)
tb.setIconSize(QSize(16, 16))
tb.setObjectName("MainToolbar")
def _action(label: str, shortcut: str | None, slot):
a = QAction(label, self)
if shortcut:
a.setShortcut(QKeySequence(shortcut))
a.triggered.connect(slot)
tb.addAction(a)
return a
_action("Open PDF…", "Ctrl+O", self._open_files)
tb.addSeparator()
self.act_copy = _action("Copy Text", "Ctrl+Shift+C", self._copy_text)
self.act_save = _action("Save as .txt…","Ctrl+S", self._save_text)
tb.addSeparator()
self.act_search = _action("Search…", "Ctrl+F", self._show_search)
tb.addSeparator()
self.act_clear = _action("Remove File", None, self._remove_current)
self.act_copy.setEnabled(False)
self.act_save.setEnabled(False)
self.act_search.setEnabled(False)
self.act_clear.setEnabled(False)
self.addToolBar(tb)
def _bind_shortcuts(self):
QShortcut(QKeySequence("Ctrl+F"), self).activated.connect(self._show_search)
QShortcut(QKeySequence("Escape"), self).activated.connect(self._hide_search)
QShortcut(QKeySequence("Ctrl+W"), self).activated.connect(self._remove_current)
def _apply_stylesheet(self):
# Detect dark mode via palette
palette = QApplication.palette()
is_dark = palette.color(QPalette.ColorRole.Window).lightness() < 128
bg = "#1E1E1E" if is_dark else "#F5F5F7"
sidebar_bg= "#2A2A2A" if is_dark else "#ECECEC"
border = "#3A3A3A" if is_dark else "#D0D0D0"
text_dim = "#999999" if is_dark else "#6E6E6E"
sel_bg = "#3A3A3C" if is_dark else "#D1E8FF"
search_bg = "#2C2C2E" if is_dark else "#FFFFFF"
status_bg = "#252525" if is_dark else "#EBEBED"
self.setStyleSheet(f"""
QMainWindow {{
background: {bg};
}}
/* ── Toolbar ── */
QToolBar#MainToolbar {{
background: {sidebar_bg};
border-bottom: 1px solid {border};
spacing: 4px;
padding: 4px 8px;
}}
QToolBar#MainToolbar QToolButton {{
font-family: "{UI_FONT}";
font-size: 13px;
padding: 3px 10px;
border-radius: 5px;
border: none;
}}
QToolBar#MainToolbar QToolButton:hover {{
background: {sel_bg};
}}
QToolBar#MainToolbar QToolButton:pressed {{
background: {border};
}}
QToolBar#MainToolbar QToolButton:disabled {{
color: {text_dim};
}}
/* ── Sidebar ── */
QListWidget#Sidebar {{
background: {sidebar_bg};
border: none;
border-right: 1px solid {border};
font-family: "{UI_FONT}";
font-size: 13px;
padding: 4px 0px;
outline: 0;
}}
QListWidget#Sidebar::item {{
padding: 7px 12px;
border-radius: 6px;
margin: 1px 6px;
}}
QListWidget#Sidebar::item:selected {{
background: {sel_bg};
}}
QListWidget#Sidebar::item:hover:!selected {{
background: {border};
}}
/* ── Drop zone ── */
DropZoneWidget QLabel {{
color: {text_dim};
}}
DropZoneWidget QLabel#subtitle {{
font-size: 13px;
}}
/* ── Search bar ── */
SearchBar {{
background: {search_bg};
border-bottom: 1px solid {border};
}}
SearchBar QLineEdit {{
background: {bg};
border: 1px solid {border};
border-radius: 6px;
padding: 4px 8px;
font-family: "{UI_FONT}";
font-size: 13px;
}}
SearchBar QLineEdit:focus {{
border-color: #0A84FF;
}}
QLabel#matchLabel {{
color: {text_dim};
font-size: 12px;
}}
QPushButton#closeSearchBtn {{
background: transparent;
border: none;
color: {text_dim};
font-size: 13px;
border-radius: 11px;
}}
QPushButton#closeSearchBtn:hover {{
background: {border};
color: #FFFFFF;
}}
/* ── Status bar ── */
QLabel#StatusLabel {{
background: {status_bg};
border-top: 1px solid {border};
color: {text_dim};
font-family: "{UI_FONT}";
font-size: 12px;
padding: 4px 14px;
}}
/* ── New File Button ── */
QPushButton#NewFileBtn {{
background: transparent;
border: none;
border-top: 1px solid {border};
border-right: 1px solid {border};
font-family: "{UI_FONT}";
font-size: 13px;
font-weight: 500;
color: #0A84FF;
padding: 0px;
text-align: center;
}}
QPushButton#NewFileBtn:hover {{
background: {sel_bg};
}}
QPushButton#NewFileBtn:pressed {{
background: {border};
}}
/* ── Scrollbar ── */
QScrollBar:vertical {{
border: none;
background: transparent;
width: 8px;
margin: 0;
}}
QScrollBar::handle:vertical {{
background: {border};
border-radius: 4px;
min-height: 30px;
}}
QScrollBar::add-line:vertical,
QScrollBar::sub-line:vertical {{
height: 0;
}}
QScrollBar:horizontal {{
height: 0;
}}
""")
# ── Drag & drop onto main window ──────────────────────────────────────────
def dragEnterEvent(self, event: QDragEnterEvent):
if self._event_has_pdfs(event):
event.acceptProposedAction()
def dropEvent(self, event: QDropEvent):
paths = self._pdf_paths_from_event(event)
if paths:
self._load_pdfs(paths)
# ── File loading ──────────────────────────────────────────────────────────
def _open_files(self):
paths, _ = QFileDialog.getOpenFileNames(
self, "Open PDF Files", "",
"PDF Files (*.pdf);;All Files (*.*)"
)
if paths:
self._load_pdfs(paths)
def _load_pdfs(self, paths: list):
for path in paths:
path = os.path.abspath(path)
if any(d.path == path for d in self.documents):
continue # already loaded
doc = PDFDocument(path)
self.documents.append(doc)
item = FileListItem(doc)
self.sidebar.addItem(item)
# Load on background thread
thread = QThread()
worker = PDFWorker(path)
worker.moveToThread(thread)
worker.finished.connect(self._on_pdf_loaded)
worker.error.connect(self._on_pdf_error)
thread.started.connect(worker.run)
# Clean up when done
worker.finished.connect(thread.quit)
worker.error.connect(thread.quit)
thread.finished.connect(thread.deleteLater)
self._workers.append((thread, worker))
thread.start()
# Show last added file
if self.documents:
last_row = self.sidebar.count() - 1
self.sidebar.setCurrentRow(last_row)
def _on_pdf_loaded(self, path: str, text: str, page_count: int):
doc = self._find_doc(path)
if not doc:
return
doc.text = text
doc.page_count = page_count
doc.loading = False
self._refresh_sidebar_item(doc)
if self.current_doc and self.current_doc.path == path:
self._show_document(doc)
def _on_pdf_error(self, path: str, error_msg: str):
doc = self._find_doc(path)
if not doc:
return
doc.error = error_msg
doc.loading = False
self._refresh_sidebar_item(doc)
if self.current_doc and self.current_doc.path == path:
self._show_document(doc)
def _find_doc(self, path: str) -> PDFDocument | None:
return next((d for d in self.documents if d.path == path), None)
def _refresh_sidebar_item(self, doc: PDFDocument):
for i in range(self.sidebar.count()):
item = self.sidebar.item(i)
if isinstance(item, FileListItem) and item.doc is doc:
item._refresh()
break
# ── Display ───────────────────────────────────────────────────────────────
def _on_sidebar_changed(self, current, _previous):
if isinstance(current, FileListItem):
self._show_document(current.doc)
def _show_document(self, doc: PDFDocument):
self.current_doc = doc
self.drop_zone.setVisible(False)
self.text_viewer.setVisible(True)
if doc.loading:
self.text_viewer.setPlainText("⏳ Loading…")
else:
self.text_viewer.setPlainText(doc.text)
# Re-apply any active search
query = self.search_bar.input.text()
if query and self.search_bar.isVisible():
count = self.text_viewer.highlight_search(query)
self.search_bar.set_match_count(count, query)
# Update actions
has_doc = not doc.loading and not doc.error
self.act_copy.setEnabled(has_doc)
self.act_save.setEnabled(has_doc)
self.act_search.setEnabled(True)
self.act_clear.setEnabled(True)
self.status_label.setText(doc.status_line)
def _update_empty_state(self):
has_docs = bool(self.documents)
self.drop_zone.setVisible(not has_docs and not self.text_viewer.isVisible())
if not has_docs:
self.text_viewer.setVisible(False)
self.drop_zone.setVisible(True)
self.status_label.setText("Open a PDF to get started")
self.act_copy.setEnabled(False)
self.act_save.setEnabled(False)
self.act_search.setEnabled(False)
self.act_clear.setEnabled(False)
self.current_doc = None
# ── Search ────────────────────────────────────────────────────────────────
def _show_search(self):
if not self.current_doc:
return
self.search_bar.setVisible(True)
self.search_bar.focus()
def _hide_search(self):
self.search_bar.setVisible(False)
self.search_bar.clear()
self.text_viewer.highlight_search("")
def _on_search_changed(self, query: str):
count = self.text_viewer.highlight_search(query)
self.search_bar.set_match_count(count, query)
# ── Actions ───────────────────────────────────────────────────────────────
def _copy_text(self):
if self.current_doc and self.current_doc.text:
QApplication.clipboard().setText(self.current_doc.text)
self._flash_status("✓ Text copied to clipboard", 2500)
def _save_text(self):
if not self.current_doc:
return
default = str(Path(self.current_doc.path).with_suffix(".txt").name)
path, _ = QFileDialog.getSaveFileName(
self, "Save Extracted Text", default,
"Text Files (*.txt);;All Files (*.*)"
)
if path:
try:
with open(path, "w", encoding="utf-8") as fh:
fh.write(self.current_doc.text)
self._flash_status(f"✓ Saved to {Path(path).name}", 3000)
except OSError as exc:
QMessageBox.critical(self, "Save Error", str(exc))
def _remove_current(self):
if not self.current_doc:
return
row = self.sidebar.currentRow()
if row >= 0:
self.sidebar.takeItem(row)
self.documents.remove(self.current_doc)
self.current_doc = None
if self.documents:
new_row = min(row, self.sidebar.count() - 1)
self.sidebar.setCurrentRow(new_row)
else:
self._update_empty_state()
self._hide_search()
# ── Context menu on sidebar ───────────────────────────────────────────────
def _sidebar_context_menu(self, pos):
item = self.sidebar.itemAt(pos)
if not isinstance(item, FileListItem):
return
from PyQt6.QtWidgets import QMenu
menu = QMenu(self)
menu.addAction("Show in Finder", lambda: self._reveal_in_finder(item.doc.path))
menu.addSeparator()
menu.addAction("Remove from List", self._remove_current)
menu.exec(self.sidebar.viewport().mapToGlobal(pos))
def _reveal_in_finder(self, path: str):
import subprocess
subprocess.Popen(["open", "-R", path])
# ── Helpers ───────────────────────────────────────────────────────────────
def _flash_status(self, msg: str, duration_ms: int = 2000):
self.status_label.setText(msg)
if self.current_doc:
doc = self.current_doc
QTimer.singleShot(duration_ms, lambda: (
self.status_label.setText(doc.status_line)
if self.current_doc is doc else None
))
@staticmethod
def _event_has_pdfs(event) -> bool:
return (event.mimeData().hasUrls() and
any(u.toLocalFile().lower().endswith(".pdf")
for u in event.mimeData().urls()))
@staticmethod
def _pdf_paths_from_event(event) -> list:
return [u.toLocalFile() for u in event.mimeData().urls()
if u.toLocalFile().lower().endswith(".pdf")]
# ── Entry point ───────────────────────────────────────────────────────────────
def main():
# Enable high-DPI scaling
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough
)
app = QApplication(sys.argv)
app.setApplicationName(APP_NAME)
app.setApplicationVersion(APP_VERSION)
app.setOrganizationName("PDFTextExtractor")
# Use native macOS style
if sys.platform == "darwin":
app.setStyle("macos")
icon = _make_app_icon()
app.setWindowIcon(icon)
window = MainWindow()
window.setWindowIcon(icon)
window.show()
# Support opening files via command-line args
args = sys.argv[1:]
pdf_args = [a for a in args if a.lower().endswith(".pdf") and os.path.isfile(a)]
if pdf_args:
window._load_pdfs(pdf_args)
sys.exit(app.exec())
if __name__ == "__main__":
main()