|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+from pymupdf import TEXTFLAGS_DICT, TEXT_PRESERVE_IMAGES
|
|
|
2
|
+from typing import Tuple, Dict, List
|
|
|
3
|
+
|
|
|
4
|
+import pymupdf
|
|
|
5
|
+
|
|
|
6
|
+BBox = Tuple[float, float, float, float] # (x0, y0, x1, y1) or top-left and bottom-right corners
|
|
|
7
|
+Origin = Tuple[float, float] # (x, y) or (x1, y0) or bottom-left corner
|
|
|
8
|
+
|
|
|
9
|
+def extract_text(pdf_path: str):
|
|
|
10
|
+ flags = TEXTFLAGS_DICT & ~TEXT_PRESERVE_IMAGES
|
|
|
11
|
+ extracted_text = {}
|
|
|
12
|
+ with pymupdf.open(pdf_path) as pdf_file:
|
|
|
13
|
+ for page in pdf_file:
|
|
|
14
|
+ text_dict = page.get_text('dict', flags=flags, sort=True)
|
|
|
15
|
+ for block in text_dict['blocks']:
|
|
|
16
|
+ for line in block['lines']:
|
|
|
17
|
+ for span in line['spans']:
|
|
|
18
|
+ original_text = span['text']
|
|
|
19
|
+ stripped_text = original_text.strip()
|
|
|
20
|
+ if not stripped_text:
|
|
|
21
|
+ continue
|
|
|
22
|
+ origin = span['origin']
|
|
|
23
|
+ bbox = span['bbox']
|
|
|
24
|
+ corrected_bbox = _correct_bbox(bbox, origin)
|
|
|
25
|
+ text_with_metadata = {
|
|
|
26
|
+ 'original_text': span['text'],
|
|
|
27
|
+ 'stripped_text': stripped_text,
|
|
|
28
|
+ 'bbox': corrected_bbox,
|
|
|
29
|
+ 'origin': origin,
|
|
|
30
|
+ 'size': span['size'],
|
|
|
31
|
+ 'font': span['font'],
|
|
|
32
|
+ 'color': _int_to_rgbf(span['color']),
|
|
|
33
|
+ }
|
|
|
34
|
+ extracted_text.setdefault(page.number, []).append(text_with_metadata)
|
|
|
35
|
+ return extracted_text
|
|
|
36
|
+
|
|
|
37
|
+def replace_texts(pdf_path: str, output_path: str, replacement_data: Dict, preserve_original_fonts: bool = True):
|
|
|
38
|
+ with pymupdf.open(pdf_path) as pdf_file:
|
|
|
39
|
+ for page_number, page_replacements in replacement_data.items():
|
|
|
40
|
+ page = pdf_file[int(page_number)]
|
|
|
41
|
+ page_fonts = page.get_fonts()
|
|
|
42
|
+
|
|
|
43
|
+ # Add redact annotation to remove the original text
|
|
|
44
|
+ for replacement in page_replacements:
|
|
|
45
|
+ page.add_redact_annot(replacement['bbox'])
|
|
|
46
|
+
|
|
|
47
|
+ page.apply_redactions()
|
|
|
48
|
+
|
|
|
49
|
+ # Insert the corrected text
|
|
|
50
|
+ for replacement in page_replacements:
|
|
|
51
|
+ original_text = replacement['original_text']
|
|
|
52
|
+ stripped_text = replacement['stripped_text']
|
|
|
53
|
+ corrected_text = replacement['corrected_text']
|
|
|
54
|
+ replacement_text = original_text.replace(stripped_text, corrected_text)
|
|
|
55
|
+
|
|
|
56
|
+ page.insert_text(
|
|
|
57
|
+ replacement['origin'],
|
|
|
58
|
+ replacement_text,
|
|
|
59
|
+ fontsize=replacement['size'],
|
|
|
60
|
+ fontname=_get_font_name(replacement['font'], page_fonts) if preserve_original_fonts else 'helvetica',
|
|
|
61
|
+ color=replacement['color'],
|
|
|
62
|
+ )
|
|
|
63
|
+ pdf_file.save(output_path, garbage=3, deflate=True, clean=True)
|
|
|
64
|
+
|
|
|
65
|
+def _correct_bbox(bbox: BBox, origin: Origin) -> BBox:
|
|
|
66
|
+ corrected_bbox = list(bbox)
|
|
|
67
|
+ if bbox[3] != origin[1]:
|
|
|
68
|
+ corrected_bbox[1] = origin[1] - (bbox[3] - bbox[1])
|
|
|
69
|
+ corrected_bbox[3] = origin[1]
|
|
|
70
|
+ return tuple(corrected_bbox)
|
|
|
71
|
+
|
|
|
72
|
+def _int_to_rgbf(color: int) -> Tuple[float, float, float]:
|
|
|
73
|
+ return (
|
|
|
74
|
+ ((color >> 16) & 0xFF) / 255,
|
|
|
75
|
+ ((color >> 8) & 0xFF) / 255,
|
|
|
76
|
+ (color & 0xFF) / 255,
|
|
|
77
|
+ )
|
|
|
78
|
+
|
|
|
79
|
+def _get_font_name(keyword: str, fonts: List):
|
|
|
80
|
+ for font in fonts:
|
|
|
81
|
+ if keyword in font[3]:
|
|
|
82
|
+ return font[4]
|
|
|
83
|
+ return 'helvetica'
|