natural-pdf 0.1.4__py3-none-any.whl → 0.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. docs/api/index.md +386 -0
  2. docs/assets/favicon.png +3 -0
  3. docs/assets/favicon.svg +3 -0
  4. docs/assets/javascripts/custom.js +17 -0
  5. docs/assets/logo.svg +3 -0
  6. docs/assets/sample-screen.png +0 -0
  7. docs/assets/social-preview.png +17 -0
  8. docs/assets/social-preview.svg +17 -0
  9. docs/assets/stylesheets/custom.css +65 -0
  10. docs/document-qa/index.ipynb +435 -0
  11. docs/document-qa/index.md +79 -0
  12. docs/element-selection/index.ipynb +915 -0
  13. docs/element-selection/index.md +229 -0
  14. docs/index.md +170 -0
  15. docs/installation/index.md +69 -0
  16. docs/interactive-widget/index.ipynb +962 -0
  17. docs/interactive-widget/index.md +12 -0
  18. docs/layout-analysis/index.ipynb +818 -0
  19. docs/layout-analysis/index.md +185 -0
  20. docs/ocr/index.md +222 -0
  21. docs/pdf-navigation/index.ipynb +314 -0
  22. docs/pdf-navigation/index.md +97 -0
  23. docs/regions/index.ipynb +816 -0
  24. docs/regions/index.md +294 -0
  25. docs/tables/index.ipynb +658 -0
  26. docs/tables/index.md +144 -0
  27. docs/text-analysis/index.ipynb +370 -0
  28. docs/text-analysis/index.md +105 -0
  29. docs/text-extraction/index.ipynb +1478 -0
  30. docs/text-extraction/index.md +292 -0
  31. docs/tutorials/01-loading-and-extraction.ipynb +1696 -0
  32. docs/tutorials/01-loading-and-extraction.md +95 -0
  33. docs/tutorials/02-finding-elements.ipynb +340 -0
  34. docs/tutorials/02-finding-elements.md +149 -0
  35. docs/tutorials/03-extracting-blocks.ipynb +147 -0
  36. docs/tutorials/03-extracting-blocks.md +48 -0
  37. docs/tutorials/04-table-extraction.ipynb +114 -0
  38. docs/tutorials/04-table-extraction.md +50 -0
  39. docs/tutorials/05-excluding-content.ipynb +270 -0
  40. docs/tutorials/05-excluding-content.md +109 -0
  41. docs/tutorials/06-document-qa.ipynb +332 -0
  42. docs/tutorials/06-document-qa.md +91 -0
  43. docs/tutorials/07-layout-analysis.ipynb +260 -0
  44. docs/tutorials/07-layout-analysis.md +66 -0
  45. docs/tutorials/07-working-with-regions.ipynb +409 -0
  46. docs/tutorials/07-working-with-regions.md +151 -0
  47. docs/tutorials/08-spatial-navigation.ipynb +508 -0
  48. docs/tutorials/08-spatial-navigation.md +190 -0
  49. docs/tutorials/09-section-extraction.ipynb +2434 -0
  50. docs/tutorials/09-section-extraction.md +256 -0
  51. docs/tutorials/10-form-field-extraction.ipynb +484 -0
  52. docs/tutorials/10-form-field-extraction.md +201 -0
  53. docs/tutorials/11-enhanced-table-processing.ipynb +54 -0
  54. docs/tutorials/11-enhanced-table-processing.md +9 -0
  55. docs/tutorials/12-ocr-integration.ipynb +586 -0
  56. docs/tutorials/12-ocr-integration.md +188 -0
  57. docs/tutorials/13-semantic-search.ipynb +1888 -0
  58. docs/tutorials/13-semantic-search.md +77 -0
  59. docs/visual-debugging/index.ipynb +2970 -0
  60. docs/visual-debugging/index.md +157 -0
  61. docs/visual-debugging/region.png +0 -0
  62. natural_pdf/__init__.py +39 -20
  63. natural_pdf/analyzers/__init__.py +2 -1
  64. natural_pdf/analyzers/layout/base.py +32 -24
  65. natural_pdf/analyzers/layout/docling.py +131 -72
  66. natural_pdf/analyzers/layout/layout_analyzer.py +156 -113
  67. natural_pdf/analyzers/layout/layout_manager.py +98 -58
  68. natural_pdf/analyzers/layout/layout_options.py +32 -17
  69. natural_pdf/analyzers/layout/paddle.py +152 -95
  70. natural_pdf/analyzers/layout/surya.py +164 -92
  71. natural_pdf/analyzers/layout/tatr.py +149 -84
  72. natural_pdf/analyzers/layout/yolo.py +84 -44
  73. natural_pdf/analyzers/text_options.py +22 -15
  74. natural_pdf/analyzers/text_structure.py +131 -85
  75. natural_pdf/analyzers/utils.py +30 -23
  76. natural_pdf/collections/pdf_collection.py +125 -97
  77. natural_pdf/core/__init__.py +1 -1
  78. natural_pdf/core/element_manager.py +416 -337
  79. natural_pdf/core/highlighting_service.py +268 -196
  80. natural_pdf/core/page.py +907 -513
  81. natural_pdf/core/pdf.py +385 -287
  82. natural_pdf/elements/__init__.py +1 -1
  83. natural_pdf/elements/base.py +302 -214
  84. natural_pdf/elements/collections.py +708 -508
  85. natural_pdf/elements/line.py +39 -36
  86. natural_pdf/elements/rect.py +32 -30
  87. natural_pdf/elements/region.py +854 -883
  88. natural_pdf/elements/text.py +122 -99
  89. natural_pdf/exporters/__init__.py +0 -1
  90. natural_pdf/exporters/searchable_pdf.py +261 -102
  91. natural_pdf/ocr/__init__.py +23 -14
  92. natural_pdf/ocr/engine.py +17 -8
  93. natural_pdf/ocr/engine_easyocr.py +63 -47
  94. natural_pdf/ocr/engine_paddle.py +97 -68
  95. natural_pdf/ocr/engine_surya.py +54 -44
  96. natural_pdf/ocr/ocr_manager.py +88 -62
  97. natural_pdf/ocr/ocr_options.py +16 -10
  98. natural_pdf/qa/__init__.py +1 -1
  99. natural_pdf/qa/document_qa.py +119 -111
  100. natural_pdf/search/__init__.py +37 -31
  101. natural_pdf/search/haystack_search_service.py +312 -189
  102. natural_pdf/search/haystack_utils.py +186 -122
  103. natural_pdf/search/search_options.py +25 -14
  104. natural_pdf/search/search_service_protocol.py +12 -6
  105. natural_pdf/search/searchable_mixin.py +261 -176
  106. natural_pdf/selectors/__init__.py +2 -1
  107. natural_pdf/selectors/parser.py +159 -316
  108. natural_pdf/templates/__init__.py +1 -1
  109. natural_pdf/utils/highlighting.py +8 -2
  110. natural_pdf/utils/reading_order.py +65 -63
  111. natural_pdf/utils/text_extraction.py +195 -0
  112. natural_pdf/utils/visualization.py +70 -61
  113. natural_pdf/widgets/__init__.py +2 -3
  114. natural_pdf/widgets/viewer.py +749 -718
  115. {natural_pdf-0.1.4.dist-info → natural_pdf-0.1.5.dist-info}/METADATA +15 -1
  116. natural_pdf-0.1.5.dist-info/RECORD +134 -0
  117. natural_pdf-0.1.5.dist-info/top_level.txt +5 -0
  118. notebooks/Examples.ipynb +1293 -0
  119. pdfs/.gitkeep +0 -0
  120. pdfs/01-practice.pdf +543 -0
  121. pdfs/0500000US42001.pdf +0 -0
  122. pdfs/0500000US42007.pdf +0 -0
  123. pdfs/2014 Statistics.pdf +0 -0
  124. pdfs/2019 Statistics.pdf +0 -0
  125. pdfs/Atlanta_Public_Schools_GA_sample.pdf +0 -0
  126. pdfs/needs-ocr.pdf +0 -0
  127. tests/test_loading.py +50 -0
  128. tests/test_optional_deps.py +298 -0
  129. natural_pdf-0.1.4.dist-info/RECORD +0 -61
  130. natural_pdf-0.1.4.dist-info/top_level.txt +0 -1
  131. {natural_pdf-0.1.4.dist-info → natural_pdf-0.1.5.dist-info}/WHEEL +0 -0
  132. {natural_pdf-0.1.4.dist-info → natural_pdf-0.1.5.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,149 @@
1
+ # Finding Specific Elements
2
+
3
+ Extracting all the text is useful, but often you need specific pieces of information. `natural-pdf` lets you find elements using selectors, similar to CSS.
4
+
5
+ Let's find the "Site" and "Date" information from our `01-practice.pdf`:
6
+
7
+ ```python
8
+ #%pip install "natural-pdf[all]"
9
+ ```
10
+
11
+
12
+ ```python
13
+ from natural_pdf import PDF
14
+
15
+ # Load a PDF
16
+ pdf = PDF("https://github.com/jsoma/natural-pdf/raw/refs/heads/main/pdfs/01-practice.pdf")
17
+
18
+ # Get the first page (index 0)
19
+ page = pdf.pages[0]
20
+
21
+ # Find the text element containing "Site:"
22
+ # The ':contains()' pseudo-class looks for text content.
23
+ site_label = page.find('text:contains("Site:")')
24
+
25
+ # Find the text element containing "Date:"
26
+ date_label = page.find('text:contains("Date:")')
27
+
28
+ # Visualize the found elements
29
+ site_label.highlight(color="red", label="Site Label")
30
+ date_label.highlight(color="blue", label="Date Label")
31
+
32
+ # Access the text content directly
33
+ {
34
+ "Site Label": site_label.text,
35
+ "Date Label": date_label.text
36
+ }
37
+
38
+ # Display the page image to see the visualized elements
39
+ page.to_image()
40
+ ```
41
+
42
+ ## Finding Elements by Color
43
+
44
+ You can find elements based on their color:
45
+
46
+ ```python
47
+ # Find text elements that are red
48
+ red_text = page.find('text[color~=red]')
49
+ red_text.highlight(color="red", label="Red Text")
50
+ print(f"Found red text: {red_text.text}")
51
+
52
+ # Find elements with specific RGB colors
53
+ blue_text = page.find('text[color=rgb(0,0,255)]')
54
+ ```
55
+
56
+ ## Finding Lines and Shapes
57
+
58
+ Find lines and rectangles based on their properties:
59
+
60
+ ```python
61
+ # Find horizontal lines
62
+ horizontal_lines = page.find_all('line[horizontal]')
63
+
64
+ # Find thick lines (width >= 2)
65
+ thick_lines = page.find_all('line[width>=2]')
66
+
67
+ # Find rectangles
68
+ rectangles = page.find_all('rect')
69
+
70
+ # Visualize what we found
71
+ page.clear_highlights()
72
+ horizontal_lines.highlight(color="blue", label="Horizontal Lines")
73
+ thick_lines.highlight(color="red", label="Thick Lines")
74
+ rectangles.highlight(color="green", label="Rectangles")
75
+ page.to_image()
76
+ ```
77
+
78
+ ## Finding Elements by Font Properties
79
+
80
+ ```python
81
+ # Find text with specific font properties
82
+ bold_text = page.find_all('text[style~=bold]')
83
+ large_text = page.find_all('text[size>=12]')
84
+
85
+ # Find text with specific font names
86
+ helvetica_text = page.find_all('text[fontname~=Helvetica]')
87
+ ```
88
+
89
+ ## Spatial Navigation
90
+
91
+ You can find elements based on their position relative to other elements:
92
+
93
+ ```python
94
+ # Find text above a specific element
95
+ above_text = page.find('line[width=2]').above().extract_text()
96
+
97
+ # Find text below a specific element
98
+ below_text = page.find('text:contains("Summary")').below().extract_text()
99
+
100
+ # Find text to the right of a specific element
101
+ nearby_text = page.find('text:contains("Site")').right(width=200).extract_text()
102
+ ```
103
+
104
+ ## Combining Selectors
105
+
106
+ You can combine multiple conditions to find exactly what you need:
107
+
108
+ ```python
109
+ # Find large, bold text that contains specific words
110
+ important_text = page.find_all('text[size>=12][style~=bold]:contains("Critical")')
111
+
112
+ # Find red text inside a rectangle
113
+ highlighted_text = page.find('rect').find_all('text[color~=red]')
114
+ ```
115
+
116
+ <div class="admonition note">
117
+ <p class="admonition-title">Handling Missing Elements</p>
118
+
119
+ In these examples, we know certain elements exist in the PDF. In real-world scenarios, `page.find()` might not find a match and would return `None`. Production code should check for this:
120
+
121
+ ```py
122
+ site_label = page.find('text:contains("Site:")')
123
+ if site_label:
124
+ # Found it! Proceed...
125
+ site_label.highlight(color="red", label="Site Label")
126
+ site_label.text # Display or use the text
127
+ else:
128
+ # Didn't find it, handle appropriately...
129
+ "Warning: 'Site:' label not found."
130
+ ```
131
+ </div>
132
+
133
+ <div class="admonition tip">
134
+ <p class="admonition-title">Visual Debugging</p>
135
+
136
+ When working with complex selectors, it's helpful to visualize what you're finding:
137
+
138
+ ```py
139
+ # Clear any existing highlights
140
+ page.clear_highlights()
141
+
142
+ # Find and highlight elements
143
+ elements = page.find_all('text[color~=red]')
144
+ elements.highlight(color="red", label="Red Text")
145
+
146
+ # Display the page to see what was found
147
+ page.to_image(width=800)
148
+ ```
149
+ </div>