-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_text_extraction.py
More file actions
54 lines (41 loc) · 1.57 KB
/
Copy path03_text_extraction.py
File metadata and controls
54 lines (41 loc) · 1.57 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
"""
03 - Text extraction.
Two modes:
- Plain text (.fullText / per-page .text)
- Structured (per-block coordinates, font name, font size)
Run:
python examples/03_text_extraction.py
"""
from _common import header, info, success, get_sample_pdf, init_license
import exis_pdfeditor
def main() -> None:
init_license()
header("Demo 03 - Text Extraction")
sample = get_sample_pdf()
# Plain text
info("Plain text extraction...")
result = exis_pdfeditor.extract_text(str(sample))
success(f"Extracted {len(result.fullText):,} characters from {len(result.pages)} pages")
if result.pages:
first_page_preview = result.pages[0].text[:200].replace("\n", " ")
print(f" First page preview: {first_page_preview}...")
# Specific pages
info("Extracting only page 1...")
page_result = exis_pdfeditor.extract_text(str(sample), pages=[1])
success(f"Page 1: {len(page_result.fullText):,} characters")
# Structured (positions, fonts)
info("Structured extraction...")
structured = exis_pdfeditor.extract_text_structured(str(sample))
if structured.pages:
page1 = structured.pages[0]
success(f"Page 1 has {len(page1.textBlocks)} text blocks")
print()
info("First 5 text blocks on page 1:")
for block in page1.textBlocks[:5]:
text_preview = block.text[:50].replace("\n", " ")
print(
f" [{block.x:6.1f}, {block.y:6.1f}] "
f"{block.fontName} {block.fontSize:.1f}pt: {text_preview}"
)
if __name__ == "__main__":
main()