-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate.py
More file actions
64 lines (35 loc) · 1.2 KB
/
Copy pathaggregate.py
File metadata and controls
64 lines (35 loc) · 1.2 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
import pymupdf
from pathlib import Path
print("Search text then aggregate pages with match(es)")
wdir = Path(input("Directory: ")) or Path.cwd()
out = pymupdf.open()
needle = input("Needle: ")
total = 0
# Next screen
for file_path in wdir.iterdir():
if file_path.suffix.lower() == ".pdf":
print(file_path.name)
with pymupdf.open(file_path) as doc:
# Next screen
for index,page in enumerate(doc):
res = page.search_for(needle, quads=True)
if(len(res) > 0):
# Next screen
print("\tpage #",index,":",
len(res),"match(es) found")
page.add_highlight_annot(res)
out.insert_pdf(doc,
from_page = index,
to_page = index)
total += 1
#for r in res:
#pass
# Next Screen
if total > 0:
xfname = input("Output file:")
output_file = wdir / xfname
out.save(output_file)
out.close()
print(f"\nSaved PDF to: {output_file}")
else:
print("\nNo match found.")