-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMFSearch
More file actions
124 lines (103 loc) · 4.35 KB
/
Copy pathMFSearch
File metadata and controls
124 lines (103 loc) · 4.35 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
import sys
import os
import re
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QPushButton, QLabel,
QFileDialog, QListWidget, QLineEdit, QTextEdit, QMessageBox
)
from PyQt5.QtCore import Qt
import fitz # PyMuPDF
from docx import Document
class FileSearchApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Multi-File Search")
self.resize(800, 600)
self.files = []
layout = QVBoxLayout()
self.file_list = QListWidget()
layout.addWidget(QLabel("Files to Search:"))
layout.addWidget(self.file_list)
self.add_file_btn = QPushButton("Add Files")
self.add_file_btn.clicked.connect(self.add_files)
layout.addWidget(self.add_file_btn)
self.add_folder_btn = QPushButton("Add Folder")
self.add_folder_btn.clicked.connect(self.add_folder)
layout.addWidget(self.add_folder_btn)
self.search_input = QLineEdit()
self.search_input.setPlaceholderText("Enter search term...")
layout.addWidget(self.search_input)
self.search_btn = QPushButton("Search")
self.search_btn.clicked.connect(self.search_files)
layout.addWidget(self.search_btn)
self.results = QTextEdit()
self.results.setReadOnly(True)
layout.addWidget(self.results)
self.setLayout(layout)
def add_files(self):
files, _ = QFileDialog.getOpenFileNames(self, "Select Files")
for f in files:
if f not in self.files:
self.files.append(f)
self.file_list.addItem(f)
def add_folder(self):
folder = QFileDialog.getExistingDirectory(self, "Select Folder")
if folder:
for root, _, filenames in os.walk(folder):
for name in filenames:
if name.endswith((".pdf", ".docx", ".txt")):
path = os.path.join(root, name)
if path not in self.files:
self.files.append(path)
self.file_list.addItem(path)
def search_files(self):
query = self.search_input.text().strip()
if not query:
QMessageBox.warning(self, "Input Error", "Please enter a search term.")
return
results = []
for path in self.files:
try:
if path.endswith(".pdf"):
results += self.search_pdf(path, query)
elif path.endswith(".docx"):
results += self.search_docx(path, query)
elif path.endswith(".txt"):
results += self.search_txt(path, query)
except Exception as e:
results.append(f"Error reading {path}: {str(e)}")
if results:
self.results.setHtml("<br><br>".join(results))
else:
self.results.setText("No matches found.")
def search_pdf(self, path, query):
doc = fitz.open(path)
matches = []
for page_num in range(len(doc)):
text = doc[page_num].get_text()
for match in re.finditer(query, text, re.IGNORECASE):
snippet = text[max(match.start()-50, 0):match.end()+50].replace(query, f"<b>{query}</b>")
matches.append(f"<b>{os.path.basename(path)}</b> - Page {page_num + 1}: ...{snippet}...")
return matches
def search_docx(self, path, query):
doc = Document(path)
matches = []
for i, para in enumerate(doc.paragraphs):
if re.search(query, para.text, re.IGNORECASE):
snippet = re.sub(f"({query})", r"<b>\\1</b>", para.text, flags=re.IGNORECASE)
matches.append(f"<b>{os.path.basename(path)}</b> - Paragraph {i + 1}: {snippet}")
return matches
def search_txt(self, path, query):
with open(path, 'r', encoding='utf-8', errors='ignore') as file:
lines = file.readlines()
matches = []
for i, line in enumerate(lines):
if re.search(query, line, re.IGNORECASE):
snippet = re.sub(f"({query})", r"<b>\\1</b>", line.strip(), flags=re.IGNORECASE)
matches.append(f"<b>{os.path.basename(path)}</b> - Line {i + 1}: {snippet}")
return matches
if __name__ == '__main__':
app = QApplication(sys.argv)
window = FileSearchApp()
window.show()
sys.exit(app.exec_())