-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_find_replace.py
More file actions
89 lines (74 loc) · 2.49 KB
/
Copy path02_find_replace.py
File metadata and controls
89 lines (74 loc) · 2.49 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
"""
02 - Find & Replace text in a PDF.
Demonstrates the flagship feature of the library:
- Single find & replace
- Batch with multiple pairs
- Regex matching
- Page-range scoped
- Styled replacements (color + highlight)
Run:
python examples/02_find_replace.py
"""
from _common import (
header, info, success, ensure_output_dir, get_sample_pdf, init_license,
)
import exis_pdfeditor
def main() -> None:
init_license()
header("Demo 02 - Find & Replace")
sample = get_sample_pdf()
out = ensure_output_dir()
# 1. Single find & replace
info("Single find & replace...")
result = exis_pdfeditor.find_replace(
str(sample),
str(out / "02-single.pdf"),
"the", "THE", # change "the" -> "THE" everywhere
)
success(f"Replaced {result.totalReplacements} occurrences across {result.pagesModified} pages")
# 2. Batch with multiple pairs
info("Batch with multiple pairs...")
result = exis_pdfeditor.find_replace(
str(sample),
str(out / "02-batch.pdf"),
pairs=[
{"search": "the", "replace": "THE"},
{"search": "and", "replace": "AND"},
{"search": "of", "replace": "OF"},
],
)
success(f"Batch replaced {result.totalReplacements} total occurrences")
# 3. Regex - match any 4+ digit number and wrap in [n]
info("Regex find & replace...")
result = exis_pdfeditor.find_replace(
str(sample),
str(out / "02-regex.pdf"),
pairs=[
{"search": r"\d{4,}", "replace": "[NUMBER]", "isRegex": True},
],
)
success(f"Regex replaced {result.totalReplacements} occurrences")
# 4. Styled replacement - red text with yellow highlight
info("Styled replacement (red text + yellow highlight)...")
result = exis_pdfeditor.find_replace(
str(sample),
str(out / "02-styled.pdf"),
"the", "THE",
replacement_text_color={"r": 1, "g": 0, "b": 0},
replacement_highlight_color={"r": 1, "g": 1, "b": 0},
replacement_bold=True,
)
success(f"Styled {result.totalReplacements} occurrences")
# 5. Page-range scoped (page 1 only)
info("Page-range scoped (page 1 only)...")
result = exis_pdfeditor.find_replace(
str(sample),
str(out / "02-page1.pdf"),
"the", "THE",
page_range=[1],
)
success(f"Page-1-only replaced {result.totalReplacements} occurrences")
print()
info(f"Output files written to: {out}")
if __name__ == "__main__":
main()