Parsers API Reference¶
Complete API documentation for all Doctra parsers.
StructuredPDFParser¶
The base parser for comprehensive PDF document processing.
doctra.parsers.structured_pdf_parser.StructuredPDFParser
¶
Comprehensive PDF parser for extracting all types of content.
Processes PDF documents to extract text, tables, charts, and figures.
Supports OCR for text extraction and optional VLM processing for
converting visual elements into structured data.
:param use_vlm: Whether to use VLM for structured data extraction (default: False)
:param vlm_provider: VLM provider to use ("gemini", "openai", "anthropic", or "openrouter", default: "gemini")
:param vlm_model: Model name to use (defaults to provider-specific defaults)
:param vlm_api_key: API key for VLM provider (required if use_vlm is True)
:param layout_model_name: Layout detection model name (default: "PP-DocLayout_plus-L")
:param dpi: DPI for PDF rendering (default: 200)
:param min_score: Minimum confidence score for layout detection (default: 0.0)
:param ocr_lang: OCR language code (default: "eng")
:param ocr_psm: Tesseract page segmentation mode (default: 4)
:param ocr_oem: Tesseract OCR engine mode (default: 3)
:param ocr_extra_config: Additional Tesseract configuration (default: "")
:param box_separator: Separator between text boxes in output (default: "
")
Source code in doctra/parsers/structured_pdf_parser.py
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
__init__(*, use_vlm=False, vlm_provider='gemini', vlm_model=None, vlm_api_key=None, layout_model_name='PP-DocLayout_plus-L', dpi=200, min_score=0.0, ocr_lang='eng', ocr_psm=4, ocr_oem=3, ocr_extra_config='', box_separator='\n')
¶
Initialize the StructuredPDFParser with processing configuration.
:param use_vlm: Whether to use VLM for structured data extraction (default: False)
:param vlm_provider: VLM provider to use ("gemini", "openai", "anthropic", or "openrouter", default: "gemini")
:param vlm_model: Model name to use (defaults to provider-specific defaults)
:param vlm_api_key: API key for VLM provider (required if use_vlm is True)
:param layout_model_name: Layout detection model name (default: "PP-DocLayout_plus-L")
:param dpi: DPI for PDF rendering (default: 200)
:param min_score: Minimum confidence score for layout detection (default: 0.0)
:param ocr_lang: OCR language code (default: "eng")
:param ocr_psm: Tesseract page segmentation mode (default: 4)
:param ocr_oem: Tesseract OCR engine mode (default: 3)
:param ocr_extra_config: Additional Tesseract configuration (default: "")
:param box_separator: Separator between text boxes in output (default: "
")
Source code in doctra/parsers/structured_pdf_parser.py
display_pages_with_boxes(pdf_path, num_pages=3, cols=2, page_width=800, spacing=40, save_path=None)
¶
Display the first N pages of a PDF with bounding boxes and labels overlaid in a modern grid layout.
Creates a visualization showing layout detection results with bounding boxes, labels, and confidence scores overlaid on the PDF pages in a grid format.
:param pdf_path: Path to the input PDF file :param num_pages: Number of pages to display (default: 3) :param cols: Number of columns in the grid layout (default: 2) :param page_width: Width to resize each page to in pixels (default: 800) :param spacing: Spacing between pages in pixels (default: 40) :param save_path: Optional path to save the visualization (if None, displays only) :return: None
Source code in doctra/parsers/structured_pdf_parser.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
parse(pdf_path)
¶
Parse a PDF document and extract all content types.
:param pdf_path: Path to the input PDF file :return: None
Source code in doctra/parsers/structured_pdf_parser.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
EnhancedPDFParser¶
Enhanced parser with image restoration capabilities.
doctra.parsers.enhanced_pdf_parser.EnhancedPDFParser
¶
Bases: StructuredPDFParser
Enhanced PDF Parser with Image Restoration capabilities.
Extends the StructuredPDFParser with DocRes image restoration to improve
document quality before processing. This is particularly useful for:
- Scanned documents with shadows or distortion
- Low-quality PDFs that need enhancement
- Documents with perspective issues
:param use_image_restoration: Whether to apply DocRes image restoration (default: True)
:param restoration_task: DocRes task to use ("dewarping", "deshadowing", "appearance", "deblurring", "binarization", "end2end", default: "appearance")
:param restoration_device: Device for DocRes processing ("cuda", "cpu", or None for auto-detect, default: None)
:param restoration_dpi: DPI for restoration processing (default: 200)
:param use_vlm: Whether to use VLM for structured data extraction (default: False)
:param vlm_provider: VLM provider to use ("gemini", "openai", "anthropic", or "openrouter", default: "gemini")
:param vlm_model: Model name to use (defaults to provider-specific defaults)
:param vlm_api_key: API key for VLM provider (required if use_vlm is True)
:param layout_model_name: Layout detection model name (default: "PP-DocLayout_plus-L")
:param dpi: DPI for PDF rendering (default: 200)
:param min_score: Minimum confidence score for layout detection (default: 0.0)
:param ocr_lang: OCR language code (default: "eng")
:param ocr_psm: Tesseract page segmentation mode (default: 4)
:param ocr_oem: Tesseract OCR engine mode (default: 3)
:param ocr_extra_config: Additional Tesseract configuration (default: "")
:param box_separator: Separator between text boxes in output (default: "
")
Source code in doctra/parsers/enhanced_pdf_parser.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
|
__init__(*, use_image_restoration=True, restoration_task='appearance', restoration_device=None, restoration_dpi=200, use_vlm=False, vlm_provider='gemini', vlm_model=None, vlm_api_key=None, layout_model_name='PP-DocLayout_plus-L', dpi=200, min_score=0.0, ocr_lang='eng', ocr_psm=4, ocr_oem=3, ocr_extra_config='', box_separator='\n')
¶
Initialize the Enhanced PDF Parser with image restoration capabilities.
Source code in doctra/parsers/enhanced_pdf_parser.py
get_restoration_info()
¶
Get information about the current restoration configuration.
:return: Dictionary with restoration settings and status
Source code in doctra/parsers/enhanced_pdf_parser.py
parse(pdf_path, enhanced_output_dir=None)
¶
Parse a PDF document with optional image restoration.
:param pdf_path: Path to the input PDF file :param enhanced_output_dir: Directory for enhanced images (if None, uses default) :return: None
Source code in doctra/parsers/enhanced_pdf_parser.py
restore_pdf_only(pdf_path, output_path=None, task=None)
¶
Apply DocRes restoration to a PDF without parsing.
:param pdf_path: Path to the input PDF file :param output_path: Path for the enhanced PDF (if None, auto-generates) :param task: DocRes restoration task (if None, uses instance default) :return: Path to the enhanced PDF or None if failed
Source code in doctra/parsers/enhanced_pdf_parser.py
ChartTablePDFParser¶
Specialized parser for extracting charts and tables.
doctra.parsers.table_chart_extractor.ChartTablePDFParser
¶
Specialized PDF parser for extracting charts and tables.
Focuses specifically on chart and table extraction from PDF documents, with optional VLM (Vision Language Model) processing to convert visual elements into structured data.
:param extract_charts: Whether to extract charts from the document (default: True) :param extract_tables: Whether to extract tables from the document (default: True) :param use_vlm: Whether to use VLM for structured data extraction (default: False) :param vlm_provider: VLM provider to use ("gemini", "openai", "anthropic", or "openrouter", default: "gemini") :param vlm_model: Model name to use (defaults to provider-specific defaults) :param vlm_api_key: API key for VLM provider (required if use_vlm is True) :param layout_model_name: Layout detection model name (default: "PP-DocLayout_plus-L") :param dpi: DPI for PDF rendering (default: 200) :param min_score: Minimum confidence score for layout detection (default: 0.0)
Source code in doctra/parsers/table_chart_extractor.py
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
__init__(*, extract_charts=True, extract_tables=True, use_vlm=False, vlm_provider='gemini', vlm_model=None, vlm_api_key=None, layout_model_name='PP-DocLayout_plus-L', dpi=200, min_score=0.0)
¶
Initialize the ChartTablePDFParser with extraction configuration.
:param extract_charts: Whether to extract charts from the document (default: True) :param extract_tables: Whether to extract tables from the document (default: True) :param use_vlm: Whether to use VLM for structured data extraction (default: False) :param vlm_provider: VLM provider to use ("gemini", "openai", "anthropic", or "openrouter", default: "gemini") :param vlm_model: Model name to use (defaults to provider-specific defaults) :param vlm_api_key: API key for VLM provider (required if use_vlm is True) :param layout_model_name: Layout detection model name (default: "PP-DocLayout_plus-L") :param dpi: DPI for PDF rendering (default: 200) :param min_score: Minimum confidence score for layout detection (default: 0.0)
Source code in doctra/parsers/table_chart_extractor.py
parse(pdf_path, output_base_dir='outputs')
¶
Parse a PDF document and extract charts and/or tables.
:param pdf_path: Path to the input PDF file :param output_base_dir: Base directory for output files (default: "outputs") :return: None
Source code in doctra/parsers/table_chart_extractor.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
|
Quick Reference¶
StructuredPDFParser¶
from doctra import StructuredPDFParser
parser = StructuredPDFParser(
# Layout Detection
layout_model_name: str = "PP-DocLayout_plus-L",
dpi: int = 200,
min_score: float = 0.0,
# OCR Settings
ocr_lang: str = "eng",
ocr_psm: int = 4,
ocr_oem: int = 3,
ocr_extra_config: str = "",
# VLM Settings
use_vlm: bool = False,
vlm_provider: str = None,
vlm_api_key: str = None,
vlm_model: str = None,
# Output Settings
box_separator: str = "\n"
)
# Parse document
parser.parse(
pdf_path: str,
output_base_dir: str = "outputs"
)
# Visualize layout
parser.display_pages_with_boxes(
pdf_path: str,
num_pages: int = 3,
cols: int = 2,
page_width: int = 800,
spacing: int = 40,
save_path: str = None
)
EnhancedPDFParser¶
from doctra import EnhancedPDFParser
parser = EnhancedPDFParser(
# Image Restoration
use_image_restoration: bool = True,
restoration_task: str = "appearance",
restoration_device: str = None,
restoration_dpi: int = 200,
# All StructuredPDFParser parameters...
)
# Parse with enhancement
parser.parse(
pdf_path: str,
output_base_dir: str = "outputs"
)
ChartTablePDFParser¶
from doctra import ChartTablePDFParser
parser = ChartTablePDFParser(
# Extraction Settings
extract_charts: bool = True,
extract_tables: bool = True,
# VLM Settings
use_vlm: bool = False,
vlm_provider: str = None,
vlm_api_key: str = None,
vlm_model: str = None,
# Layout Detection
layout_model_name: str = "PP-DocLayout_plus-L",
dpi: int = 200,
min_score: float = 0.0
)
# Extract charts/tables
parser.parse(
pdf_path: str,
output_base_dir: str = "outputs"
)
Parameter Reference¶
Layout Detection Parameters¶
Parameter | Type | Default | Description |
---|---|---|---|
layout_model_name |
str | "PP-DocLayout_plus-L" | PaddleOCR layout detection model |
dpi |
int | 200 | Image resolution for rendering PDF pages |
min_score |
float | 0.0 | Minimum confidence score for detected elements |
OCR Parameters¶
Parameter | Type | Default | Description |
---|---|---|---|
ocr_lang |
str | "eng" | Tesseract language code |
ocr_psm |
int | 4 | Page segmentation mode |
ocr_oem |
int | 3 | OCR engine mode |
ocr_extra_config |
str | "" | Additional Tesseract configuration |
VLM Parameters¶
Parameter | Type | Default | Description |
---|---|---|---|
use_vlm |
bool | False | Enable VLM processing |
vlm_provider |
str | None | Provider: "openai", "gemini", "anthropic", "openrouter" |
vlm_api_key |
str | None | API key for the VLM provider |
vlm_model |
str | None | Specific model to use (provider-dependent) |
Image Restoration Parameters¶
Parameter | Type | Default | Description |
---|---|---|---|
use_image_restoration |
bool | True | Enable image restoration |
restoration_task |
str | "appearance" | Restoration task type |
restoration_device |
str | None | Device: "cuda", "cpu", or None (auto-detect) |
restoration_dpi |
int | 200 | DPI for restoration processing |
Extraction Parameters¶
Parameter | Type | Default | Description |
---|---|---|---|
extract_charts |
bool | True | Extract chart elements |
extract_tables |
bool | True | Extract table elements |
Output Parameters¶
Parameter | Type | Default | Description |
---|---|---|---|
box_separator |
str | "\n" | Separator between detected elements |
Return Values¶
parse() Method¶
Returns: None
Generates output files in the specified output_base_dir
:
outputs/
└── <document_name>/
├── full_parse/ # or 'enhanced_parse/', 'structured_parsing/'
│ ├── result.md
│ ├── result.html
│ ├── tables.xlsx # If VLM enabled
│ ├── tables.html # If VLM enabled
│ ├── vlm_items.json # If VLM enabled
│ └── images/
│ ├── figures/
│ ├── charts/
│ └── tables/
display_pages_with_boxes() Method¶
Returns: None
Displays or saves visualization of layout detection.
Error Handling¶
All parsers may raise:
FileNotFoundError
: PDF file not foundValueError
: Invalid parameter valuesRuntimeError
: Processing errors (e.g., Poppler not found)APIError
: VLM API errors (when VLM enabled)
Example error handling:
from doctra import StructuredPDFParser
parser = StructuredPDFParser()
try:
parser.parse("document.pdf")
except FileNotFoundError:
print("PDF file not found!")
except ValueError as e:
print(f"Invalid parameter: {e}")
except RuntimeError as e:
print(f"Processing error: {e}")
Examples¶
See the Examples section for detailed usage examples.