解析器 API 参考¶
所有 Doctra 解析器的完整 API 文档。
StructuredPDFParser¶
用于全面 PDF 文档处理的基础解析器。
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.
Features automatic detection and merging of tables split across pages
using proximity detection and LSD-based structure analysis.
:param vlm: VLM engine instance (VLMStructuredExtractor). If None, VLM processing is disabled.
: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_engine: OCR engine instance (PytesseractOCREngine or PaddleOCREngine).
If None, creates a default PytesseractOCREngine with lang="eng", psm=4, oem=3.
:param box_separator: Separator between text boxes in output (default: "
") :param merge_split_tables: Whether to detect and merge split tables (default: False) :param bottom_threshold_ratio: Ratio for "too close to bottom" detection (default: 0.20) :param top_threshold_ratio: Ratio for "too close to top" detection (default: 0.10) :param max_gap_ratio: Maximum allowed gap between tables (default: 0.05) :param column_alignment_tolerance: Pixel tolerance for column alignment (default: 10.0) :param min_merge_confidence: Minimum confidence score for merging (default: 0.7)
Source code in doctra/parsers/structured_pdf_parser.py
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 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
__init__(*, vlm=None, layout_model_name='PP-DocLayout_plus-L', dpi=200, min_score=0.0, ocr_engine=None, box_separator='\n', merge_split_tables=False, bottom_threshold_ratio=0.2, top_threshold_ratio=0.15, max_gap_ratio=0.25, column_alignment_tolerance=10.0, min_merge_confidence=0.65)
¶
Initialize the StructuredPDFParser with processing configuration.
Also suppresses noisy DEBUG logs from external libraries.
:param vlm: VLM engine instance (VLMStructuredExtractor). If None, VLM processing is disabled.
: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_engine: OCR engine instance (PytesseractOCREngine or PaddleOCREngine).
If None, creates a default PytesseractOCREngine with lang="eng", psm=4, oem=3.
:param box_separator: Separator between text boxes in output (default: "
") :param merge_split_tables: Whether to detect and merge split tables (default: False) :param bottom_threshold_ratio: Ratio for "too close to bottom" detection (default: 0.20) :param top_threshold_ratio: Ratio for "too close to top" detection (default: 0.15) :param max_gap_ratio: Maximum allowed gap between tables (default: 0.25, accounts for headers/footers) :param column_alignment_tolerance: Pixel tolerance for column alignment (default: 10.0) :param min_merge_confidence: Minimum confidence score for merging (default: 0.65)
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
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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
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
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 | |
EnhancedPDFParser¶
具有图像恢复功能的增强解析器。
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 vlm: VLM engine instance (VLMStructuredExtractor). If None, VLM processing is disabled.
: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_engine: OCR engine instance (PytesseractOCREngine or PaddleOCREngine).
If None, creates a default PytesseractOCREngine with lang="eng", psm=4, oem=3.
:param box_separator: Separator between text boxes in output (default: "
") :param merge_split_tables: Whether to detect and merge split tables (default: False) :param bottom_threshold_ratio: Ratio for "too close to bottom" detection (default: 0.20) :param top_threshold_ratio: Ratio for "too close to top" detection (default: 0.15) :param max_gap_ratio: Maximum allowed gap between tables (default: 0.25) :param column_alignment_tolerance: Pixel tolerance for column alignment (default: 10.0) :param min_merge_confidence: Minimum confidence score for merging (default: 0.65)
Source code in doctra/parsers/enhanced_pdf_parser.py
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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
__init__(*, use_image_restoration=True, restoration_task='appearance', restoration_device=None, restoration_dpi=200, vlm=None, layout_model_name='PP-DocLayout_plus-L', dpi=200, min_score=0.0, ocr_engine=None, box_separator='\n', merge_split_tables=False, bottom_threshold_ratio=0.2, top_threshold_ratio=0.15, max_gap_ratio=0.25, column_alignment_tolerance=10.0, min_merge_confidence=0.65)
¶
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¶
用于提取图表和表格的专业解析器。
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 vlm: VLM engine instance (VLMStructuredExtractor). If None, VLM processing is disabled. :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 merge_split_tables: Whether to detect and merge split tables (default: False) :param bottom_threshold_ratio: Ratio for "too close to bottom" detection (default: 0.20) :param top_threshold_ratio: Ratio for "too close to top" detection (default: 0.15) :param max_gap_ratio: Maximum allowed gap between tables (default: 0.25, accounts for headers/footers) :param column_alignment_tolerance: Pixel tolerance for column alignment (default: 10.0) :param min_merge_confidence: Minimum confidence score for merging (default: 0.65)
Source code in doctra/parsers/table_chart_extractor.py
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 | |
__init__(*, extract_charts=True, extract_tables=True, vlm=None, layout_model_name='PP-DocLayout_plus-L', dpi=200, min_score=0.0, merge_split_tables=False, bottom_threshold_ratio=0.2, top_threshold_ratio=0.15, max_gap_ratio=0.25, column_alignment_tolerance=10.0, min_merge_confidence=0.65)
¶
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 vlm: VLM engine instance (VLMStructuredExtractor). If None, VLM processing is disabled. :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 merge_split_tables: Whether to detect and merge split tables (default: False) :param bottom_threshold_ratio: Ratio for "too close to bottom" detection (default: 0.20) :param top_threshold_ratio: Ratio for "too close to top" detection (default: 0.15) :param max_gap_ratio: Maximum allowed gap between tables (default: 0.25, accounts for headers/footers) :param column_alignment_tolerance: Pixel tolerance for column alignment (default: 10.0) :param min_merge_confidence: Minimum confidence score for merging (default: 0.65)
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
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 | |
PaddleOCRVLPDFParser¶
使用 PaddleOCRVL 视觉语言模型的端到端文档解析器。
doctra.parsers.paddleocr_vl_parser.PaddleOCRVLPDFParser
¶
PDF Parser using PaddleOCRVL for end-to-end document parsing.
Combines PaddleOCRVL's vision-language model capabilities with: - DocRes image restoration for enhanced document quality - Split table detection and merging across pages
:param use_image_restoration: Whether to apply DocRes image restoration (default: True) :param restoration_task: DocRes task to use (default: "appearance") :param restoration_device: Device for DocRes processing (default: None for auto-detect) :param restoration_dpi: DPI for restoration processing (default: 200) :param use_chart_recognition: Enable chart recognition in PaddleOCRVL (default: True) :param use_doc_orientation_classify: Enable document orientation classification (default: False) :param use_doc_unwarping: Enable document unwarping (default: False) :param use_layout_detection: Enable layout detection (default: True) :param device: Device for PaddleOCRVL processing ("gpu" or "cpu", default: "gpu") :param merge_split_tables: Whether to detect and merge split tables (default: True) :param bottom_threshold_ratio: Ratio for "too close to bottom" detection (default: 0.20) :param top_threshold_ratio: Ratio for "too close to top" detection (default: 0.15) :param max_gap_ratio: Maximum allowed gap between tables (default: 0.25) :param column_alignment_tolerance: Pixel tolerance for column alignment (default: 10.0) :param min_merge_confidence: Minimum confidence score for merging (default: 0.65)
Source code in doctra/parsers/paddleocr_vl_parser.py
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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | |
__init__(*, use_image_restoration=True, restoration_task='appearance', restoration_device=None, restoration_dpi=200, use_chart_recognition=True, use_doc_orientation_classify=False, use_doc_unwarping=False, use_layout_detection=True, device='gpu', merge_split_tables=True, bottom_threshold_ratio=0.2, top_threshold_ratio=0.15, max_gap_ratio=0.25, column_alignment_tolerance=10.0, min_merge_confidence=0.65)
¶
Initialize the PaddleOCRVL PDF Parser.
Source code in doctra/parsers/paddleocr_vl_parser.py
parse(pdf_path, output_dir=None)
¶
Parse a PDF document using PaddleOCRVL.
:param pdf_path: Path to the input PDF file :param output_dir: Output directory (if None, uses default) :return: None
Source code in doctra/parsers/paddleocr_vl_parser.py
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 | |
StructuredDOCXParser¶
用于 Microsoft Word 文档(.docx 文件)的全面解析器。
doctra.parsers.structured_docx_parser.StructuredDOCXParser
¶
Comprehensive DOCX parser for extracting all types of content.
Processes DOCX documents to extract text, tables, images, and figures. Supports structured data extraction and optional VLM processing for enhanced content analysis.
:param vlm: VLM engine instance (VLMStructuredExtractor). If None, VLM processing is disabled. :param extract_images: Whether to extract embedded images (default: True) :param preserve_formatting: Whether to preserve text formatting in output (default: True) :param table_detection: Whether to detect and extract tables (default: True)
Source code in doctra/parsers/structured_docx_parser.py
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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 | |
__init__(*, vlm=None, extract_images=True, preserve_formatting=True, table_detection=True, export_excel=True)
¶
Initialize the StructuredDOCXParser with processing configuration.
:param vlm: VLM engine instance (VLMStructuredExtractor). If None, VLM processing is disabled. :param extract_images: Whether to extract embedded images (default: True) :param preserve_formatting: Whether to preserve text formatting in output (default: True) :param table_detection: Whether to detect and extract tables (default: True) :param export_excel: Whether to export tables to Excel file (default: True)
Source code in doctra/parsers/structured_docx_parser.py
parse(docx_path)
¶
Parse a DOCX document and extract all content.
:param docx_path: Path to the DOCX file to parse
Source code in doctra/parsers/structured_docx_parser.py
快速参考¶
StructuredPDFParser¶
from doctra import StructuredPDFParser
from doctra.engines.ocr import PytesseractOCREngine, PaddleOCREngine
from doctra.engines.vlm.service import VLMStructuredExtractor
# 初始化 OCR 引擎(可选 - 如果为 None 则默认为 PyTesseract)
ocr_engine = PytesseractOCREngine(lang="eng", psm=4, oem=3)
# 初始化 VLM 引擎(可选 - None 以禁用 VLM)
vlm_engine = VLMStructuredExtractor(
vlm_provider="openai",
vlm_model="gpt-4o", # 可选
api_key="your-api-key"
)
parser = StructuredPDFParser(
# 布局检测
layout_model_name: str = "PP-DocLayout_plus-L",
dpi: int = 200,
min_score: float = 0.0,
# OCR 引擎(传递初始化的引擎实例)
ocr_engine: Optional[Union[PytesseractOCREngine, PaddleOCREngine]] = None,
# VLM 引擎(传递初始化的引擎实例)
vlm: Optional[VLMStructuredExtractor] = None,
# 分割表格合并
merge_split_tables: bool = False,
bottom_threshold_ratio: float = 0.20,
top_threshold_ratio: float = 0.15,
max_gap_ratio: float = 0.25,
column_alignment_tolerance: float = 10.0,
min_merge_confidence: float = 0.65,
# 输出设置
box_separator: str = "\n"
)
# 解析文档
parser.parse(
pdf_path: str,
output_base_dir: str = "outputs"
)
# 可视化布局
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
from doctra.engines.vlm.service import VLMStructuredExtractor
# 初始化 VLM 引擎(可选)
vlm_engine = VLMStructuredExtractor(
vlm_provider="openai",
api_key="your-api-key"
)
parser = EnhancedPDFParser(
# 图像恢复
use_image_restoration: bool = True,
restoration_task: str = "appearance",
restoration_device: str = None,
restoration_dpi: int = 200,
# VLM 引擎(传递初始化的引擎实例)
vlm: Optional[VLMStructuredExtractor] = None,
# 布局检测
layout_model_name: str = "PP-DocLayout_plus-L",
dpi: int = 200,
min_score: float = 0.0,
# OCR 引擎(可选)
ocr_engine: Optional[Union[PytesseractOCREngine, PaddleOCREngine]] = None,
# 分割表格合并
merge_split_tables: bool = False,
bottom_threshold_ratio: float = 0.20,
top_threshold_ratio: float = 0.15,
max_gap_ratio: float = 0.25,
column_alignment_tolerance: float = 10.0,
min_merge_confidence: float = 0.65,
# 输出设置
box_separator: str = "\n"
)
# 使用增强功能解析
parser.parse(
pdf_path: str,
output_base_dir: str = "outputs"
)
ChartTablePDFParser¶
from doctra import ChartTablePDFParser
from doctra.engines.vlm.service import VLMStructuredExtractor
# 初始化 VLM 引擎(可选)
vlm_engine = VLMStructuredExtractor(
vlm_provider="openai",
api_key="your-api-key"
)
parser = ChartTablePDFParser(
# 提取设置
extract_charts: bool = True,
extract_tables: bool = True,
# VLM 引擎(传递初始化的引擎实例)
vlm: Optional[VLMStructuredExtractor] = None,
# 布局检测
layout_model_name: str = "PP-DocLayout_plus-L",
dpi: int = 200,
min_score: float = 0.0,
# 分割表格合并
merge_split_tables: bool = False,
bottom_threshold_ratio: float = 0.20,
top_threshold_ratio: float = 0.15,
max_gap_ratio: float = 0.25,
column_alignment_tolerance: float = 10.0,
min_merge_confidence: float = 0.65,
)
# 提取图表/表格
parser.parse(
pdf_path: str,
output_base_dir: str = "outputs"
)
StructuredDOCXParser¶
from doctra import StructuredDOCXParser
from doctra.engines.vlm.service import VLMStructuredExtractor
# 初始化 VLM 引擎(可选)
vlm_engine = VLMStructuredExtractor(
vlm_provider="openai",
api_key="your-api-key"
)
parser = StructuredDOCXParser(
# VLM 引擎(传递初始化的引擎实例)
vlm: Optional[VLMStructuredExtractor] = None,
# 处理选项
extract_images: bool = True,
preserve_formatting: bool = True,
table_detection: bool = True,
export_excel: bool = True
)
# 解析 DOCX 文档
parser.parse(
docx_path: str
)
参数参考¶
布局检测参数¶
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
layout_model_name |
str | "PP-DocLayout_plus-L" | PaddleOCR 布局检测模型 |
dpi |
int | 200 | 渲染 PDF 页面的图像分辨率 |
min_score |
float | 0.0 | 检测元素的最小置信度分数 |
OCR 参数¶
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
ocr_engine |
Optional[Union[PytesseractOCREngine, PaddleOCREngine]] |
None |
OCR 引擎实例。如果为 None,则创建默认的 PytesseractOCREngine,使用 lang="eng", psm=4, oem=3 |
OCR 引擎配置:
OCR 引擎必须在外部初始化并传递给解析器。这使用依赖注入模式以实现更清晰的 API 设计。
PytesseractOCREngine 参数:
- lang (str, 默认: "eng"):Tesseract 语言代码(例如 "eng"、"fra"、"spa"、"deu",或多个:"eng+fra")
- psm (int, 默认: 4):页面分割模式(3=自动,4=单列,6=统一块,11=稀疏文本,12=带 OSD 的稀疏)
- oem (int, 默认: 3):OCR 引擎模式(0=传统,1=神经网络 LSTM,3=默认两者)
- extra_config (str, 默认: ""):额外的 Tesseract 配置字符串
PaddleOCREngine 参数:
- device (str, 默认: "gpu"):OCR 处理设备("cpu" 或 "gpu")
- use_doc_orientation_classify (bool, 默认: False):启用文档方向分类
- use_doc_unwarping (bool, 默认: False):启用文本图像校正
- use_textline_orientation (bool, 默认: False):启用文本行方向分类
示例:
from doctra.engines.ocr import PytesseractOCREngine, PaddleOCREngine
# PyTesseract
tesseract_ocr = PytesseractOCREngine(lang="eng", psm=4, oem=3)
parser = StructuredPDFParser(ocr_engine=tesseract_ocr)
# PaddleOCR
paddle_ocr = PaddleOCREngine(device="gpu")
parser = StructuredPDFParser(ocr_engine=paddle_ocr)
注意:使用 PaddleOCR 时,默认使用 PaddleOCR 3.0 的 PP-OCRv5_server 模型。模型在首次使用时自动下载。
VLM 参数¶
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
vlm |
Optional[VLMStructuredExtractor] |
None |
VLM 引擎实例。如果为 None,则禁用 VLM 处理。 |
VLM 引擎配置:
VLM 引擎必须在外部初始化并传递给解析器。这使用依赖注入模式以实现更清晰的 API 设计。
VLMStructuredExtractor 参数:
- vlm_provider (str, 必需):要使用的 VLM 提供商("openai"、"gemini"、"anthropic"、"openrouter"、"qianfan"、"ollama")
- vlm_model (str, 可选):要使用的模型名称(默认为提供商特定的默认值)
- api_key (str, 可选):VLM 提供商的 API 密钥(除 Ollama 外的所有提供商都需要)
示例:
from doctra.engines.vlm.service import VLMStructuredExtractor
# 初始化 VLM 引擎
vlm_engine = VLMStructuredExtractor(
vlm_provider="openai",
vlm_model="gpt-4o", # 可选
api_key="your-api-key"
)
# 传递给解析器
parser = StructuredPDFParser(vlm=vlm_engine)
图像恢复参数¶
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
use_image_restoration |
bool | True | 启用图像恢复 |
restoration_task |
str | "appearance" | 恢复任务类型 |
restoration_device |
str | None | 设备:"cuda"、"cpu" 或 None(自动检测) |
restoration_dpi |
int | 200 | 恢复处理的 DPI |
分割表格合并参数¶
适用于 StructuredPDFParser 和 EnhancedPDFParser。
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
merge_split_tables |
bool | False | 启用自动检测和合并跨页分割的表格 |
bottom_threshold_ratio |
float | 0.20 | 检测页面底部附近表格的比率(0-1)。在此比率内距离底部的表格被视为候选。 |
top_threshold_ratio |
float | 0.15 | 检测页面顶部附近表格的比率(0-1)。在此比率内距离顶部的表格被视为候选。 |
max_gap_ratio |
float | 0.25 | 表格段之间的最大允许间隙,作为页面高度的比率。考虑页眉、页脚和页边距。 |
column_alignment_tolerance |
float | 10.0 | 比较表格结构时列对齐验证的像素容差。 |
min_merge_confidence |
float | 0.65 | 合并两个表格段所需的最小置信度分数(0-1)。值越高越保守。 |
提取参数¶
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
extract_charts |
bool | True | 提取图表元素 |
extract_tables |
bool | True | 提取表格元素 |
DOCX 处理参数¶
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
extract_images |
bool | True | 从 DOCX 中提取嵌入的图像 |
preserve_formatting |
bool | True | 在输出中保留文本格式 |
table_detection |
bool | True | 检测并提取表格 |
export_excel |
bool | True | 将表格导出到 Excel 文件 |
输出参数¶
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
box_separator |
str | "\n" | 检测元素之间的分隔符 |
返回值¶
parse() 方法¶
返回:None
在指定的 output_base_dir 中生成输出文件:
outputs/
└── <document_name>/
├── full_parse/ # 或 'enhanced_parse/'、'structured_parsing/'
│ ├── result.md
│ ├── result.html
│ ├── tables.xlsx # 如果启用 VLM
│ ├── tables.html # 如果启用 VLM
│ ├── vlm_items.json # 如果启用 VLM
│ └── images/
│ ├── figures/
│ ├── charts/
│ └── tables/
对于 DOCX 解析,生成:
outputs/
└── <document_name>/
├── document.md
├── document.html
├── tables.xlsx # 带目录
└── images/
├── image1.png
├── image2.jpg
└── ...
display_pages_with_boxes() 方法¶
返回:None
显示或保存布局检测的可视化。
错误处理¶
所有解析器可能引发:
FileNotFoundError:未找到 PDF 文件ValueError:无效的参数值RuntimeError:处理错误(例如,未找到 Poppler)APIError:VLM API 错误(启用 VLM 时)
错误处理示例:
from doctra import StructuredPDFParser
parser = StructuredPDFParser()
try:
parser.parse("document.pdf")
except FileNotFoundError:
print("未找到 PDF 文件!")
except ValueError as e:
print(f"无效参数:{e}")
except RuntimeError as e:
print(f"处理错误:{e}")
示例¶
有关详细使用示例,请参阅示例部分。