docling-extractor 1.0.0__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Nalini Panwar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,3 @@
1
+ include README.md
2
+ include LICENSE
3
+ include pyproject.toml
@@ -0,0 +1,302 @@
1
+ Metadata-Version: 2.4
2
+ Name: docling-extractor
3
+ Version: 1.0.0
4
+ Summary: Production-grade document extraction with intelligent fallback chain: Docling -> PyMuPDF -> pdfplumber -> Tesseract
5
+ Author-email: Nalini Panwar <panwarnalini@gmail.com>
6
+ Maintainer-email: Nalini Panwar <panwarnalini@gmail.com>
7
+ License: MIT
8
+ Project-URL: Homepage, https://github.com/panwarnalini-hub/clinical-doc-pipelines
9
+ Project-URL: Documentation, https://github.com/panwarnalini-hub/clinical-doc-pipelines/blob/main/README.md
10
+ Project-URL: Repository, https://github.com/panwarnalini-hub/clinical-doc-pipelines
11
+ Project-URL: Bug Tracker, https://github.com/panwarnalini-hub/clinical-doc-pipelines/issues
12
+ Keywords: document-extraction,pdf-processing,docling,ocr,data-engineering,clinical-trials,nlp,databricks
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: Science/Research
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Operating System :: OS Independent
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.8
20
+ Classifier: Programming Language :: Python :: 3.9
21
+ Classifier: Programming Language :: Python :: 3.10
22
+ Classifier: Programming Language :: Python :: 3.11
23
+ Classifier: Programming Language :: Python :: 3.12
24
+ Classifier: Topic :: Scientific/Engineering
25
+ Classifier: Topic :: Text Processing
26
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
27
+ Requires-Python: >=3.8
28
+ Description-Content-Type: text/markdown
29
+ License-File: LICENSE
30
+ Requires-Dist: PyMuPDF>=1.23.0
31
+ Requires-Dist: pdfplumber>=0.10.0
32
+ Provides-Extra: full
33
+ Requires-Dist: docling>=2.0.0; extra == "full"
34
+ Requires-Dist: docling-core>=2.0.0; extra == "full"
35
+ Requires-Dist: pytesseract>=0.3.10; extra == "full"
36
+ Provides-Extra: dev
37
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
38
+ Requires-Dist: black>=23.0.0; extra == "dev"
39
+ Requires-Dist: isort>=5.12.0; extra == "dev"
40
+ Dynamic: license-file
41
+
42
+ # Docling Extractor
43
+
44
+ Production-grade document extraction library with intelligent fallback chain for robust PDF processing.
45
+
46
+ ## Overview
47
+
48
+ Docling Extractor provides a reliable pipeline for extracting text, tables, images, and structured content from PDF documents. The library automatically detects document type (scanned vs. digital) and applies the appropriate extraction method with fallback support.
49
+
50
+ ## Key Features
51
+
52
+ **Intelligent PDF Detection**
53
+ - Automatically identifies scanned vs. digital PDFs
54
+ - Routes documents to optimal extraction method
55
+
56
+ **Robust Fallback Chain**
57
+
58
+ For Digital PDFs:
59
+ 1. Docling - Advanced layout analysis with table/image extraction
60
+ 2. PyMuPDF - Fast text extraction with basic structure
61
+ 3. pdfplumber - Table-focused extraction
62
+ 4. Raw text - Binary extraction as last resort
63
+
64
+ For Scanned PDFs:
65
+ 1. Tesseract OCR - Text recognition
66
+ 2. PyMuPDF - Image extraction with minimal text
67
+
68
+ **Production Ready**
69
+ - 90-second timeout protection with process termination
70
+ - Databricks optimized for distributed processing
71
+ - Structured output (pages, sections, tables, images, formulas)
72
+ - Comprehensive error handling and logging
73
+
74
+ ## Installation
75
+
76
+ Basic installation (PyMuPDF + pdfplumber):
77
+ ```bash
78
+ pip install docling-extractor
79
+ ```
80
+
81
+ Full installation (includes Docling and Tesseract):
82
+ ```bash
83
+ pip install docling-extractor[full]
84
+ ```
85
+
86
+ **Note:** Tesseract requires system installation:
87
+ ```bash
88
+ # Ubuntu/Debian
89
+ sudo apt-get install tesseract-ocr
90
+
91
+ # macOS
92
+ brew install tesseract
93
+
94
+ # Windows
95
+ # Download from: https://github.com/UB-Mannheim/tesseract/wiki
96
+ ```
97
+
98
+ ## Quick Start
99
+
100
+ ### Basic Usage
101
+
102
+ ```python
103
+ from docling_extractor import extract_single_document
104
+
105
+ # Extract a document
106
+ result = extract_single_document(
107
+ input_path="/path/to/document.pdf",
108
+ output_dir="/path/to/output",
109
+ document_id="doc_001"
110
+ )
111
+
112
+ # Access extracted content
113
+ print(f"Status: {result['registry']['processing_status']}")
114
+ print(f"Pages extracted: {len(result['pages'])}")
115
+ print(f"Tables found: {len(result['tables'])}")
116
+ print(f"Tools used: {result['registry']['tools_used']}")
117
+
118
+ # Get page text
119
+ for page in result['pages']:
120
+ print(f"Page {page['page_number']}: {page['text'][:100]}...")
121
+ ```
122
+
123
+ ### Working with Different Document Types
124
+
125
+ ```python
126
+ # Digital PDF (uses Docling -> PyMuPDF -> pdfplumber chain)
127
+ result = extract_single_document(
128
+ input_path="digital_document.pdf",
129
+ output_dir="./output"
130
+ )
131
+
132
+ # Scanned PDF (uses Tesseract -> PyMuPDF chain)
133
+ result = extract_single_document(
134
+ input_path="scanned_document.pdf",
135
+ output_dir="./output"
136
+ )
137
+ ```
138
+
139
+ ### Accessing Structured Data
140
+
141
+ ```python
142
+ result = extract_single_document(
143
+ input_path="document.pdf",
144
+ output_dir="./output",
145
+ document_id="my_doc"
146
+ )
147
+
148
+ # Pages
149
+ for page in result['pages']:
150
+ print(f"Page {page['page_number']}")
151
+ print(page['text'])
152
+
153
+ # Tables
154
+ for table in result['tables']:
155
+ print(f"Table on page {table['page_number']}")
156
+ print(f"Dimensions: {table['row_count']}x{table['column_count']}")
157
+
158
+ # Images
159
+ for img in result['images']:
160
+ print(f"Image saved to: {img['image_path']}")
161
+ print(f"Size: {img['width']}x{img['height']}")
162
+
163
+ # Sections (from Docling)
164
+ for section in result['sections']:
165
+ print(f"Section type: {section['section_type']}")
166
+ print(f"Content: {section['content_text'][:100]}...")
167
+ ```
168
+
169
+ ## Output Structure
170
+
171
+ The extraction result contains:
172
+
173
+ ```python
174
+ {
175
+ 'registry': {
176
+ 'protocol_id': 'document_id',
177
+ 'document_id': 'document_id',
178
+ 'processing_status': 'success', # or 'failed'
179
+ 'page_count': 10,
180
+ 'tools_used': ['docling'], # or ['pymupdf'], ['pdfplumber'], etc.
181
+ 'error_message': '',
182
+ 'processed_at': '2024-12-31T12:00:00'
183
+ },
184
+ 'pages': [
185
+ {
186
+ 'document_id': 'doc_id',
187
+ 'protocol_id': 'doc_id',
188
+ 'page_number': 1,
189
+ 'text': 'extracted text...',
190
+ 'source_path': '/path/to/file.pdf'
191
+ }
192
+ ],
193
+ 'tables': [...],
194
+ 'images': [...],
195
+ 'sections': [...],
196
+ 'formulas': [...],
197
+ 'errors': [...]
198
+ }
199
+ ```
200
+
201
+ ## Use Cases
202
+
203
+ **Clinical Trials & Regulatory Documents**
204
+ - Extract data from clinical trial protocols
205
+ - Process regulatory submission documents
206
+ - Handle ICH-GCP compliant document structures
207
+
208
+ **Research & Academic Papers**
209
+ - Extract structured content from research papers
210
+ - Preserve table and figure information
211
+ - Handle multi-column layouts
212
+
213
+ **Financial Documents**
214
+ - Process annual reports and filings
215
+ - Extract tables from financial statements
216
+ - Handle scanned historical documents
217
+
218
+ **Enterprise Document Processing**
219
+ - Batch processing with Databricks integration
220
+ - Reliable extraction with fallback support
221
+ - Structured output for downstream NLP/ML
222
+
223
+ ## Advanced Configuration
224
+
225
+ ### Using Docling Directly
226
+
227
+ ```python
228
+ from docling_extractor import DoclingExtractor
229
+
230
+ extractor = DoclingExtractor(output_dir="./output")
231
+ result = extractor.extract(
232
+ path="document.pdf",
233
+ doc_id="my_document"
234
+ )
235
+ ```
236
+
237
+ ### Checking PDF Type
238
+
239
+ ```python
240
+ from docling_extractor import is_scanned_pdf
241
+
242
+ if is_scanned_pdf("document.pdf"):
243
+ print("Scanned PDF - will use OCR")
244
+ else:
245
+ print("Digital PDF - will use text extraction")
246
+ ```
247
+
248
+ ## Performance
249
+
250
+ Typical processing times (single-threaded):
251
+ - Digital PDF (10 pages): 5-15 seconds
252
+ - Scanned PDF (10 pages): 30-60 seconds
253
+ - Timeout: 90 seconds (configurable in code)
254
+
255
+ For Databricks distributed processing, use `extract_single_document` with Spark UDFs.
256
+
257
+ ## Requirements
258
+
259
+ **Core Dependencies:**
260
+ - Python >= 3.8
261
+ - PyMuPDF >= 1.23.0
262
+ - pdfplumber >= 0.10.0
263
+
264
+ **Optional Dependencies:**
265
+ - docling >= 2.0.0 (for advanced extraction)
266
+ - docling-core >= 2.0.0
267
+ - pytesseract >= 0.3.10 (for OCR)
268
+
269
+ ## Troubleshooting
270
+
271
+ **Docling timeout issues:**
272
+ The library includes a 90-second hard timeout with process termination. If Docling hangs, it will automatically fall back to PyMuPDF.
273
+
274
+ **OCR not working:**
275
+ Ensure Tesseract is installed system-wide. Check with:
276
+ ```bash
277
+ tesseract --version
278
+ ```
279
+
280
+ **Read-only filesystem (Databricks):**
281
+ The library is designed for Databricks' read-only site-packages. Docling OCR is disabled by default to avoid RapidOCR filesystem issues.
282
+
283
+ ## License
284
+
285
+ MIT License - see LICENSE file for details
286
+
287
+ ## Author
288
+
289
+ Nalini Panwar - [LinkedIn](https://www.linkedin.com/in/nalinipanwar/) | [GitHub](https://github.com/panwarnalini-hub)
290
+
291
+ ## Contributing
292
+
293
+ Contributions welcome. Please open an issue or submit a pull request at:
294
+ https://github.com/panwarnalini-hub/clinical-doc-pipelines
295
+
296
+ ## Acknowledgments
297
+
298
+ Built with:
299
+ - [Docling](https://github.com/DS4SD/docling) - IBM Research document understanding
300
+ - [PyMuPDF](https://pymupdf.readthedocs.io/) - Fast PDF processing
301
+ - [pdfplumber](https://github.com/jsvine/pdfplumber) - Table extraction
302
+ - [Tesseract](https://github.com/tesseract-ocr/tesseract) - OCR engine
@@ -0,0 +1,261 @@
1
+ # Docling Extractor
2
+
3
+ Production-grade document extraction library with intelligent fallback chain for robust PDF processing.
4
+
5
+ ## Overview
6
+
7
+ Docling Extractor provides a reliable pipeline for extracting text, tables, images, and structured content from PDF documents. The library automatically detects document type (scanned vs. digital) and applies the appropriate extraction method with fallback support.
8
+
9
+ ## Key Features
10
+
11
+ **Intelligent PDF Detection**
12
+ - Automatically identifies scanned vs. digital PDFs
13
+ - Routes documents to optimal extraction method
14
+
15
+ **Robust Fallback Chain**
16
+
17
+ For Digital PDFs:
18
+ 1. Docling - Advanced layout analysis with table/image extraction
19
+ 2. PyMuPDF - Fast text extraction with basic structure
20
+ 3. pdfplumber - Table-focused extraction
21
+ 4. Raw text - Binary extraction as last resort
22
+
23
+ For Scanned PDFs:
24
+ 1. Tesseract OCR - Text recognition
25
+ 2. PyMuPDF - Image extraction with minimal text
26
+
27
+ **Production Ready**
28
+ - 90-second timeout protection with process termination
29
+ - Databricks optimized for distributed processing
30
+ - Structured output (pages, sections, tables, images, formulas)
31
+ - Comprehensive error handling and logging
32
+
33
+ ## Installation
34
+
35
+ Basic installation (PyMuPDF + pdfplumber):
36
+ ```bash
37
+ pip install docling-extractor
38
+ ```
39
+
40
+ Full installation (includes Docling and Tesseract):
41
+ ```bash
42
+ pip install docling-extractor[full]
43
+ ```
44
+
45
+ **Note:** Tesseract requires system installation:
46
+ ```bash
47
+ # Ubuntu/Debian
48
+ sudo apt-get install tesseract-ocr
49
+
50
+ # macOS
51
+ brew install tesseract
52
+
53
+ # Windows
54
+ # Download from: https://github.com/UB-Mannheim/tesseract/wiki
55
+ ```
56
+
57
+ ## Quick Start
58
+
59
+ ### Basic Usage
60
+
61
+ ```python
62
+ from docling_extractor import extract_single_document
63
+
64
+ # Extract a document
65
+ result = extract_single_document(
66
+ input_path="/path/to/document.pdf",
67
+ output_dir="/path/to/output",
68
+ document_id="doc_001"
69
+ )
70
+
71
+ # Access extracted content
72
+ print(f"Status: {result['registry']['processing_status']}")
73
+ print(f"Pages extracted: {len(result['pages'])}")
74
+ print(f"Tables found: {len(result['tables'])}")
75
+ print(f"Tools used: {result['registry']['tools_used']}")
76
+
77
+ # Get page text
78
+ for page in result['pages']:
79
+ print(f"Page {page['page_number']}: {page['text'][:100]}...")
80
+ ```
81
+
82
+ ### Working with Different Document Types
83
+
84
+ ```python
85
+ # Digital PDF (uses Docling -> PyMuPDF -> pdfplumber chain)
86
+ result = extract_single_document(
87
+ input_path="digital_document.pdf",
88
+ output_dir="./output"
89
+ )
90
+
91
+ # Scanned PDF (uses Tesseract -> PyMuPDF chain)
92
+ result = extract_single_document(
93
+ input_path="scanned_document.pdf",
94
+ output_dir="./output"
95
+ )
96
+ ```
97
+
98
+ ### Accessing Structured Data
99
+
100
+ ```python
101
+ result = extract_single_document(
102
+ input_path="document.pdf",
103
+ output_dir="./output",
104
+ document_id="my_doc"
105
+ )
106
+
107
+ # Pages
108
+ for page in result['pages']:
109
+ print(f"Page {page['page_number']}")
110
+ print(page['text'])
111
+
112
+ # Tables
113
+ for table in result['tables']:
114
+ print(f"Table on page {table['page_number']}")
115
+ print(f"Dimensions: {table['row_count']}x{table['column_count']}")
116
+
117
+ # Images
118
+ for img in result['images']:
119
+ print(f"Image saved to: {img['image_path']}")
120
+ print(f"Size: {img['width']}x{img['height']}")
121
+
122
+ # Sections (from Docling)
123
+ for section in result['sections']:
124
+ print(f"Section type: {section['section_type']}")
125
+ print(f"Content: {section['content_text'][:100]}...")
126
+ ```
127
+
128
+ ## Output Structure
129
+
130
+ The extraction result contains:
131
+
132
+ ```python
133
+ {
134
+ 'registry': {
135
+ 'protocol_id': 'document_id',
136
+ 'document_id': 'document_id',
137
+ 'processing_status': 'success', # or 'failed'
138
+ 'page_count': 10,
139
+ 'tools_used': ['docling'], # or ['pymupdf'], ['pdfplumber'], etc.
140
+ 'error_message': '',
141
+ 'processed_at': '2024-12-31T12:00:00'
142
+ },
143
+ 'pages': [
144
+ {
145
+ 'document_id': 'doc_id',
146
+ 'protocol_id': 'doc_id',
147
+ 'page_number': 1,
148
+ 'text': 'extracted text...',
149
+ 'source_path': '/path/to/file.pdf'
150
+ }
151
+ ],
152
+ 'tables': [...],
153
+ 'images': [...],
154
+ 'sections': [...],
155
+ 'formulas': [...],
156
+ 'errors': [...]
157
+ }
158
+ ```
159
+
160
+ ## Use Cases
161
+
162
+ **Clinical Trials & Regulatory Documents**
163
+ - Extract data from clinical trial protocols
164
+ - Process regulatory submission documents
165
+ - Handle ICH-GCP compliant document structures
166
+
167
+ **Research & Academic Papers**
168
+ - Extract structured content from research papers
169
+ - Preserve table and figure information
170
+ - Handle multi-column layouts
171
+
172
+ **Financial Documents**
173
+ - Process annual reports and filings
174
+ - Extract tables from financial statements
175
+ - Handle scanned historical documents
176
+
177
+ **Enterprise Document Processing**
178
+ - Batch processing with Databricks integration
179
+ - Reliable extraction with fallback support
180
+ - Structured output for downstream NLP/ML
181
+
182
+ ## Advanced Configuration
183
+
184
+ ### Using Docling Directly
185
+
186
+ ```python
187
+ from docling_extractor import DoclingExtractor
188
+
189
+ extractor = DoclingExtractor(output_dir="./output")
190
+ result = extractor.extract(
191
+ path="document.pdf",
192
+ doc_id="my_document"
193
+ )
194
+ ```
195
+
196
+ ### Checking PDF Type
197
+
198
+ ```python
199
+ from docling_extractor import is_scanned_pdf
200
+
201
+ if is_scanned_pdf("document.pdf"):
202
+ print("Scanned PDF - will use OCR")
203
+ else:
204
+ print("Digital PDF - will use text extraction")
205
+ ```
206
+
207
+ ## Performance
208
+
209
+ Typical processing times (single-threaded):
210
+ - Digital PDF (10 pages): 5-15 seconds
211
+ - Scanned PDF (10 pages): 30-60 seconds
212
+ - Timeout: 90 seconds (configurable in code)
213
+
214
+ For Databricks distributed processing, use `extract_single_document` with Spark UDFs.
215
+
216
+ ## Requirements
217
+
218
+ **Core Dependencies:**
219
+ - Python >= 3.8
220
+ - PyMuPDF >= 1.23.0
221
+ - pdfplumber >= 0.10.0
222
+
223
+ **Optional Dependencies:**
224
+ - docling >= 2.0.0 (for advanced extraction)
225
+ - docling-core >= 2.0.0
226
+ - pytesseract >= 0.3.10 (for OCR)
227
+
228
+ ## Troubleshooting
229
+
230
+ **Docling timeout issues:**
231
+ The library includes a 90-second hard timeout with process termination. If Docling hangs, it will automatically fall back to PyMuPDF.
232
+
233
+ **OCR not working:**
234
+ Ensure Tesseract is installed system-wide. Check with:
235
+ ```bash
236
+ tesseract --version
237
+ ```
238
+
239
+ **Read-only filesystem (Databricks):**
240
+ The library is designed for Databricks' read-only site-packages. Docling OCR is disabled by default to avoid RapidOCR filesystem issues.
241
+
242
+ ## License
243
+
244
+ MIT License - see LICENSE file for details
245
+
246
+ ## Author
247
+
248
+ Nalini Panwar - [LinkedIn](https://www.linkedin.com/in/nalinipanwar/) | [GitHub](https://github.com/panwarnalini-hub)
249
+
250
+ ## Contributing
251
+
252
+ Contributions welcome. Please open an issue or submit a pull request at:
253
+ https://github.com/panwarnalini-hub/clinical-doc-pipelines
254
+
255
+ ## Acknowledgments
256
+
257
+ Built with:
258
+ - [Docling](https://github.com/DS4SD/docling) - IBM Research document understanding
259
+ - [PyMuPDF](https://pymupdf.readthedocs.io/) - Fast PDF processing
260
+ - [pdfplumber](https://github.com/jsvine/pdfplumber) - Table extraction
261
+ - [Tesseract](https://github.com/tesseract-ocr/tesseract) - OCR engine
@@ -0,0 +1,25 @@
1
+ """
2
+ docling_extractor - Production-grade document extraction with intelligent fallback chains.
3
+
4
+ Main components:
5
+ - extract_single_document: Primary entry point for document processing
6
+ - DoclingExtractor: Handles digital PDF extraction with Docling
7
+ - is_scanned_pdf: Detects whether a PDF is scanned or digital
8
+
9
+ For full documentation, see: https://github.com/panwarnalini-hub/clinical-doc-pipelines
10
+ """
11
+
12
+ __version__ = "1.0.0"
13
+ __author__ = "Nalini Panwar"
14
+
15
+ from .extractor import (
16
+ extract_single_document,
17
+ DoclingExtractor,
18
+ is_scanned_pdf,
19
+ )
20
+
21
+ __all__ = [
22
+ "extract_single_document",
23
+ "DoclingExtractor",
24
+ "is_scanned_pdf",
25
+ ]