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,48 @@
1
+ # Extracting Text Blocks
2
+
3
+ Often, you need a specific section, like a paragraph between two headings. You can find a starting element and select everything below it until an ending element.
4
+
5
+ Let's extract the "Summary" section from `01-practice.pdf`. It starts after "Summary:" and ends before the thick horizontal line.
6
+
7
+ ```python
8
+ #%pip install "natural-pdf[all]"
9
+ ```
10
+
11
+
12
+ ```python
13
+ from natural_pdf import PDF
14
+
15
+ # Load the PDF and get the page
16
+ pdf = PDF("https://github.com/jsoma/natural-pdf/raw/refs/heads/main/pdfs/01-practice.pdf")
17
+ page = pdf.pages[0]
18
+
19
+ # Find the starting element ("Summary:")
20
+ start_marker = page.find('text:contains("Summary:")')
21
+
22
+ # Select elements below the start_marker, stopping *before*
23
+ # the thick horizontal line (a line with height > 1).
24
+ summary_elements = start_marker.below(
25
+ include_element=True, # Include the "Summary:" text itself
26
+ until="line[height > 1]"
27
+ )
28
+
29
+ # Visualize the elements found in this block
30
+ summary_elements.highlight(color="lightgreen", label="Summary Block")
31
+
32
+ # Extract and display the text from the collection of summary elements
33
+ summary_elements.extract_text()
34
+
35
+ ```
36
+
37
+ ```python
38
+ # Display the page image to see the visualization
39
+ page.to_image()
40
+ ```
41
+
42
+ This selects the elements using `.below(until=...)` and extracts their text. The second code block displays the page image with the visualized section.
43
+
44
+ <div class="admonition note">
45
+ <p class="admonition-title">Selector Specificity</p>
46
+
47
+ We used `line[height > 1]` to find the thick horizontal line. You might need to adjust selectors based on the specific PDF structure. Inspecting element properties can help you find reliable start and end markers.
48
+ </div>
@@ -0,0 +1,114 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "887b8e7a",
6
+ "metadata": {},
7
+ "source": [
8
+ "# Basic Table Extraction\n",
9
+ "\n",
10
+ "PDFs often contain tables, and `natural-pdf` provides methods to extract their data, building on `pdfplumber`'s capabilities.\n",
11
+ "\n",
12
+ "Let's extract the \"Violations\" table from our practice PDF."
13
+ ]
14
+ },
15
+ {
16
+ "cell_type": "code",
17
+ "execution_count": 1,
18
+ "id": "c920f4f6",
19
+ "metadata": {
20
+ "execution": {
21
+ "iopub.execute_input": "2025-04-16T14:57:17.973992Z",
22
+ "iopub.status.busy": "2025-04-16T14:57:17.973825Z",
23
+ "iopub.status.idle": "2025-04-16T14:57:17.979183Z",
24
+ "shell.execute_reply": "2025-04-16T14:57:17.978811Z"
25
+ },
26
+ "lines_to_next_cell": 2
27
+ },
28
+ "outputs": [],
29
+ "source": [
30
+ "#%pip install \"natural-pdf[all]\""
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 2,
36
+ "id": "28430cf6",
37
+ "metadata": {
38
+ "execution": {
39
+ "iopub.execute_input": "2025-04-16T14:57:17.980899Z",
40
+ "iopub.status.busy": "2025-04-16T14:57:17.980746Z",
41
+ "iopub.status.idle": "2025-04-16T14:57:24.102292Z",
42
+ "shell.execute_reply": "2025-04-16T14:57:24.101834Z"
43
+ }
44
+ },
45
+ "outputs": [],
46
+ "source": [
47
+ "from natural_pdf import PDF\n",
48
+ "\n",
49
+ "# Load a PDF\n",
50
+ "pdf = PDF(\"https://github.com/jsoma/natural-pdf/raw/refs/heads/main/pdfs/01-practice.pdf\")\n",
51
+ "page = pdf.pages[0]\n",
52
+ "\n",
53
+ "# Use extract_tables() to find all tables on the page.\n",
54
+ "# It returns a list of tables, where each table is a list of lists.\n",
55
+ "tables_data = page.extract_tables()\n",
56
+ "\n",
57
+ "# Display the first table found\n",
58
+ "tables_data[0] if tables_data else \"No tables found\"\n",
59
+ "\n",
60
+ "# You can also visualize the general area of the first table \n",
61
+ "# by finding elements in that region\n",
62
+ "if tables_data:\n",
63
+ " # Find a header element in the table\n",
64
+ " statute_header = page.find('text:contains(\"Statute\")')\n",
65
+ " if statute_header:\n",
66
+ " # Show the area\n",
67
+ " statute_header.below(height=100).highlight(color=\"green\", label=\"Table Area\")\n",
68
+ " page.to_image()"
69
+ ]
70
+ },
71
+ {
72
+ "cell_type": "markdown",
73
+ "id": "4ad2a9db",
74
+ "metadata": {},
75
+ "source": [
76
+ "This code uses `page.extract_tables()` which attempts to automatically detect tables based on visual cues like lines and whitespace. The result is a list of lists, representing the rows and cells of the table.\n",
77
+ "\n",
78
+ "<div class=\"admonition note\">\n",
79
+ "<p class=\"admonition-title\">Table Settings and Limitations</p>\n",
80
+ "\n",
81
+ " The default `extract_tables()` works well for simple, clearly defined tables. However, it might struggle with:\n",
82
+ " * Tables without clear borders or lines.\n",
83
+ " * Complex merged cells.\n",
84
+ " * Tables spanning multiple pages.\n",
85
+ "\n",
86
+ " `pdfplumber` (and thus `natural-pdf`) allows passing `table_settings` dictionaries to `extract_tables()` for more control over the detection strategy (e.g., `\"vertical_strategy\": \"text\"`, `\"horizontal_strategy\": \"text\"`).\n",
87
+ "\n",
88
+ " For even more robust table detection, especially for tables without explicit lines, using Layout Analysis (like `page.analyze_layout(engine='tatr')`) first, finding the table `region`, and then calling `region.extract_table()` can yield better results. We'll explore layout analysis in a later tutorial.\n",
89
+ "</div> "
90
+ ]
91
+ }
92
+ ],
93
+ "metadata": {
94
+ "jupytext": {
95
+ "cell_metadata_filter": "-all",
96
+ "main_language": "python",
97
+ "notebook_metadata_filter": "-all"
98
+ },
99
+ "language_info": {
100
+ "codemirror_mode": {
101
+ "name": "ipython",
102
+ "version": 3
103
+ },
104
+ "file_extension": ".py",
105
+ "mimetype": "text/x-python",
106
+ "name": "python",
107
+ "nbconvert_exporter": "python",
108
+ "pygments_lexer": "ipython3",
109
+ "version": "3.10.13"
110
+ }
111
+ },
112
+ "nbformat": 4,
113
+ "nbformat_minor": 5
114
+ }
@@ -0,0 +1,50 @@
1
+ # Basic Table Extraction
2
+
3
+ PDFs often contain tables, and `natural-pdf` provides methods to extract their data, building on `pdfplumber`'s capabilities.
4
+
5
+ Let's extract the "Violations" table from our 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
+ page = pdf.pages[0]
18
+
19
+ # Use extract_tables() to find all tables on the page.
20
+ # It returns a list of tables, where each table is a list of lists.
21
+ tables_data = page.extract_tables()
22
+
23
+ # Display the first table found
24
+ tables_data[0] if tables_data else "No tables found"
25
+
26
+ # You can also visualize the general area of the first table
27
+ # by finding elements in that region
28
+ if tables_data:
29
+ # Find a header element in the table
30
+ statute_header = page.find('text:contains("Statute")')
31
+ if statute_header:
32
+ # Show the area
33
+ statute_header.below(height=100).highlight(color="green", label="Table Area")
34
+ page.to_image()
35
+ ```
36
+
37
+ This code uses `page.extract_tables()` which attempts to automatically detect tables based on visual cues like lines and whitespace. The result is a list of lists, representing the rows and cells of the table.
38
+
39
+ <div class="admonition note">
40
+ <p class="admonition-title">Table Settings and Limitations</p>
41
+
42
+ The default `extract_tables()` works well for simple, clearly defined tables. However, it might struggle with:
43
+ * Tables without clear borders or lines.
44
+ * Complex merged cells.
45
+ * Tables spanning multiple pages.
46
+
47
+ `pdfplumber` (and thus `natural-pdf`) allows passing `table_settings` dictionaries to `extract_tables()` for more control over the detection strategy (e.g., `"vertical_strategy": "text"`, `"horizontal_strategy": "text"`).
48
+
49
+ For even more robust table detection, especially for tables without explicit lines, using Layout Analysis (like `page.analyze_layout(engine='tatr')`) first, finding the table `region`, and then calling `region.extract_table()` can yield better results. We'll explore layout analysis in a later tutorial.
50
+ </div>