API Reference
This section provides detailed documentation for all the classes and methods in Natural PDF.
Core Classes
natural_pdf
Natural PDF - A more intuitive interface for working with PDFs.
Classes
natural_pdf.ConfigSection
A configuration section that holds key-value option pairs.
Source code in natural_pdf/__init__.py
41 42 43 44 45 46 47 48 49 | |
natural_pdf.Flow
Bases: Visualizable
Defines a logical flow or sequence of physical Page or Region objects.
A Flow represents a continuous logical document structure that spans across multiple pages or regions, enabling operations on content that flows across boundaries. This is essential for handling multi-page tables, articles that span columns, or any content that requires reading order across segments.
Flows specify arrangement (vertical/horizontal) and alignment rules to create a unified coordinate system for element extraction and text processing. They enable natural-pdf to treat fragmented content as a single continuous area for analysis and extraction operations.
The Flow system is particularly useful for: - Multi-page tables that break across page boundaries - Multi-column articles with complex reading order - Forms that span multiple pages - Any content requiring logical continuation across segments
Attributes:
| Name | Type | Description |
|---|---|---|
segments |
List[Region]
|
List of Page or Region objects in flow order. |
arrangement |
Literal['vertical', 'horizontal']
|
Primary flow direction ('vertical' or 'horizontal'). |
alignment |
Literal['start', 'center', 'end', 'top', 'left', 'bottom', 'right']
|
Cross-axis alignment for segments of different sizes. |
segment_gap |
float
|
Virtual gap between segments in PDF points. |
Example
Multi-page table flow:
pdf = npdf.PDF("multi_page_table.pdf")
# Create flow for table spanning pages 2-4
table_flow = Flow(
segments=[pdf.pages[1], pdf.pages[2], pdf.pages[3]],
arrangement='vertical',
alignment='left',
segment_gap=10.0
)
# Extract table as if it were continuous
table_data = table_flow.extract_table()
text_content = table_flow.get_text()
Multi-column article flow:
page = pdf.pages[0]
left_column = page.region(0, 0, 300, page.height)
right_column = page.region(320, 0, page.width, page.height)
# Create horizontal flow for columns
article_flow = Flow(
segments=[left_column, right_column],
arrangement='horizontal',
alignment='top'
)
# Read in proper order
article_text = article_flow.get_text()
Note
Flows create virtual coordinate systems that map element positions across segments, enabling spatial navigation and element selection to work seamlessly across boundaries.
Source code in natural_pdf/flows/flow.py
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 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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 | |
Functions
natural_pdf.Flow.__init__(segments, arrangement, alignment='start', segment_gap=0.0)
Initializes a Flow object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
segments
|
Union[List[Union[Page, Region]], PageCollection]
|
An ordered list of natural_pdf.core.page.Page or natural_pdf.elements.region.Region objects that constitute the flow, or a PageCollection containing pages. |
required |
arrangement
|
Literal['vertical', 'horizontal']
|
The primary direction of the flow. - "vertical": Segments are stacked top-to-bottom. - "horizontal": Segments are arranged left-to-right. |
required |
alignment
|
Literal['start', 'center', 'end', 'top', 'left', 'bottom', 'right']
|
How segments are aligned on their cross-axis if they have differing dimensions. For a "vertical" arrangement: - "left" (or "start"): Align left edges. - "center": Align centers. - "right" (or "end"): Align right edges. For a "horizontal" arrangement: - "top" (or "start"): Align top edges. - "center": Align centers. - "bottom" (or "end"): Align bottom edges. |
'start'
|
segment_gap
|
float
|
The virtual gap (in PDF points) between segments. |
0.0
|
Source code in natural_pdf/flows/flow.py
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 | |
natural_pdf.Flow.analyze_layout(engine=None, options=None, confidence=None, classes=None, exclude_classes=None, device=None, existing='replace', model_name=None, client=None)
Analyze layout across all segments in the flow.
This method efficiently groups segments by their parent pages, runs layout analysis only once per unique page, then filters results appropriately for each segment. This avoids redundant analysis when multiple flow segments come from the same page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Optional[str]
|
Name of the layout engine (e.g., 'yolo', 'tatr'). Uses manager's default if None. |
None
|
options
|
Optional[Any]
|
Specific LayoutOptions object for advanced configuration. |
None
|
confidence
|
Optional[float]
|
Minimum confidence threshold. |
None
|
classes
|
Optional[List[str]]
|
Specific classes to detect. |
None
|
exclude_classes
|
Optional[List[str]]
|
Classes to exclude. |
None
|
device
|
Optional[str]
|
Device for inference. |
None
|
existing
|
str
|
How to handle existing detected regions: 'replace' (default) or 'append'. |
'replace'
|
model_name
|
Optional[str]
|
Optional model name for the engine. |
None
|
client
|
Optional[Any]
|
Optional client for API-based engines. |
None
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection containing all detected Region objects from all segments. |
Example
Multi-page layout analysis:
pdf = npdf.PDF("document.pdf")
# Create flow for first 3 pages
page_flow = Flow(
segments=pdf.pages[:3],
arrangement='vertical'
)
# Analyze layout across all pages (efficiently)
all_regions = page_flow.analyze_layout(engine='yolo')
# Find all tables across the flow
tables = all_regions.filter('region[type=table]')
Source code in natural_pdf/flows/flow.py
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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 | |
natural_pdf.Flow.extract_table(method=None, table_settings=None, use_ocr=False, ocr_config=None, text_options=None, cell_extraction_func=None, show_progress=False, content_filter=None, stitch_rows=None, merge_headers=None)
extract_table(method: Optional[str] = None, table_settings: Optional[dict] = None, use_ocr: bool = False, ocr_config: Optional[dict] = None, text_options: Optional[dict] = None, cell_extraction_func: Optional[Any] = None, show_progress: bool = False, content_filter: Optional[Any] = None, stitch_rows: Callable[[List[Optional[str]]], bool] = None) -> TableResult
extract_table(method: Optional[str] = None, table_settings: Optional[dict] = None, use_ocr: bool = False, ocr_config: Optional[dict] = None, text_options: Optional[dict] = None, cell_extraction_func: Optional[Any] = None, show_progress: bool = False, content_filter: Optional[Any] = None, stitch_rows: Callable[[List[Optional[str]], List[Optional[str]], int, Union[Page, PhysicalRegion]], bool] = None) -> TableResult
Extract table data from all segments in the flow, combining results sequentially.
This method extracts table data from each segment in flow order and combines the results into a single logical table. This is particularly useful for multi-page tables or tables that span across columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Optional[str]
|
Method to use: 'tatr', 'pdfplumber', 'text', 'stream', 'lattice', or None (auto-detect). |
None
|
table_settings
|
Optional[dict]
|
Settings for pdfplumber table extraction. |
None
|
use_ocr
|
bool
|
Whether to use OCR for text extraction (currently only applicable with 'tatr' method). |
False
|
ocr_config
|
Optional[dict]
|
OCR configuration parameters. |
None
|
text_options
|
Optional[dict]
|
Dictionary of options for the 'text' method. |
None
|
cell_extraction_func
|
Optional[Any]
|
Optional callable function that takes a cell Region object and returns its string content. For 'text' method only. |
None
|
show_progress
|
bool
|
If True, display a progress bar during cell text extraction for the 'text' method. |
False
|
content_filter
|
Optional[Any]
|
Optional content filter to apply during cell text extraction. |
None
|
merge_headers
|
Optional[bool]
|
Whether to merge tables by removing repeated headers from subsequent segments. If None (default), auto-detects by checking if the first row of each segment matches the first row of the first segment. If segments have inconsistent header patterns (some repeat, others don't), raises ValueError. Useful for multi-page tables where headers repeat on each page. |
None
|
stitch_rows
|
Optional[Callable]
|
Optional callable to determine when rows should be merged across segment boundaries. Applied AFTER header removal if merge_headers is enabled. Two overloaded signatures are supported: |
None
|
Returns:
| Type | Description |
|---|---|
TableResult
|
TableResult object containing the aggregated table data from all segments. |
Example
Multi-page table extraction:
pdf = npdf.PDF("multi_page_table.pdf")
# Create flow for table spanning pages 2-4
table_flow = Flow(
segments=[pdf.pages[1], pdf.pages[2], pdf.pages[3]],
arrangement='vertical'
)
# Extract table as if it were continuous
table_data = table_flow.extract_table()
df = table_data.df # Convert to pandas DataFrame
# Custom row stitching - single parameter (simple case)
table_data = table_flow.extract_table(
stitch_rows=lambda row: row and not (row[0] or "").strip()
)
# Custom row stitching - full parameters (advanced case)
table_data = table_flow.extract_table(
stitch_rows=lambda prev, curr, idx, seg: idx == 0 and curr and not (curr[0] or "").strip()
)
Source code in natural_pdf/flows/flow.py
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 | |
natural_pdf.Flow.find(selector=None, *, text=None, apply_exclusions=True, regex=False, case=True, **kwargs)
Finds the first element within the flow that matches the given selector or text criteria.
Elements found are wrapped as FlowElement objects, anchored to this Flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string. |
None
|
text
|
Optional[str]
|
Text content to search for. |
None
|
apply_exclusions
|
bool
|
Whether to respect exclusion zones on the original pages/regions. |
True
|
regex
|
bool
|
Whether the text search uses regex. |
False
|
case
|
bool
|
Whether the text search is case-sensitive. |
True
|
**kwargs
|
Additional filter parameters for the underlying find operation. |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[FlowElement]
|
A FlowElement if a match is found, otherwise None. |
Source code in natural_pdf/flows/flow.py
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 | |
natural_pdf.Flow.find_all(selector=None, *, text=None, apply_exclusions=True, regex=False, case=True, **kwargs)
Finds all elements within the flow that match the given selector or text criteria.
This method efficiently groups segments by their parent pages, searches at the page level, then filters results appropriately for each segment. This ensures elements that intersect with flow segments (but aren't fully contained) are still found.
Elements found are wrapped as FlowElement objects, anchored to this Flow, and returned in a FlowElementCollection.
Source code in natural_pdf/flows/flow.py
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 | |
natural_pdf.Flow.get_element_flow_coordinates(physical_element)
Translates a physical element's coordinates into the flow's virtual coordinate system. (Placeholder - very complex if segment_gap > 0 or complex alignments)
Source code in natural_pdf/flows/flow.py
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 | |
natural_pdf.Flow.get_sections(start_elements=None, end_elements=None, new_section_on_page_break=False, include_boundaries='both', orientation='vertical')
Extract logical sections from the Flow based on start and end boundary elements, mirroring the behaviour of PDF/PageCollection.get_sections().
This implementation is a thin wrapper that converts the Flow into a temporary PageCollection (constructed from the unique pages that the Flow spans) and then delegates the heavy‐lifting to that existing implementation. Any FlowElement / FlowElementCollection inputs are automatically unwrapped to their underlying physical elements so that PageCollection can work with them directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_elements
|
Elements or selector string that mark the start of sections (optional). |
None
|
|
end_elements
|
Elements or selector string that mark the end of sections (optional). |
None
|
|
new_section_on_page_break
|
bool
|
Whether to start a new section at page boundaries (default: False). |
False
|
include_boundaries
|
str
|
How to include boundary elements: 'start', 'end', 'both', or 'none' (default: 'both'). |
'both'
|
orientation
|
str
|
'vertical' (default) or 'horizontal' - determines section direction. |
'vertical'
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection of Region/FlowRegion objects representing the |
ElementCollection
|
extracted sections. |
Source code in natural_pdf/flows/flow.py
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 | |
natural_pdf.Flow.get_segment_bounding_box_in_flow(segment_index)
Calculates the conceptual bounding box of a segment within the flow's coordinate system. This considers arrangement, alignment, and segment gaps. (This is a placeholder for more complex logic if a true virtual coordinate system is needed) For now, it might just return the physical segment's bbox if gaps are 0 and alignment is simple.
Source code in natural_pdf/flows/flow.py
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 | |
natural_pdf.Flow.highlights(show=False)
Create a highlight context for accumulating highlights.
This allows for clean syntax to show multiple highlight groups:
Example
with flow.highlights() as h: h.add(flow.find_all('table'), label='tables', color='blue') h.add(flow.find_all('text:bold'), label='bold text', color='red') h.show()
Or with automatic display
with flow.highlights(show=True) as h: h.add(flow.find_all('table'), label='tables') h.add(flow.find_all('text:bold'), label='bold') # Automatically shows when exiting the context
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show
|
bool
|
If True, automatically show highlights when exiting context |
False
|
Returns:
| Type | Description |
|---|---|
|
HighlightContext for accumulating highlights |
Source code in natural_pdf/flows/flow.py
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 | |
natural_pdf.Flow.show(*, resolution=None, width=None, color=None, labels=True, label_format=None, highlights=None, layout='stack', stack_direction='vertical', gap=5, columns=None, crop=False, crop_bbox=None, in_context=False, separator_color=None, separator_thickness=2, **kwargs)
Generate a preview image with highlights.
If in_context=True, shows segments as cropped images stacked together with separators between segments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution
|
Optional[float]
|
DPI for rendering (default from global settings) |
None
|
width
|
Optional[int]
|
Target width in pixels (overrides resolution) |
None
|
color
|
Optional[Union[str, Tuple[int, int, int]]]
|
Default highlight color |
None
|
labels
|
bool
|
Whether to show labels for highlights |
True
|
label_format
|
Optional[str]
|
Format string for labels |
None
|
highlights
|
Optional[List[Dict[str, Any]]]
|
Additional highlight groups to show |
None
|
layout
|
Literal['stack', 'grid', 'single']
|
How to arrange multiple pages/regions |
'stack'
|
stack_direction
|
Literal['vertical', 'horizontal']
|
Direction for stack layout |
'vertical'
|
gap
|
int
|
Pixels between stacked images |
5
|
columns
|
Optional[int]
|
Number of columns for grid layout |
None
|
crop
|
Union[bool, Literal['content']]
|
Whether to crop |
False
|
crop_bbox
|
Optional[Tuple[float, float, float, float]]
|
Explicit crop bounds |
None
|
in_context
|
bool
|
If True, use special Flow visualization with separators |
False
|
separator_color
|
Optional[Tuple[int, int, int]]
|
RGB color for separator lines (default: red) |
None
|
separator_thickness
|
int
|
Thickness of separator lines |
2
|
**kwargs
|
Additional parameters passed to rendering |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Image]
|
PIL Image object or None if nothing to render |
Source code in natural_pdf/flows/flow.py
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 | |
natural_pdf.FlowRegion
Bases: Visualizable
Represents a selected area within a Flow, potentially composed of multiple physical Region objects (constituent_regions) that might span across different original pages or disjoint physical regions defined in the Flow.
A FlowRegion is the result of a directional operation (e.g., .below(), .above()) on a FlowElement.
Source code in natural_pdf/flows/region.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 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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 | |
Attributes
natural_pdf.FlowRegion.bbox
property
The bounding box that encloses all constituent regions. Calculated dynamically and cached.
natural_pdf.FlowRegion.is_empty
property
Checks if the FlowRegion contains no constituent regions or if all are empty.
natural_pdf.FlowRegion.normalized_type
property
Return the normalized type for selector compatibility. This allows FlowRegion to be found by selectors like 'table'.
natural_pdf.FlowRegion.type
property
Return the type attribute for selector compatibility. This is an alias for normalized_type.
Functions
natural_pdf.FlowRegion.__getattr__(name)
Dynamically proxy attribute access to the source FlowElement for safe attributes only. Spatial methods (above, below, left, right) are explicitly implemented to prevent silent failures and incorrect behavior.
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.__init__(flow, constituent_regions, source_flow_element, boundary_element_found=None)
Initializes a FlowRegion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flow
|
Flow
|
The Flow instance this region belongs to. |
required |
constituent_regions
|
List[Region]
|
A list of physical natural_pdf.elements.region.Region objects that make up this FlowRegion. |
required |
source_flow_element
|
FlowElement
|
The FlowElement that created this FlowRegion. |
required |
boundary_element_found
|
Optional[Element]
|
The physical element that stopped an 'until' search, if applicable. |
None
|
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.above(height=None, width='full', include_source=False, until=None, include_endpoint=True, **kwargs)
Create a FlowRegion with regions above this FlowRegion.
For vertical flows: Only expands the topmost constituent region upward. For horizontal flows: Expands all constituent regions upward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
|
Optional[float]
|
Height of the region above, in points |
None
|
width
|
str
|
Width mode - "full" for full page width or "element" for element width |
'full'
|
include_source
|
bool
|
Whether to include this FlowRegion in the result |
False
|
until
|
Optional[str]
|
Optional selector string to specify an upper boundary element |
None
|
include_endpoint
|
bool
|
Whether to include the boundary element in the region |
True
|
**kwargs
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
FlowRegion
|
New FlowRegion with regions above |
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.below(height=None, width='full', include_source=False, until=None, include_endpoint=True, **kwargs)
Create a FlowRegion with regions below this FlowRegion.
For vertical flows: Only expands the bottommost constituent region downward. For horizontal flows: Expands all constituent regions downward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
|
Optional[float]
|
Height of the region below, in points |
None
|
width
|
str
|
Width mode - "full" for full page width or "element" for element width |
'full'
|
include_source
|
bool
|
Whether to include this FlowRegion in the result |
False
|
until
|
Optional[str]
|
Optional selector string to specify a lower boundary element |
None
|
include_endpoint
|
bool
|
Whether to include the boundary element in the region |
True
|
**kwargs
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
FlowRegion
|
New FlowRegion with regions below |
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.elements(apply_exclusions=True)
Collects all unique physical elements from all constituent physical regions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
apply_exclusions
|
bool
|
Whether to respect PDF exclusion zones within each constituent physical region when gathering elements. |
True
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
An ElementCollection containing all unique elements. |
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.expand(left=0, right=0, top=0, bottom=0, width_factor=1.0, height_factor=1.0)
Create a new FlowRegion with all constituent regions expanded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
float
|
Amount to expand left edge (positive value expands leftwards) |
0
|
right
|
float
|
Amount to expand right edge (positive value expands rightwards) |
0
|
top
|
float
|
Amount to expand top edge (positive value expands upwards) |
0
|
bottom
|
float
|
Amount to expand bottom edge (positive value expands downwards) |
0
|
width_factor
|
float
|
Factor to multiply width by (applied after absolute expansion) |
1.0
|
height_factor
|
float
|
Factor to multiply height by (applied after absolute expansion) |
1.0
|
Returns:
| Type | Description |
|---|---|
FlowRegion
|
New FlowRegion with expanded constituent regions |
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.extract_table(method=None, table_settings=None, use_ocr=False, ocr_config=None, text_options=None, cell_extraction_func=None, show_progress=False, stitch_rows=None, merge_headers=None, **kwargs)
Extracts a single logical table from the FlowRegion.
This is a convenience wrapper that iterates through the constituent
physical regions in flow order, calls their extract_table
method, and concatenates the resulting rows. It mirrors the public
interface of :pymeth:natural_pdf.elements.region.Region.extract_table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method, table_settings, use_ocr, ocr_config, text_options, cell_extraction_func, show_progress
|
Same as in :pymeth: |
required | |
merge_headers
|
Optional[bool]
|
Whether to merge tables by removing repeated headers from subsequent pages/segments. If None (default), auto-detects by checking if the first row of each segment matches the first row of the first segment. If segments have inconsistent header patterns (some repeat, others don't), raises ValueError. Useful for multi-page tables where headers repeat on each page. |
None
|
**kwargs
|
Additional keyword arguments forwarded to the underlying
|
{}
|
Returns:
| Type | Description |
|---|---|
TableResult
|
A TableResult object containing the aggregated table data. Rows returned from |
TableResult
|
consecutive constituent regions are appended in document order. If |
TableResult
|
no tables are detected in any region, an empty TableResult is returned. |
stitch_rows parameter
Controls whether the first rows of subsequent segments/regions should be merged into the previous row (to handle spill-over across page breaks). Applied AFTER header removal if merge_headers is enabled.
• None (default) – no merging (behaviour identical to previous versions).
• Callable – custom predicate taking
(prev_row, cur_row, row_idx_in_segment, segment_object) → bool.
Return True to merge cur_row into prev_row (default column-wise merge is used).
Source code in natural_pdf/flows/region.py
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 | |
natural_pdf.FlowRegion.extract_tables(method=None, table_settings=None, **kwargs)
Extract all tables from the FlowRegion.
This simply chains :pymeth:Region.extract_tables over each physical
region and concatenates their results, preserving flow order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method, table_settings
|
Forwarded to underlying |
required | |
**kwargs
|
Additional keyword arguments forwarded. |
{}
|
Returns:
| Type | Description |
|---|---|
List[List[List[Optional[str]]]]
|
A list where each item is a full table (list of rows). The order of |
List[List[List[Optional[str]]]]
|
tables follows the order of the constituent regions in the flow. |
Source code in natural_pdf/flows/region.py
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 | |
natural_pdf.FlowRegion.extract_text(apply_exclusions=True, **kwargs)
Extracts and concatenates text from all constituent physical regions. The order of concatenation respects the flow's arrangement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
apply_exclusions
|
bool
|
Whether to respect PDF exclusion zones within each constituent physical region during text extraction. |
True
|
**kwargs
|
Additional arguments passed to the underlying extract_text method of each constituent region. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
The combined text content as a string. |
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.find(selector=None, *, text=None, **kwargs)
Find the first element in flow order that matches the selector or text.
This implementation iterates through the constituent regions *in the order
they appear in self.constituent_regions (i.e. document flow order),
delegating the search to each region's own find method. It therefore
avoids constructing a huge intermediate ElementCollection and returns as
soon as a match is found, which is substantially faster and ensures that
selectors such as 'table' work exactly as they do on an individual
Region.
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.find_all(selector=None, *, text=None, **kwargs)
Find all elements across the constituent regions that match the given selector or text.
Rather than first materialising every element in the FlowRegion (which
can be extremely slow for multi-page flows), this implementation simply
chains each region's native find_all call and concatenates their
results into a single ElementCollection while preserving flow order.
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.get_highlight_specs()
Get highlight specifications for all constituent regions.
This implements the highlighting protocol for FlowRegions, returning specs for each constituent region so they can be highlighted on their respective pages.
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of highlight specification dictionaries, one for each |
List[Dict[str, Any]]
|
constituent region. |
Source code in natural_pdf/flows/region.py
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 | |
natural_pdf.FlowRegion.get_sections(start_elements=None, end_elements=None, new_section_on_page_break=False, include_boundaries='both', orientation='vertical')
Extract logical sections from this FlowRegion based on start/end boundary elements.
This delegates to the parent Flow's get_sections() method, but only operates on the segments that are part of this FlowRegion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_elements
|
Elements or selector string that mark the start of sections |
None
|
|
end_elements
|
Elements or selector string that mark the end of sections |
None
|
|
new_section_on_page_break
|
bool
|
Whether to start a new section at page boundaries |
False
|
include_boundaries
|
str
|
How to include boundary elements: 'start', 'end', 'both', or 'none' |
'both'
|
orientation
|
str
|
'vertical' (default) or 'horizontal' - determines section direction |
'vertical'
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection of FlowRegion objects representing the extracted sections |
Example
Split a multi-page table region by headers
table_region = flow.find("text:contains('Table 4')").below(until="text:contains('Table 5')") sections = table_region.get_sections(start_elements="text:bold")
Source code in natural_pdf/flows/region.py
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 | |
natural_pdf.FlowRegion.highlight(label=None, color=None, **kwargs)
Highlights all constituent physical regions on their respective pages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
Optional[str]
|
A base label for the highlights. Each constituent region might get an indexed label. |
None
|
color
|
Optional[Union[Tuple, str]]
|
Color for the highlight. |
None
|
**kwargs
|
Additional arguments for the underlying highlight method. |
{}
|
Returns:
| Type | Description |
|---|---|
FlowRegion
|
Self for method chaining. |
Source code in natural_pdf/flows/region.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
natural_pdf.FlowRegion.highlights(show=False)
Create a highlight context for accumulating highlights.
This allows for clean syntax to show multiple highlight groups:
Example
with flow_region.highlights() as h: h.add(flow_region.find_all('table'), label='tables', color='blue') h.add(flow_region.find_all('text:bold'), label='bold text', color='red') h.show()
Or with automatic display
with flow_region.highlights(show=True) as h: h.add(flow_region.find_all('table'), label='tables') h.add(flow_region.find_all('text:bold'), label='bold') # Automatically shows when exiting the context
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show
|
bool
|
If True, automatically show highlights when exiting context |
False
|
Returns:
| Type | Description |
|---|---|
HighlightContext
|
HighlightContext for accumulating highlights |
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.left(width=None, height='full', include_source=False, until=None, include_endpoint=True, **kwargs)
Create a FlowRegion with regions to the left of this FlowRegion.
For vertical flows: Expands all constituent regions leftward. For horizontal flows: Only expands the leftmost constituent region leftward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
Optional[float]
|
Width of the region to the left, in points |
None
|
height
|
str
|
Height mode - "full" for full page height or "element" for element height |
'full'
|
include_source
|
bool
|
Whether to include this FlowRegion in the result |
False
|
until
|
Optional[str]
|
Optional selector string to specify a left boundary element |
None
|
include_endpoint
|
bool
|
Whether to include the boundary element in the region |
True
|
**kwargs
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
FlowRegion
|
New FlowRegion with regions to the left |
Source code in natural_pdf/flows/region.py
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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 | |
natural_pdf.FlowRegion.right(width=None, height='full', include_source=False, until=None, include_endpoint=True, **kwargs)
Create a FlowRegion with regions to the right of this FlowRegion.
For vertical flows: Expands all constituent regions rightward. For horizontal flows: Only expands the rightmost constituent region rightward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
Optional[float]
|
Width of the region to the right, in points |
None
|
height
|
str
|
Height mode - "full" for full page height or "element" for element height |
'full'
|
include_source
|
bool
|
Whether to include this FlowRegion in the result |
False
|
until
|
Optional[str]
|
Optional selector string to specify a right boundary element |
None
|
include_endpoint
|
bool
|
Whether to include the boundary element in the region |
True
|
**kwargs
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
FlowRegion
|
New FlowRegion with regions to the right |
Source code in natural_pdf/flows/region.py
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 | |
natural_pdf.FlowRegion.split(by=None, page_breaks=True, **kwargs)
Split this FlowRegion into sections.
This is a convenience method that wraps get_sections() with common splitting patterns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
by
|
Optional[str]
|
Selector string for elements that mark section boundaries (e.g., "text:bold") |
None
|
page_breaks
|
bool
|
Whether to also split at page boundaries (default: True) |
True
|
**kwargs
|
Additional arguments passed to get_sections() |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection of FlowRegion objects representing the sections |
Example
Split by bold headers
sections = flow_region.split(by="text:bold")
Split only by specific text pattern, ignoring page breaks
sections = flow_region.split( by="text:contains('Section')", page_breaks=False )
Source code in natural_pdf/flows/region.py
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 | |
natural_pdf.FlowRegion.to_images(resolution=150, **kwargs)
Generates and returns a list of cropped PIL Images, one for each constituent physical region of this FlowRegion.
Source code in natural_pdf/flows/region.py
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 | |
natural_pdf.FlowRegion.to_region()
Convert this FlowRegion to a region (returns a copy). This is equivalent to calling expand() with no arguments.
Returns:
| Type | Description |
|---|---|
FlowRegion
|
Copy of this FlowRegion |
Source code in natural_pdf/flows/region.py
920 921 922 923 924 925 926 927 928 | |
natural_pdf.Guides
Manages vertical and horizontal guide lines for table extraction and layout analysis.
Guides are collections of coordinates that can be used to define table boundaries, column positions, or general layout structures. They can be created through various detection methods or manually specified.
Attributes:
| Name | Type | Description |
|---|---|---|
verticals |
List of x-coordinates for vertical guide lines |
|
horizontals |
List of y-coordinates for horizontal guide lines |
|
context |
Optional Page/Region that these guides relate to |
|
bounds |
Optional bounding box (x0, y0, x1, y1) for relative coordinate conversion |
|
snap_behavior |
How to handle failed snapping operations ('warn', 'ignore', 'raise') |
Source code in natural_pdf/analyzers/guides.py
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 | |
Attributes
natural_pdf.Guides.cells
property
Access cells by index like guides.cells[row][col] or guides.cells[row, col].
natural_pdf.Guides.columns
property
Access columns by index like guides.columns[0].
natural_pdf.Guides.horizontal
property
writable
Get horizontal guide coordinates.
natural_pdf.Guides.n_cols
property
Number of columns defined by vertical guides.
natural_pdf.Guides.n_rows
property
Number of rows defined by horizontal guides.
natural_pdf.Guides.rows
property
Access rows by index like guides.rows[0].
natural_pdf.Guides.vertical
property
writable
Get vertical guide coordinates.
Functions
natural_pdf.Guides.__add__(other)
Combine two guide sets.
Returns:
| Type | Description |
|---|---|
Guides
|
New Guides object with combined coordinates |
Source code in natural_pdf/analyzers/guides.py
2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 | |
natural_pdf.Guides.__init__(verticals=None, horizontals=None, context=None, bounds=None, relative=False, snap_behavior='warn')
Initialize a Guides object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
verticals
|
Optional[Union[List[float], Page, Region, FlowRegion]]
|
List of x-coordinates for vertical guides, or a Page/Region/FlowRegion as context |
None
|
horizontals
|
Optional[List[float]]
|
List of y-coordinates for horizontal guides |
None
|
context
|
Optional[Union[Page, Region, FlowRegion]]
|
Page, Region, or FlowRegion object these guides were created from |
None
|
bounds
|
Optional[Tuple[float, float, float, float]]
|
Bounding box (x0, top, x1, bottom) if context not provided |
None
|
relative
|
bool
|
Whether coordinates are relative (0-1) or absolute |
False
|
snap_behavior
|
Literal['raise', 'warn', 'ignore']
|
How to handle snapping conflicts ('raise', 'warn', or 'ignore') |
'warn'
|
Source code in natural_pdf/analyzers/guides.py
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 | |
natural_pdf.Guides.__repr__()
String representation of the guides.
Source code in natural_pdf/analyzers/guides.py
4151 4152 4153 4154 4155 4156 4157 | |
natural_pdf.Guides.above(guide_index, obj=None)
Get a region above a horizontal guide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guide_index
|
int
|
Horizontal guide index |
required |
obj
|
Optional[Union[Page, Region]]
|
Page or Region to create the region on (uses self.context if None) |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
Region above the specified guide |
Source code in natural_pdf/analyzers/guides.py
2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 | |
natural_pdf.Guides.add_content(axis='vertical', markers=None, obj=None, align='left', outer=True, tolerance=5, apply_exclusions=True)
Instance method: Add guides from content, allowing chaining. This allows: Guides.new(page).add_content(axis='vertical', markers=[...])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
Literal['vertical', 'horizontal']
|
Which axis to create guides for |
'vertical'
|
markers
|
Union[str, List[str], ElementCollection, None]
|
Content to search for. Can be: - str: single selector or literal text - List[str]: list of selectors or literal text strings - ElementCollection: collection of elements to extract text from - None: no markers |
None
|
obj
|
Optional[Union[Page, Region]]
|
Page or Region to search (uses self.context if None) |
None
|
align
|
Literal['left', 'right', 'center', 'between']
|
How to align guides relative to found elements |
'left'
|
outer
|
Union[str, bool]
|
Whether to add outer boundary guides. Can be: - bool: True/False to add/not add both - "first": To add boundary before the first element - "last": To add boundary before the last element |
True
|
tolerance
|
float
|
Tolerance for snapping to element edges |
5
|
apply_exclusions
|
bool
|
Whether to apply exclusion zones when searching for text |
True
|
Returns:
| Type | Description |
|---|---|
Guides
|
Self for method chaining |
Source code in natural_pdf/analyzers/guides.py
4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 | |
natural_pdf.Guides.add_horizontal(y)
Add a horizontal guide at the specified y-coordinate.
Source code in natural_pdf/analyzers/guides.py
2375 2376 2377 2378 2379 | |
natural_pdf.Guides.add_lines(axis='both', obj=None, threshold='auto', source_label=None, max_lines_h=None, max_lines_v=None, outer=False, detection_method='vector', resolution=192, **detect_kwargs)
Instance method: Add guides from lines, allowing chaining. This allows: Guides.new(page).add_lines(axis='horizontal')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
Literal['vertical', 'horizontal', 'both']
|
Which axis to detect lines for |
'both'
|
obj
|
Optional[Union[Page, Region]]
|
Page or Region to search (uses self.context if None) |
None
|
threshold
|
Union[float, str]
|
Line detection threshold ('auto' or float 0.0-1.0) |
'auto'
|
source_label
|
Optional[str]
|
Filter lines by source label (vector) or label for detected lines (pixels) |
None
|
max_lines_h
|
Optional[int]
|
Maximum horizontal lines to use |
None
|
max_lines_v
|
Optional[int]
|
Maximum vertical lines to use |
None
|
outer
|
bool
|
Whether to add outer boundary guides |
False
|
detection_method
|
str
|
'vector' (use existing LineElements) or 'pixels' (detect from image) |
'vector'
|
resolution
|
int
|
DPI for pixel-based detection (default: 192) |
192
|
**detect_kwargs
|
Additional parameters for pixel detection (see from_lines) |
{}
|
Returns:
| Type | Description |
|---|---|
Guides
|
Self for method chaining |
Source code in natural_pdf/analyzers/guides.py
4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 | |
natural_pdf.Guides.add_vertical(x)
Add a vertical guide at the specified x-coordinate.
Source code in natural_pdf/analyzers/guides.py
2369 2370 2371 2372 2373 | |
natural_pdf.Guides.add_whitespace(axis='both', obj=None, min_gap=10)
Instance method: Add guides from whitespace, allowing chaining. This allows: Guides.new(page).add_whitespace(axis='both')
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
Literal['vertical', 'horizontal', 'both']
|
Which axis to create guides for |
'both'
|
obj
|
Optional[Union[Page, Region]]
|
Page or Region to search (uses self.context if None) |
None
|
min_gap
|
float
|
Minimum gap size to consider |
10
|
Returns:
| Type | Description |
|---|---|
Guides
|
Self for method chaining |
Source code in natural_pdf/analyzers/guides.py
4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 | |
natural_pdf.Guides.below(guide_index, obj=None)
Get a region below a horizontal guide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guide_index
|
int
|
Horizontal guide index |
required |
obj
|
Optional[Union[Page, Region]]
|
Page or Region to create the region on (uses self.context if None) |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
Region below the specified guide |
Source code in natural_pdf/analyzers/guides.py
2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 | |
natural_pdf.Guides.between_horizontal(start_index, end_index, obj=None)
Get a region between two horizontal guides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_index
|
int
|
Starting horizontal guide index |
required |
end_index
|
int
|
Ending horizontal guide index |
required |
obj
|
Optional[Union[Page, Region]]
|
Page or Region to create the region on (uses self.context if None) |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
Region between the specified guides |
Source code in natural_pdf/analyzers/guides.py
2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 | |
natural_pdf.Guides.between_vertical(start_index, end_index, obj=None)
Get a region between two vertical guides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_index
|
int
|
Starting vertical guide index |
required |
end_index
|
int
|
Ending vertical guide index |
required |
obj
|
Optional[Union[Page, Region]]
|
Page or Region to create the region on (uses self.context if None) |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
Region between the specified guides |
Source code in natural_pdf/analyzers/guides.py
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 | |
natural_pdf.Guides.build_grid(target=None, source='guides', cell_padding=0.5, include_outer_boundaries=False, *, multi_page='auto')
Create table structure (table, rows, columns, cells) from guide coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Optional[Union[Page, Region]]
|
Page or Region to create regions on (uses self.context if None) |
None
|
source
|
str
|
Source label for created regions (for identification) |
'guides'
|
cell_padding
|
float
|
Internal padding for cell regions in points |
0.5
|
include_outer_boundaries
|
bool
|
Whether to add boundaries at edges if missing |
False
|
multi_page
|
Literal['auto', True, False]
|
Controls multi-region table creation for FlowRegions. - "auto": (default) Creates a unified grid if there are multiple regions or guides span pages. - True: Forces creation of a unified multi-region grid. - False: Creates separate grids for each region. |
'auto'
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with 'counts' and 'regions' created. |
Source code in natural_pdf/analyzers/guides.py
3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 | |
natural_pdf.Guides.cell(row, col, obj=None)
Get a cell region from the guides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
int
|
Row index (0-based) |
required |
col
|
int
|
Column index (0-based) |
required |
obj
|
Optional[Union[Page, Region]]
|
Page or Region to create the cell on (uses self.context if None) |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
Region representing the specified cell |
Raises:
| Type | Description |
|---|---|
IndexError
|
If row or column index is out of range |
Source code in natural_pdf/analyzers/guides.py
2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 | |
natural_pdf.Guides.column(index, obj=None)
Get a column region from the guides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Column index (0-based) |
required |
obj
|
Optional[Union[Page, Region]]
|
Page or Region to create the column on (uses self.context if None) |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
Region representing the specified column |
Raises:
| Type | Description |
|---|---|
IndexError
|
If column index is out of range |
Source code in natural_pdf/analyzers/guides.py
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 | |
natural_pdf.Guides.divide(obj, n=None, cols=None, rows=None, axis='both')
classmethod
Create guides by evenly dividing an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Union[Page, Region, Tuple[float, float, float, float]]
|
Object to divide (Page, Region, or bbox tuple) |
required |
n
|
Optional[int]
|
Number of divisions (creates n+1 guides). Used if cols/rows not specified. |
None
|
cols
|
Optional[int]
|
Number of columns (creates cols+1 vertical guides) |
None
|
rows
|
Optional[int]
|
Number of rows (creates rows+1 horizontal guides) |
None
|
axis
|
Literal['vertical', 'horizontal', 'both']
|
Which axis to divide along |
'both'
|
Returns:
| Type | Description |
|---|---|
Guides
|
New Guides object with evenly spaced lines |
Examples:
Divide into 3 columns
guides = Guides.divide(page, cols=3)
Divide into 5 rows
guides = Guides.divide(region, rows=5)
Divide both axes
guides = Guides.divide(page, cols=3, rows=5)
Source code in natural_pdf/analyzers/guides.py
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 | |
natural_pdf.Guides.extract_table(target=None, source='guides_temp', cell_padding=0.5, include_outer_boundaries=False, method=None, table_settings=None, use_ocr=False, ocr_config=None, text_options=None, cell_extraction_func=None, show_progress=False, content_filter=None, apply_exclusions=True, *, multi_page='auto', header='first', skip_repeating_headers=None)
Extract table data directly from guides without leaving temporary regions.
This method: 1. Creates table structure using build_grid() 2. Extracts table data from the created table region 3. Cleans up all temporary regions 4. Returns the TableResult
When passed a collection (PageCollection, ElementCollection, or list), this method will extract tables from each element and combine them into a single result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Optional[Union[Page, Region, PageCollection, ElementCollection, List[Union[Page, Region]]]]
|
Page, Region, or collection of Pages/Regions to extract from (uses self.context if None) |
None
|
source
|
str
|
Source label for temporary regions (will be cleaned up) |
'guides_temp'
|
cell_padding
|
float
|
Internal padding for cell regions in points |
0.5
|
include_outer_boundaries
|
bool
|
Whether to add boundaries at edges if missing |
False
|
method
|
Optional[str]
|
Table extraction method ('tatr', 'pdfplumber', 'text', etc.) |
None
|
table_settings
|
Optional[dict]
|
Settings for pdfplumber table extraction |
None
|
use_ocr
|
bool
|
Whether to use OCR for text extraction |
False
|
ocr_config
|
Optional[dict]
|
OCR configuration parameters |
None
|
text_options
|
Optional[Dict]
|
Dictionary of options for the 'text' method |
None
|
cell_extraction_func
|
Optional[Callable[[Region], Optional[str]]]
|
Optional callable for custom cell text extraction |
None
|
show_progress
|
bool
|
Controls progress bar for text method |
False
|
content_filter
|
Optional[Union[str, Callable[[str], bool], List[str]]]
|
Content filtering function or patterns |
None
|
apply_exclusions
|
bool
|
Whether to apply exclusion regions during text extraction (default: True) |
True
|
multi_page
|
Literal['auto', True, False]
|
Controls multi-region table creation for FlowRegions |
'auto'
|
header
|
Union[str, List[str], None]
|
How to handle headers when extracting from collections: - "first": Use first row of first element as headers (default) - "all": Expect headers on each element, use from first element - None: No headers, use numeric indices - List[str]: Custom column names |
'first'
|
skip_repeating_headers
|
Optional[bool]
|
Whether to remove duplicate header rows when extracting from collections. Defaults to True when header is "first" or "all", False otherwise. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
TableResult |
TableResult
|
Extracted table data |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no table region is created from the guides |
Example
from natural_pdf.analyzers import Guides
# Single page extraction
guides = Guides.from_lines(page, source_label="detected")
table_data = guides.extract_table()
df = table_data.to_df()
# Multiple page extraction
guides = Guides(pages[0])
guides.vertical.from_content(['Column 1', 'Column 2'])
table_result = guides.extract_table(pages, header=['Col1', 'Col2'])
df = table_result.to_df()
# Region collection extraction
regions = pdf.find_all('region[type=table]')
guides = Guides(regions[0])
guides.vertical.from_lines(n=3)
table_result = guides.extract_table(regions)
Source code in natural_pdf/analyzers/guides.py
4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 | |
natural_pdf.Guides.from_content(obj, axis='vertical', markers=None, align='left', outer=True, tolerance=5, apply_exclusions=True)
classmethod
Create guides based on text content positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Union[Page, Region, FlowRegion]
|
Page, Region, or FlowRegion to search for content |
required |
axis
|
Literal['vertical', 'horizontal']
|
Whether to create vertical or horizontal guides |
'vertical'
|
markers
|
Union[str, List[str], ElementCollection, None]
|
Content to search for. Can be: - str: single selector (e.g., 'text:contains("Name")') or literal text - List[str]: list of selectors or literal text strings - ElementCollection: collection of elements to extract text from - None: no markers |
None
|
align
|
Union[Literal['left', 'right', 'center', 'between'], Literal['top', 'bottom']]
|
Where to place guides relative to found text: - For vertical guides: 'left', 'right', 'center', 'between' - For horizontal guides: 'top', 'bottom', 'center', 'between' |
'left'
|
outer
|
bool
|
Whether to add guides at the boundaries |
True
|
tolerance
|
float
|
Maximum distance to search for text |
5
|
apply_exclusions
|
bool
|
Whether to apply exclusion zones when searching for text |
True
|
Returns:
| Type | Description |
|---|---|
Guides
|
New Guides object aligned to text content |
Source code in natural_pdf/analyzers/guides.py
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 | |
natural_pdf.Guides.from_lines(obj, axis='both', threshold='auto', source_label=None, max_lines_h=None, max_lines_v=None, outer=False, detection_method='pixels', resolution=192, **detect_kwargs)
classmethod
Create guides from detected line elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Union[Page, Region, FlowRegion]
|
Page, Region, or FlowRegion to detect lines from |
required |
axis
|
Literal['vertical', 'horizontal', 'both']
|
Which orientations to detect |
'both'
|
threshold
|
Union[float, str]
|
Detection threshold ('auto' or float 0.0-1.0) - used for pixel detection |
'auto'
|
source_label
|
Optional[str]
|
Filter for line source (vector method) or label for detected lines (pixel method) |
None
|
max_lines_h
|
Optional[int]
|
Maximum number of horizontal lines to keep |
None
|
max_lines_v
|
Optional[int]
|
Maximum number of vertical lines to keep |
None
|
outer
|
bool
|
Whether to add outer boundary guides |
False
|
detection_method
|
str
|
'vector' (use existing LineElements) or 'pixels' (detect from image) |
'pixels'
|
resolution
|
int
|
DPI for pixel-based detection (default: 192) |
192
|
**detect_kwargs
|
Additional parameters for pixel-based detection: - min_gap_h: Minimum gap between horizontal lines (pixels) - min_gap_v: Minimum gap between vertical lines (pixels) - binarization_method: 'adaptive' or 'otsu' - morph_op_h/v: Morphological operations ('open', 'close', 'none') - smoothing_sigma_h/v: Gaussian smoothing sigma - method: 'projection' (default) or 'lsd' (requires opencv) |
{}
|
Returns:
| Type | Description |
|---|---|
Guides
|
New Guides object with detected line positions |
Source code in natural_pdf/analyzers/guides.py
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 | |
natural_pdf.Guides.from_whitespace(obj, axis='both', min_gap=10)
classmethod
Create guides by detecting whitespace gaps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Union[Page, Region, FlowRegion]
|
Page or Region to analyze |
required |
min_gap
|
float
|
Minimum gap size to consider as whitespace |
10
|
axis
|
Literal['vertical', 'horizontal', 'both']
|
Which axes to analyze for gaps |
'both'
|
Returns:
| Type | Description |
|---|---|
Guides
|
New Guides object positioned at whitespace gaps |
Source code in natural_pdf/analyzers/guides.py
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 | |
natural_pdf.Guides.get_cells()
Get all cell bounding boxes from guide intersections.
Returns:
| Type | Description |
|---|---|
List[Tuple[float, float, float, float]]
|
List of (x0, y0, x1, y1) tuples for each cell |
Source code in natural_pdf/analyzers/guides.py
3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 | |
natural_pdf.Guides.left_of(guide_index, obj=None)
Get a region to the left of a vertical guide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guide_index
|
int
|
Vertical guide index |
required |
obj
|
Optional[Union[Page, Region]]
|
Page or Region to create the region on (uses self.context if None) |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
Region to the left of the specified guide |
Source code in natural_pdf/analyzers/guides.py
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 | |
natural_pdf.Guides.new(context=None)
classmethod
Create a new empty Guides object, optionally with a context.
This provides a clean way to start building guides through chaining: guides = Guides.new(page).add_content(axis='vertical', markers=[...])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
Optional[Union[Page, Region]]
|
Optional Page or Region to use as default context for operations |
None
|
Returns:
| Type | Description |
|---|---|
Guides
|
New empty Guides object |
Source code in natural_pdf/analyzers/guides.py
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 | |
natural_pdf.Guides.remove_horizontal(index)
Remove a horizontal guide by index.
Source code in natural_pdf/analyzers/guides.py
2387 2388 2389 2390 2391 | |
natural_pdf.Guides.remove_vertical(index)
Remove a vertical guide by index.
Source code in natural_pdf/analyzers/guides.py
2381 2382 2383 2384 2385 | |
natural_pdf.Guides.right_of(guide_index, obj=None)
Get a region to the right of a vertical guide.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
guide_index
|
int
|
Vertical guide index |
required |
obj
|
Optional[Union[Page, Region]]
|
Page or Region to create the region on (uses self.context if None) |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
Region to the right of the specified guide |
Source code in natural_pdf/analyzers/guides.py
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 | |
natural_pdf.Guides.row(index, obj=None)
Get a row region from the guides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Row index (0-based) |
required |
obj
|
Optional[Union[Page, Region]]
|
Page or Region to create the row on (uses self.context if None) |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
Region representing the specified row |
Raises:
| Type | Description |
|---|---|
IndexError
|
If row index is out of range |
Source code in natural_pdf/analyzers/guides.py
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 | |
natural_pdf.Guides.shift(index, offset, axis='vertical')
Move a specific guide by a offset amount.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Index of the guide to move |
required |
offset
|
float
|
Amount to move (positive = right/down) |
required |
axis
|
Literal['vertical', 'horizontal']
|
Which guide list to modify |
'vertical'
|
Returns:
| Type | Description |
|---|---|
Guides
|
Self for method chaining |
Source code in natural_pdf/analyzers/guides.py
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 | |
natural_pdf.Guides.show(on=None, **kwargs)
Display the guides overlaid on a page or region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
on
|
Page, Region, PIL Image, or string to display guides on. If None, uses self.context (the object guides were created from). If string 'page', uses the page from self.context. |
None
|
|
**kwargs
|
Additional arguments passed to to_image() if applicable. |
{}
|
Returns:
| Type | Description |
|---|---|
|
PIL Image with guides drawn on it. |
Source code in natural_pdf/analyzers/guides.py
2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 | |
natural_pdf.Guides.snap_to_whitespace(axis='vertical', min_gap=10.0, detection_method='pixels', threshold='auto', on_no_snap='warn')
Snap guides to nearby whitespace gaps (troughs) using optimal assignment. Modifies this Guides object in place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axis
|
str
|
Direction to snap ('vertical' or 'horizontal') |
'vertical'
|
min_gap
|
float
|
Minimum gap size to consider as a valid trough |
10.0
|
detection_method
|
str
|
Method for detecting troughs: 'pixels' - use pixel-based density analysis (default) 'text' - use text element spacing analysis |
'pixels'
|
threshold
|
Union[float, str]
|
Threshold for what counts as a trough: - float (0.0-1.0): areas with this fraction or less of max density count as troughs - 'auto': automatically find threshold that creates enough troughs for guides |
'auto'
|
on_no_snap
|
str
|
Action when snapping fails ('warn', 'ignore', 'raise') |
'warn'
|
Returns:
| Type | Description |
|---|---|
Guides
|
Self for method chaining. |
Source code in natural_pdf/analyzers/guides.py
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 | |
natural_pdf.Guides.to_absolute(bounds)
Convert relative coordinates to absolute coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
Tuple[float, float, float, float]
|
Target bounding box (x0, y0, x1, y1) |
required |
Returns:
| Type | Description |
|---|---|
Guides
|
New Guides object with absolute coordinates |
Source code in natural_pdf/analyzers/guides.py
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 | |
natural_pdf.Guides.to_dict()
Convert to dictionary format suitable for pdfplumber table_settings.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with explicit_vertical_lines and explicit_horizontal_lines |
Source code in natural_pdf/analyzers/guides.py
3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 | |
natural_pdf.Guides.to_relative()
Convert absolute coordinates to relative (0-1) coordinates.
Returns:
| Type | Description |
|---|---|
Guides
|
New Guides object with relative coordinates |
Source code in natural_pdf/analyzers/guides.py
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 | |
natural_pdf.Judge
Visual classifier for regions using simple image metrics.
Requires class labels to be specified. For binary classification, requires at least one example of each class before making decisions.
Examples:
Checkbox detection:
judge = Judge("checkboxes", labels=["unchecked", "checked"])
judge.add(empty_box, "unchecked")
judge.add(marked_box, "checked")
result = judge.decide(new_box)
if result.label == "checked":
print("Box is checked!")
Signature detection:
judge = Judge("signatures", labels=["unsigned", "signed"])
judge.add(blank_area, "unsigned")
judge.add(signature_area, "signed")
result = judge.decide(new_region)
print(f"Classification: {result.label} (confidence: {result.score})")
Source code in natural_pdf/judge.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 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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 | |
Functions
natural_pdf.Judge.__init__(name, labels, base_dir=None, target_prior=None)
Initialize a Judge for visual classification.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name for this judge (used for folder name) |
required |
labels
|
List[str]
|
Class labels (required, typically 2 for binary classification) |
required |
base_dir
|
Optional[str]
|
Base directory for storage. Defaults to current directory |
None
|
target_prior
|
Optional[float]
|
Target prior probability for the FIRST label in the labels list. - 0.5 (default) = neutral, treats both classes equally - >0.5 = favors labels[0] - <0.5 = favors labels[1] Example: Judge("cb", ["checked", "unchecked"], target_prior=0.6) favors detecting "checked" checkboxes. |
None
|
Source code in natural_pdf/judge.py
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 | |
natural_pdf.Judge.add(region, label=None)
Add a region to the judge's dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Region object to add |
required | |
label
|
Optional[str]
|
Class label. If None, added to unlabeled for later teaching |
None
|
Raises:
| Type | Description |
|---|---|
JudgeError
|
If label is not in allowed labels |
Source code in natural_pdf/judge.py
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 | |
natural_pdf.Judge.count(target_label, regions)
Count how many regions match the target label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_label
|
str
|
The class label to count |
required |
regions
|
List[Region]
|
List of regions to check |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of regions classified as target_label |
Source code in natural_pdf/judge.py
558 559 560 561 562 563 564 565 566 567 568 569 570 | |
natural_pdf.Judge.decide(regions)
Classify one or more regions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regions
|
Union[Region, List[Region]]
|
Single region or list of regions to classify |
required |
Returns:
| Type | Description |
|---|---|
Union[Decision, List[Decision]]
|
Decision or list of Decisions with label and score |
Raises:
| Type | Description |
|---|---|
JudgeError
|
If not enough training examples |
Source code in natural_pdf/judge.py
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 | |
natural_pdf.Judge.forget(region=None, delete=False)
Clear training data, delete all files, or move a specific region to unlabeled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Optional[Region]
|
If provided, move this specific region to unlabeled |
None
|
delete
|
bool
|
If True, permanently delete all files |
False
|
Source code in natural_pdf/judge.py
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 | |
natural_pdf.Judge.info()
Show configuration and training information for this Judge.
Source code in natural_pdf/judge.py
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 | |
natural_pdf.Judge.inspect(preview=True)
Inspect all stored examples, showing their true labels and predicted labels/scores. Useful for debugging classification issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preview
|
bool
|
If True (default), display images inline in HTML tables (requires IPython/Jupyter). If False, use text-only output. |
True
|
Source code in natural_pdf/judge.py
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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 | |
natural_pdf.Judge.load(path)
classmethod
Load a judge from a saved configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the saved judge.json file or the judge directory |
required |
Returns:
| Type | Description |
|---|---|
Judge
|
Loaded Judge instance |
Source code in natural_pdf/judge.py
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 | |
natural_pdf.Judge.lookup(region)
Look up a region and return its hash and image if found in training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Region to look up |
required |
Returns:
| Type | Description |
|---|---|
Optional[Tuple[str, Image]]
|
Tuple of (hash, image) if found, None if not found |
Source code in natural_pdf/judge.py
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 | |
natural_pdf.Judge.pick(target_label, regions, labels=None)
Pick which region best matches the target label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_label
|
str
|
The class label to look for |
required |
regions
|
List[Region]
|
List of regions to choose from |
required |
labels
|
Optional[List[str]]
|
Optional human-friendly labels for each region |
None
|
Returns:
| Type | Description |
|---|---|
PickResult
|
PickResult with winning region, index, label (if provided), and score |
Raises:
| Type | Description |
|---|---|
JudgeError
|
If target_label not in allowed labels |
Source code in natural_pdf/judge.py
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 | |
natural_pdf.Judge.save(path=None)
Save the judge configuration (auto-retrains first).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Optional[str]
|
Optional path to save to. Defaults to judge.json in root directory |
None
|
Source code in natural_pdf/judge.py
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 | |
natural_pdf.Judge.show(max_per_class=10, size=(100, 100))
Display a grid showing examples from each category.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_per_class
|
int
|
Maximum number of examples to show per class |
10
|
size
|
Tuple[int, int]
|
Size of each image in pixels (width, height) |
(100, 100)
|
Source code in natural_pdf/judge.py
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 | |
natural_pdf.Judge.teach(labels=None, review=False)
Interactive teaching interface using IPython widgets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Optional[List[str]]
|
Labels to use for teaching. Defaults to self.labels |
None
|
review
|
bool
|
If True, review already labeled images for re-classification |
False
|
Source code in natural_pdf/judge.py
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 | |
natural_pdf.JudgeError
Bases: Exception
Raised when Judge operations fail.
Source code in natural_pdf/judge.py
27 28 29 30 | |
natural_pdf.Options
Global options for natural-pdf, similar to pandas options.
Source code in natural_pdf/__init__.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
natural_pdf.PDF
Bases: TextMixin, ExtractionMixin, ExportMixin, ClassificationMixin, CheckboxDetectionMixin, VisualSearchMixin, Visualizable
Enhanced PDF wrapper built on top of pdfplumber.
This class provides a fluent interface for working with PDF documents, with improved selection, navigation, and extraction capabilities. It integrates OCR, layout analysis, and AI-powered data extraction features while maintaining compatibility with the underlying pdfplumber API.
The PDF class supports loading from files, URLs, or streams, and provides spatial navigation, element selection with CSS-like selectors, and advanced document processing workflows including multi-page content flows.
Attributes:
| Name | Type | Description |
|---|---|---|
pages |
PageCollection
|
Lazy-loaded list of Page objects for document pages. |
path |
Resolved path to the PDF file or source identifier. |
|
source_path |
Original path, URL, or stream identifier provided during initialization. |
|
highlighter |
Service for rendering highlighted visualizations of document content. |
Example
Basic usage:
import natural_pdf as npdf
pdf = npdf.PDF("document.pdf")
page = pdf.pages[0]
text_elements = page.find_all('text:contains("Summary")')
Advanced usage with OCR:
pdf = npdf.PDF("scanned_document.pdf")
pdf.apply_ocr(engine="easyocr", resolution=144)
tables = pdf.pages[0].find_all('table')
Source code in natural_pdf/core/pdf.py
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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 | |
Attributes
natural_pdf.PDF.metadata
property
Access PDF metadata as a dictionary.
Returns document metadata such as title, author, creation date, and other properties embedded in the PDF file. The exact keys available depend on what metadata was included when the PDF was created.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary containing PDF metadata. Common keys include 'Title', |
Dict[str, Any]
|
'Author', 'Subject', 'Creator', 'Producer', 'CreationDate', and |
Dict[str, Any]
|
'ModDate'. May be empty if no metadata is available. |
Example
pdf = npdf.PDF("document.pdf")
print(pdf.metadata.get('Title', 'No title'))
print(f"Created: {pdf.metadata.get('CreationDate')}")
natural_pdf.PDF.pages
property
Access pages as a PageCollection object.
Provides access to individual pages of the PDF document through a collection interface that supports indexing, slicing, and iteration. Pages are lazy-loaded to minimize memory usage.
Returns:
| Type | Description |
|---|---|
PageCollection
|
PageCollection object that provides list-like access to PDF pages. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If PDF pages are not yet initialized. |
Example
pdf = npdf.PDF("document.pdf")
# Access individual pages
first_page = pdf.pages[0]
last_page = pdf.pages[-1]
# Slice pages
first_three = pdf.pages[0:3]
# Iterate over pages
for page in pdf.pages:
print(f"Page {page.index} has {len(page.chars)} characters")
Functions
natural_pdf.PDF.__enter__()
Context manager entry.
Source code in natural_pdf/core/pdf.py
2122 2123 2124 | |
natural_pdf.PDF.__exit__(exc_type, exc_val, exc_tb)
Context manager exit.
Source code in natural_pdf/core/pdf.py
2126 2127 2128 | |
natural_pdf.PDF.__getitem__(key)
Access pages by index or slice.
Source code in natural_pdf/core/pdf.py
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 | |
natural_pdf.PDF.__init__(path_or_url_or_stream, reading_order=True, font_attrs=None, keep_spaces=True, text_tolerance=None, auto_text_tolerance=True, text_layer=True)
Initialize the enhanced PDF object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path_or_url_or_stream
|
Path to the PDF file (str/Path), a URL (str), or a file-like object (stream). URLs must start with 'http://' or 'https://'. |
required | |
reading_order
|
bool
|
If True, use natural reading order for text extraction. Defaults to True. |
True
|
font_attrs
|
Optional[List[str]]
|
List of font attributes for grouping characters into words. Common attributes include ['fontname', 'size']. Defaults to None. |
None
|
keep_spaces
|
bool
|
If True, include spaces in word elements during text extraction. Defaults to True. |
True
|
text_tolerance
|
Optional[dict]
|
PDFplumber-style tolerance settings for text grouping. Dictionary with keys like 'x_tolerance', 'y_tolerance'. Defaults to None. |
None
|
auto_text_tolerance
|
bool
|
If True, automatically scale text tolerance based on font size and document characteristics. Defaults to True. |
True
|
text_layer
|
bool
|
If True, preserve existing text layer from the PDF. If False, removes all existing text elements during initialization, useful for OCR-only workflows. Defaults to True. |
True
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If path_or_url_or_stream is not a valid type. |
IOError
|
If the PDF file cannot be opened or read. |
ValueError
|
If URL download fails. |
Example
# From file path
pdf = npdf.PDF("document.pdf")
# From URL
pdf = npdf.PDF("https://example.com/document.pdf")
# From stream
with open("document.pdf", "rb") as f:
pdf = npdf.PDF(f)
# With custom settings
pdf = npdf.PDF("document.pdf",
reading_order=False,
text_layer=False, # For OCR-only processing
font_attrs=['fontname', 'size', 'flags'])
Source code in natural_pdf/core/pdf.py
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 | |
natural_pdf.PDF.__len__()
Return the number of pages in the PDF.
Source code in natural_pdf/core/pdf.py
2069 2070 2071 2072 2073 | |
natural_pdf.PDF.__repr__()
Return a string representation of the PDF object.
Source code in natural_pdf/core/pdf.py
2130 2131 2132 2133 2134 2135 2136 2137 2138 | |
natural_pdf.PDF.add_exclusion(exclusion_func, label=None)
Add an exclusion function to the PDF.
Exclusion functions define regions of each page that should be ignored during text extraction and analysis operations. This is useful for filtering out headers, footers, watermarks, or other administrative content that shouldn't be included in the main document processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exclusion_func
|
A function that takes a Page object and returns a Region to exclude from processing, or None if no exclusion should be applied to that page. The function is called once per page. |
required | |
label
|
str
|
Optional descriptive label for this exclusion rule, useful for debugging and identification. |
None
|
Returns:
| Type | Description |
|---|---|
PDF
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If PDF pages are not yet initialized. |
Example
pdf = npdf.PDF("document.pdf")
# Exclude headers (top 50 points of each page)
pdf.add_exclusion(
lambda page: page.region(0, 0, page.width, 50),
label="header_exclusion"
)
# Exclude any text containing "CONFIDENTIAL"
pdf.add_exclusion(
lambda page: page.find('text:contains("CONFIDENTIAL")').above(include_source=True)
if page.find('text:contains("CONFIDENTIAL")') else None,
label="confidential_exclusion"
)
# Chain multiple exclusions
pdf.add_exclusion(header_func).add_exclusion(footer_func)
Source code in natural_pdf/core/pdf.py
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 | |
natural_pdf.PDF.add_region(region_func, name=None)
Add a region function to the PDF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region_func
|
Callable[[Page], Optional[Region]]
|
A function that takes a Page and returns a Region, or None |
required |
name
|
str
|
Optional name for the region |
None
|
Returns:
| Type | Description |
|---|---|
PDF
|
Self for method chaining |
Source code in natural_pdf/core/pdf.py
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 | |
natural_pdf.PDF.analyze_layout(*args, **kwargs)
Analyzes the layout of all pages in the PDF.
This is a convenience method that calls analyze_layout on the PDF's page collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments passed to pages.analyze_layout(). |
()
|
|
**kwargs
|
Keyword arguments passed to pages.analyze_layout(). |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
An ElementCollection of all detected Region objects. |
Source code in natural_pdf/core/pdf.py
2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 | |
natural_pdf.PDF.apply_ocr(engine=None, languages=None, min_confidence=None, device=None, resolution=None, apply_exclusions=True, detect_only=False, replace=True, options=None, pages=None)
Apply OCR to specified pages of the PDF using batch processing.
Performs optical character recognition on the specified pages, converting image-based text into searchable and extractable text elements. This method supports multiple OCR engines and provides batch processing for efficiency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Optional[str]
|
Name of the OCR engine to use. Supported engines include 'easyocr' (default), 'surya', 'paddle', and 'doctr'. If None, uses the global default from natural_pdf.options.ocr.engine. |
None
|
languages
|
Optional[List[str]]
|
List of language codes for OCR recognition (e.g., ['en', 'es']). If None, uses the global default from natural_pdf.options.ocr.languages. |
None
|
min_confidence
|
Optional[float]
|
Minimum confidence threshold (0.0-1.0) for accepting OCR results. Text with lower confidence will be filtered out. If None, uses the global default. |
None
|
device
|
Optional[str]
|
Device to run OCR on ('cpu', 'cuda', 'mps'). Engine-specific availability varies. If None, uses engine defaults. |
None
|
resolution
|
Optional[int]
|
DPI resolution for rendering pages to images before OCR. Higher values improve accuracy but increase processing time and memory. Typical values: 150 (fast), 300 (balanced), 600 (high quality). |
None
|
apply_exclusions
|
bool
|
If True, mask excluded regions before OCR to prevent processing of headers, footers, or other unwanted content. |
True
|
detect_only
|
bool
|
If True, only detect text bounding boxes without performing character recognition. Useful for layout analysis workflows. |
False
|
replace
|
bool
|
If True, replace any existing OCR elements on the pages. If False, append new OCR results to existing elements. |
True
|
options
|
Optional[Any]
|
Engine-specific options object (e.g., EasyOCROptions, SuryaOptions). Allows fine-tuning of engine behavior beyond common parameters. |
None
|
pages
|
Optional[Union[Iterable[int], range, slice]]
|
Page indices to process. Can be: - None: Process all pages - slice: Process a range of pages (e.g., slice(0, 10)) - Iterable[int]: Process specific page indices (e.g., [0, 2, 5]) |
None
|
Returns:
| Type | Description |
|---|---|
PDF
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If invalid page index is provided. |
TypeError
|
If pages parameter has invalid type. |
RuntimeError
|
If OCR engine is not available or fails. |
Example
pdf = npdf.PDF("scanned_document.pdf")
# Basic OCR on all pages
pdf.apply_ocr()
# High-quality OCR with specific settings
pdf.apply_ocr(
engine='easyocr',
languages=['en', 'es'],
resolution=300,
min_confidence=0.8
)
# OCR specific pages only
pdf.apply_ocr(pages=[0, 1, 2]) # First 3 pages
pdf.apply_ocr(pages=slice(5, 10)) # Pages 5-9
# Detection-only workflow for layout analysis
pdf.apply_ocr(detect_only=True, resolution=150)
Note
OCR processing can be time and memory intensive, especially at high resolutions. Consider using exclusions to mask unwanted regions and processing pages in batches for large documents.
Source code in natural_pdf/core/pdf.py
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 | |
natural_pdf.PDF.ask(question, mode='extractive', pages=None, min_confidence=0.1, model=None, **kwargs)
Ask a single question about the document content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
Question string to ask about the document |
required |
mode
|
str
|
"extractive" to extract answer from document, "generative" to generate |
'extractive'
|
pages
|
Union[int, List[int], range]
|
Specific pages to query (default: all pages) |
None
|
min_confidence
|
float
|
Minimum confidence threshold for answers |
0.1
|
model
|
str
|
Optional model name for question answering |
None
|
**kwargs
|
Additional parameters passed to the QA engine |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict containing: answer, confidence, found, page_num, source_elements, etc. |
Source code in natural_pdf/core/pdf.py
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 | |
natural_pdf.PDF.ask_batch(questions, mode='extractive', pages=None, min_confidence=0.1, model=None, **kwargs)
Ask multiple questions about the document content using batch processing.
This method processes multiple questions efficiently in a single batch, avoiding the multiprocessing resource accumulation that can occur with sequential individual question calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
questions
|
List[str]
|
List of question strings to ask about the document |
required |
mode
|
str
|
"extractive" to extract answer from document, "generative" to generate |
'extractive'
|
pages
|
Union[int, List[int], range]
|
Specific pages to query (default: all pages) |
None
|
min_confidence
|
float
|
Minimum confidence threshold for answers |
0.1
|
model
|
str
|
Optional model name for question answering |
None
|
**kwargs
|
Additional parameters passed to the QA engine |
{}
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of Dicts, each containing: answer, confidence, found, page_num, source_elements, etc. |
Source code in natural_pdf/core/pdf.py
1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 | |
natural_pdf.PDF.classify_pages(labels, model=None, pages=None, analysis_key='classification', using=None, **kwargs)
Classifies specified pages of the PDF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
List[str]
|
List of category names |
required |
model
|
Optional[str]
|
Model identifier ('text', 'vision', or specific HF ID) |
None
|
pages
|
Optional[Union[Iterable[int], range, slice]]
|
Page indices, slice, or None for all pages |
None
|
analysis_key
|
str
|
Key to store results in page's analyses dict |
'classification'
|
using
|
Optional[str]
|
Processing mode ('text' or 'vision') |
None
|
**kwargs
|
Additional arguments for the ClassificationManager |
{}
|
Returns:
| Type | Description |
|---|---|
PDF
|
Self for method chaining |
Source code in natural_pdf/core/pdf.py
2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 | |
natural_pdf.PDF.clear_exclusions()
Clear all exclusion functions from the PDF.
Removes all previously added exclusion functions that were used to filter out unwanted content (like headers, footers, or administrative text) from text extraction and analysis operations.
Returns:
| Type | Description |
|---|---|
PDF
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
AttributeError
|
If PDF pages are not yet initialized. |
Example
pdf = npdf.PDF("document.pdf")
pdf.add_exclusion(lambda page: page.find('text:contains("CONFIDENTIAL")').above())
# Later, remove all exclusions
pdf.clear_exclusions()
Source code in natural_pdf/core/pdf.py
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 | |
natural_pdf.PDF.close()
Close the underlying PDF file and clean up any temporary files.
Source code in natural_pdf/core/pdf.py
2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 | |
natural_pdf.PDF.deskew(pages=None, resolution=300, angle=None, detection_resolution=72, force_overwrite=False, **deskew_kwargs)
Creates a new, in-memory PDF object containing deskewed versions of the specified pages from the original PDF.
This method renders each selected page, detects and corrects skew using the 'deskew' library, and then combines the resulting images into a new PDF using 'img2pdf'. The new PDF object is returned directly.
Important: The returned PDF is image-based. Any existing text, OCR results, annotations, or other elements from the original pages will not be carried over.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pages
|
Optional[Union[Iterable[int], range, slice]]
|
Page indices/slice to include (0-based). If None, processes all pages. |
None
|
resolution
|
int
|
DPI resolution for rendering the output deskewed pages. |
300
|
angle
|
Optional[float]
|
The specific angle (in degrees) to rotate by. If None, detects automatically. |
None
|
detection_resolution
|
int
|
DPI resolution used for skew detection if angles are not already cached on the page objects. |
72
|
force_overwrite
|
bool
|
If False (default), raises a ValueError if any target page already contains processed elements (text, OCR, regions) to prevent accidental data loss. Set to True to proceed anyway. |
False
|
**deskew_kwargs
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
PDF
|
A new PDF object representing the deskewed document. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If 'deskew' or 'img2pdf' libraries are not installed. |
ValueError
|
If |
FileNotFoundError
|
If the source PDF cannot be read (if file-based). |
IOError
|
If creating the in-memory PDF fails. |
RuntimeError
|
If rendering or deskewing individual pages fails. |
Source code in natural_pdf/core/pdf.py
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 | |
natural_pdf.PDF.detect_checkboxes(*args, **kwargs)
Detects checkboxes on all pages in the PDF.
This is a convenience method that calls detect_checkboxes on the PDF's page collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments passed to pages.detect_checkboxes(). |
()
|
|
**kwargs
|
Keyword arguments passed to pages.detect_checkboxes(). |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
An ElementCollection of all detected checkbox Region objects. |
Source code in natural_pdf/core/pdf.py
2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 | |
natural_pdf.PDF.export_ocr_correction_task(output_zip_path, **kwargs)
Exports OCR results from this PDF into a correction task package. Exports OCR results from this PDF into a correction task package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_zip_path
|
str
|
The path to save the output zip file |
required |
output_zip_path
|
str
|
The path to save the output zip file |
required |
**kwargs
|
Additional arguments passed to create_correction_task_package |
{}
|
Source code in natural_pdf/core/pdf.py
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 | |
natural_pdf.PDF.extract_tables(selector=None, merge_across_pages=False, **kwargs)
Extract tables from the document or matching elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
Optional selector to filter tables |
None
|
merge_across_pages
|
bool
|
Whether to merge tables that span across pages |
False
|
**kwargs
|
Additional extraction parameters |
{}
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
List of extracted tables |
Source code in natural_pdf/core/pdf.py
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 | |
natural_pdf.PDF.extract_text(selector=None, preserve_whitespace=True, use_exclusions=True, debug_exclusions=False, **kwargs)
Extract text from the entire document or matching elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
Optional selector to filter elements |
None
|
preserve_whitespace
|
Whether to keep blank characters |
True
|
|
use_exclusions
|
Whether to apply exclusion regions |
True
|
|
debug_exclusions
|
Whether to output detailed debugging for exclusions |
False
|
|
preserve_whitespace
|
Whether to keep blank characters |
True
|
|
use_exclusions
|
Whether to apply exclusion regions |
True
|
|
debug_exclusions
|
Whether to output detailed debugging for exclusions |
False
|
|
**kwargs
|
Additional extraction parameters |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Extracted text as string |
Source code in natural_pdf/core/pdf.py
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 | |
natural_pdf.PDF.find(selector=None, *, text=None, apply_exclusions=True, regex=False, case=True, **kwargs)
find(*, text: str, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> Optional[Any]
find(selector: str, *, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> Optional[Any]
Find the first element matching the selector OR text content across all pages.
Provide EITHER selector OR text, but not both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string. |
None
|
text
|
Optional[str]
|
Text content to search for (equivalent to 'text:contains(...)'). |
None
|
apply_exclusions
|
bool
|
Whether to exclude elements in exclusion regions (default: True). |
True
|
regex
|
bool
|
Whether to use regex for text search ( |
False
|
case
|
bool
|
Whether to do case-sensitive text search ( |
True
|
**kwargs
|
Additional filter parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Element object or None if not found. |
Source code in natural_pdf/core/pdf.py
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 | |
natural_pdf.PDF.find_all(selector=None, *, text=None, apply_exclusions=True, regex=False, case=True, **kwargs)
find_all(*, text: str, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
find_all(selector: str, *, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
Find all elements matching the selector OR text content across all pages.
Provide EITHER selector OR text, but not both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string. |
None
|
text
|
Optional[str]
|
Text content to search for (equivalent to 'text:contains(...)'). |
None
|
apply_exclusions
|
bool
|
Whether to exclude elements in exclusion regions (default: True). |
True
|
regex
|
bool
|
Whether to use regex for text search ( |
False
|
case
|
bool
|
Whether to do case-sensitive text search ( |
True
|
**kwargs
|
Additional filter parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection with matching elements. |
Source code in natural_pdf/core/pdf.py
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 | |
natural_pdf.PDF.from_images(images, resolution=300, apply_ocr=True, ocr_engine=None, **pdf_options)
classmethod
Create a PDF from image(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Union[Image, List[Image], str, List[str], Path, List[Path]]
|
Single image, list of images, or path(s)/URL(s) to image files |
required |
resolution
|
int
|
DPI for the PDF (default: 300, good for OCR and viewing) |
300
|
apply_ocr
|
bool
|
Apply OCR to make searchable (default: True) |
True
|
ocr_engine
|
Optional[str]
|
OCR engine to use (default: auto-detect) |
None
|
**pdf_options
|
Options passed to PDF constructor |
{}
|
Returns:
| Type | Description |
|---|---|
PDF
|
PDF object containing the images as pages |
Example
# Simple scan to searchable PDF
pdf = PDF.from_images("scan.jpg")
# From URL
pdf = PDF.from_images("https://example.com/image.png")
# Multiple pages (mix of local and URLs)
pdf = PDF.from_images(["page1.png", "https://example.com/page2.jpg"])
# Without OCR
pdf = PDF.from_images(images, apply_ocr=False)
# With specific engine
pdf = PDF.from_images(images, ocr_engine='surya')
Source code in natural_pdf/core/pdf.py
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 | |
natural_pdf.PDF.get_id()
Get unique identifier for this PDF.
Source code in natural_pdf/core/pdf.py
2140 2141 2142 2143 | |
natural_pdf.PDF.get_manager(key)
Retrieve a manager instance by its key, instantiating it lazily if needed.
Managers are specialized components that handle specific functionality like classification, structured data extraction, or OCR processing. They are instantiated on-demand to minimize memory usage and startup time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The manager key to retrieve. Common keys include 'classification' and 'structured_data'. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The manager instance for the specified key. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no manager is registered for the given key. |
RuntimeError
|
If the manager failed to initialize. |
Example
pdf = npdf.PDF("document.pdf")
classification_mgr = pdf.get_manager('classification')
structured_data_mgr = pdf.get_manager('structured_data')
Source code in natural_pdf/core/pdf.py
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 | |
natural_pdf.PDF.get_sections(start_elements=None, end_elements=None, new_section_on_page_break=False, include_boundaries='both', orientation='vertical')
Extract sections from the entire PDF based on start/end elements.
This method delegates to the PageCollection.get_sections() method, providing a convenient way to extract document sections across all pages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_elements
|
Elements or selector string that mark the start of sections (optional) |
None
|
|
end_elements
|
Elements or selector string that mark the end of sections (optional) |
None
|
|
new_section_on_page_break
|
Whether to start a new section at page boundaries (default: False) |
False
|
|
include_boundaries
|
How to include boundary elements: 'start', 'end', 'both', or 'none' (default: 'both') |
'both'
|
|
orientation
|
'vertical' (default) or 'horizontal' - determines section direction |
'vertical'
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection of Region objects representing the extracted sections |
Example
Extract sections between headers:
pdf = npdf.PDF("document.pdf")
# Get sections between headers
sections = pdf.get_sections(
start_elements='text[size>14]:bold',
end_elements='text[size>14]:bold'
)
# Get sections that break at page boundaries
sections = pdf.get_sections(
start_elements='text:contains("Chapter")',
new_section_on_page_break=True
)
Note
You can provide only start_elements, only end_elements, or both. - With only start_elements: sections go from each start to the next start (or end of document) - With only end_elements: sections go from beginning of document to each end - With both: sections go from each start to the corresponding end
Source code in natural_pdf/core/pdf.py
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 | |
natural_pdf.PDF.highlights(show=False)
Create a highlight context for accumulating highlights.
This allows for clean syntax to show multiple highlight groups:
Example
with pdf.highlights() as h: h.add(pdf.find_all('table'), label='tables', color='blue') h.add(pdf.find_all('text:bold'), label='bold text', color='red') h.show()
Or with automatic display
with pdf.highlights(show=True) as h: h.add(pdf.find_all('table'), label='tables') h.add(pdf.find_all('text:bold'), label='bold') # Automatically shows when exiting the context
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show
|
bool
|
If True, automatically show highlights when exiting context |
False
|
Returns:
| Type | Description |
|---|---|
HighlightContext
|
HighlightContext for accumulating highlights |
Source code in natural_pdf/core/pdf.py
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 | |
natural_pdf.PDF.save_pdf(output_path, ocr=False, original=False, dpi=300)
Saves the PDF object (all its pages) to a new file.
Choose one saving mode:
- ocr=True: Creates a new, image-based PDF using OCR results from all pages.
Text generated during the natural-pdf session becomes searchable,
but original vector content is lost. Requires 'ocr-export' extras.
- original=True: Saves a copy of the original PDF file this object represents.
Any OCR results or analyses from the natural-pdf session are NOT included.
If the PDF was opened from an in-memory buffer, this mode may not be suitable.
Requires 'ocr-export' extras.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
Union[str, Path]
|
Path to save the new PDF file. |
required |
ocr
|
bool
|
If True, save as a searchable, image-based PDF using OCR data. |
False
|
original
|
bool
|
If True, save the original source PDF content. |
False
|
dpi
|
int
|
Resolution (dots per inch) used only when ocr=True. |
300
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the PDF has no pages, if neither or both 'ocr' and 'original' are True. |
ImportError
|
If required libraries are not installed for the chosen mode. |
RuntimeError
|
If an unexpected error occurs during saving. |
Source code in natural_pdf/core/pdf.py
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 | |
natural_pdf.PDF.save_searchable(output_path, dpi=300, **kwargs)
DEPRECATED: Use save_pdf(..., ocr=True) instead. Saves the PDF with an OCR text layer, making content searchable.
Requires optional dependencies. Install with: pip install "natural-pdf[ocr-export]"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
Union[str, Path]
|
Path to save the searchable PDF |
required |
dpi
|
int
|
Resolution for rendering and OCR overlay |
300
|
**kwargs
|
Additional keyword arguments passed to the exporter |
{}
|
Source code in natural_pdf/core/pdf.py
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 | |
natural_pdf.PDF.search_within_index(query, search_service, options=None)
Finds relevant documents from this PDF within a search index. Finds relevant documents from this PDF within a search index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Union[str, Path, Image, Region]
|
The search query (text, image path, PIL Image, Region) |
required |
search_service
|
SearchServiceProtocol
|
A pre-configured SearchService instance |
required |
options
|
Optional[SearchOptions]
|
Optional SearchOptions to configure the query |
None
|
query
|
Union[str, Path, Image, Region]
|
The search query (text, image path, PIL Image, Region) |
required |
search_service
|
SearchServiceProtocol
|
A pre-configured SearchService instance |
required |
options
|
Optional[SearchOptions]
|
Optional SearchOptions to configure the query |
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
A list of result dictionaries, sorted by relevance |
List[Dict[str, Any]]
|
A list of result dictionaries, sorted by relevance |
Raises:
| Type | Description |
|---|---|
ImportError
|
If search dependencies are not installed |
ValueError
|
If search_service is None |
TypeError
|
If search_service does not conform to the protocol |
FileNotFoundError
|
If the collection managed by the service does not exist |
RuntimeError
|
For other search failures |
ImportError
|
If search dependencies are not installed |
ValueError
|
If search_service is None |
TypeError
|
If search_service does not conform to the protocol |
FileNotFoundError
|
If the collection managed by the service does not exist |
RuntimeError
|
For other search failures |
Source code in natural_pdf/core/pdf.py
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 | |
natural_pdf.PDF.split(divider, **kwargs)
Divide the PDF into sections based on the provided divider elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
divider
|
Elements or selector string that mark section boundaries |
required | |
**kwargs
|
Additional parameters passed to get_sections() - include_boundaries: How to include boundary elements (default: 'start') - orientation: 'vertical' or 'horizontal' (default: 'vertical') - new_section_on_page_break: Whether to split at page boundaries (default: False) |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection of Region objects representing the sections |
Example
Split a PDF by chapter titles
chapters = pdf.split("text[size>20]:contains('Chapter')")
Export each chapter to a separate file
for i, chapter in enumerate(chapters): chapter_text = chapter.extract_text() with open(f"chapter_{i+1}.txt", "w") as f: f.write(chapter_text)
Split by horizontal rules/lines
sections = pdf.split("line[orientation=horizontal]")
Split only by page breaks (no divider elements)
pages = pdf.split(None, new_section_on_page_break=True)
Source code in natural_pdf/core/pdf.py
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 | |
natural_pdf.PDF.update_text(transform, pages=None, selector='text', max_workers=None, progress_callback=None)
Applies corrections to text elements using a callback function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
correction_callback
|
Function that takes an element and returns corrected text or None |
required | |
pages
|
Optional[Union[Iterable[int], range, slice]]
|
Optional page indices/slice to limit the scope of correction |
None
|
selector
|
str
|
Selector to apply corrections to (default: "text") |
'text'
|
max_workers
|
Optional[int]
|
Maximum number of threads to use for parallel execution |
None
|
progress_callback
|
Optional[Callable[[], None]]
|
Optional callback function for progress updates |
None
|
Returns:
| Type | Description |
|---|---|
PDF
|
Self for method chaining |
Source code in natural_pdf/core/pdf.py
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 | |
natural_pdf.PDFCollection
Bases: SearchableMixin, ApplyMixin, ExportMixin, ShapeDetectionMixin, VisualSearchMixin
Source code in natural_pdf/core/pdf_collection.py
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 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 | |
Attributes
natural_pdf.PDFCollection.pdfs
property
Returns the list of PDF objects held by the collection.
Functions
natural_pdf.PDFCollection.__init__(source, recursive=True, **pdf_options)
Initializes a collection of PDF documents from various sources.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Union[str, Iterable[Union[str, PDF]]]
|
The source of PDF documents. Can be: - An iterable (e.g., list) of existing PDF objects. - An iterable (e.g., list) of file paths/URLs/globs (strings). - A single file path/URL/directory/glob string. |
required |
recursive
|
bool
|
If source involves directories or glob patterns, whether to search recursively (default: True). |
True
|
**pdf_options
|
Any
|
Keyword arguments passed to the PDF constructor. |
{}
|
Source code in natural_pdf/core/pdf_collection.py
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 | |
natural_pdf.PDFCollection.apply_ocr(engine=None, languages=None, min_confidence=None, device=None, resolution=None, apply_exclusions=True, detect_only=False, replace=True, options=None, pages=None, max_workers=None)
Apply OCR to all PDFs in the collection, potentially in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Optional[str]
|
OCR engine to use (e.g., 'easyocr', 'paddleocr', 'surya') |
None
|
languages
|
Optional[List[str]]
|
List of language codes for OCR |
None
|
min_confidence
|
Optional[float]
|
Minimum confidence threshold for text detection |
None
|
device
|
Optional[str]
|
Device to use for OCR (e.g., 'cpu', 'cuda') |
None
|
resolution
|
Optional[int]
|
DPI resolution for page rendering |
None
|
apply_exclusions
|
bool
|
Whether to apply exclusion regions |
True
|
detect_only
|
bool
|
If True, only detect text regions without extracting text |
False
|
replace
|
bool
|
If True, replace existing OCR elements |
True
|
options
|
Optional[Any]
|
Engine-specific options |
None
|
pages
|
Optional[Union[slice, List[int]]]
|
Specific pages to process (None for all pages) |
None
|
max_workers
|
Optional[int]
|
Maximum number of threads to process PDFs concurrently. If None or 1, processing is sequential. (default: None) |
None
|
Returns:
| Type | Description |
|---|---|
PDFCollection
|
Self for method chaining |
Source code in natural_pdf/core/pdf_collection.py
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 | |
natural_pdf.PDFCollection.categorize(labels, **kwargs)
Categorizes PDFs in the collection based on content or features.
Source code in natural_pdf/core/pdf_collection.py
633 634 635 636 | |
natural_pdf.PDFCollection.classify_all(labels, using=None, model=None, analysis_key='classification', **kwargs)
Classify each PDF document in the collection using batch processing.
This method gathers content from all PDFs and processes them in a single batch to avoid multiprocessing resource accumulation that can occur with sequential individual classifications.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
List[str]
|
A list of string category names. |
required |
using
|
Optional[str]
|
Processing mode ('text', 'vision'). If None, manager infers (defaulting to text). |
None
|
model
|
Optional[str]
|
Optional specific model identifier (e.g., HF ID). If None, manager uses default for 'using' mode. |
None
|
analysis_key
|
str
|
Key under which to store the ClassificationResult in each PDF's |
'classification'
|
**kwargs
|
Additional arguments passed down to the ClassificationManager. |
{}
|
Returns:
| Type | Description |
|---|---|
PDFCollection
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If labels list is empty, or if using='vision' on a multi-page PDF. |
ClassificationError
|
If classification fails. |
ImportError
|
If classification dependencies are missing. |
Source code in natural_pdf/core/pdf_collection.py
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 814 815 816 817 | |
natural_pdf.PDFCollection.correct_ocr(correction_callback, max_workers=None, progress_callback=None)
Apply OCR correction to all relevant elements across all pages and PDFs in the collection using a single progress bar.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
correction_callback
|
Callable[[Any], Optional[str]]
|
Function to apply to each OCR element. It receives the element and should return the corrected text (str) or None. |
required |
max_workers
|
Optional[int]
|
Max threads to use for parallel execution within each page. |
None
|
progress_callback
|
Optional[Callable[[], None]]
|
Optional callback function to call after processing each element. |
None
|
Returns:
| Type | Description |
|---|---|
PDFCollection
|
Self for method chaining. |
Source code in natural_pdf/core/pdf_collection.py
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 | |
natural_pdf.PDFCollection.export_ocr_correction_task(output_zip_path, **kwargs)
Exports OCR results from all PDFs in this collection into a single correction task package (zip file).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_zip_path
|
str
|
The path to save the output zip file. |
required |
**kwargs
|
Additional arguments passed to create_correction_task_package (e.g., image_render_scale, overwrite). |
{}
|
Source code in natural_pdf/core/pdf_collection.py
638 639 640 641 642 643 644 645 646 647 648 649 650 651 | |
natural_pdf.PDFCollection.find_all(selector=None, *, text=None, apply_exclusions=True, regex=False, case=True, **kwargs)
find_all(*, text: str, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
find_all(selector: str, *, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
Find all elements matching the selector OR text across all PDFs in the collection.
Provide EITHER selector OR text, but not both.
This creates an ElementCollection that can span multiple PDFs. Note that some ElementCollection methods have limitations when spanning PDFs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string to query elements. |
None
|
text
|
Optional[str]
|
Text content to search for (equivalent to 'text:contains(...)'). |
None
|
apply_exclusions
|
bool
|
Whether to exclude elements in exclusion regions (default: True). |
True
|
regex
|
bool
|
Whether to use regex for text search ( |
False
|
case
|
bool
|
Whether to do case-sensitive text search ( |
True
|
**kwargs
|
Additional keyword arguments passed to the find_all method of each PDF. |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection containing all matching elements across all PDFs. |
Source code in natural_pdf/core/pdf_collection.py
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 | |
natural_pdf.PDFCollection.from_directory(directory_path, recursive=True, **pdf_options)
classmethod
Creates a PDFCollection explicitly from PDF files within a directory.
Source code in natural_pdf/core/pdf_collection.py
226 227 228 229 230 231 232 | |
natural_pdf.PDFCollection.from_glob(pattern, recursive=True, **pdf_options)
classmethod
Creates a PDFCollection explicitly from a single glob pattern.
Source code in natural_pdf/core/pdf_collection.py
212 213 214 215 216 | |
natural_pdf.PDFCollection.from_globs(patterns, recursive=True, **pdf_options)
classmethod
Creates a PDFCollection explicitly from a list of glob patterns.
Source code in natural_pdf/core/pdf_collection.py
218 219 220 221 222 223 224 | |
natural_pdf.PDFCollection.from_paths(paths_or_urls, **pdf_options)
classmethod
Creates a PDFCollection explicitly from a list of file paths or URLs.
Source code in natural_pdf/core/pdf_collection.py
206 207 208 209 210 | |
natural_pdf.PDFCollection.get_indexable_items()
Yields Page objects from the collection, conforming to Indexable.
Source code in natural_pdf/core/pdf_collection.py
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | |
natural_pdf.PDFCollection.show(limit=30, per_pdf_limit=10, **kwargs)
Display all PDFs in the collection with labels.
Each PDF is shown with its pages in a grid layout (6 columns by default), and all PDFs are stacked vertically with labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
Optional[int]
|
Maximum total pages to show across all PDFs (default: 30) |
30
|
per_pdf_limit
|
Optional[int]
|
Maximum pages to show per PDF (default: 10) |
10
|
**kwargs
|
Additional arguments passed to each PDF's show() method (e.g., columns, exclusions, resolution, etc.) |
{}
|
Returns:
| Type | Description |
|---|---|
|
Displayed image in Jupyter or None |
Source code in natural_pdf/core/pdf_collection.py
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 | |
natural_pdf.Page
Bases: TextMixin, ClassificationMixin, ExtractionMixin, ShapeDetectionMixin, CheckboxDetectionMixin, DescribeMixin, VisualSearchMixin, Visualizable
Enhanced Page wrapper built on top of pdfplumber.Page.
This class provides a fluent interface for working with PDF pages, with improved selection, navigation, extraction, and question-answering capabilities. It integrates multiple analysis capabilities through mixins and provides spatial navigation with CSS-like selectors.
The Page class serves as the primary interface for document analysis, offering: - Element selection and spatial navigation - OCR and layout analysis integration - Table detection and extraction - AI-powered classification and data extraction - Visual debugging with highlighting and cropping - Text style analysis and structure detection
Attributes:
| Name | Type | Description |
|---|---|---|
index |
int
|
Zero-based index of this page in the PDF. |
number |
int
|
One-based page number (index + 1). |
width |
float
|
Page width in points. |
height |
float
|
Page height in points. |
bbox |
float
|
Bounding box tuple (x0, top, x1, bottom) of the page. |
chars |
List[Any]
|
Collection of character elements on the page. |
words |
List[Any]
|
Collection of word elements on the page. |
lines |
List[Any]
|
Collection of line elements on the page. |
rects |
List[Any]
|
Collection of rectangle elements on the page. |
images |
List[Any]
|
Collection of image elements on the page. |
metadata |
Dict[str, Any]
|
Dictionary for storing analysis results and custom data. |
Example
Basic usage:
pdf = npdf.PDF("document.pdf")
page = pdf.pages[0]
# Find elements with CSS-like selectors
headers = page.find_all('text[size>12]:bold')
summaries = page.find('text:contains("Summary")')
# Spatial navigation
content_below = summaries.below(until='text[size>12]:bold')
# Table extraction
tables = page.extract_table()
Advanced usage:
# Apply OCR if needed
page.apply_ocr(engine='easyocr', resolution=300)
# Layout analysis
page.analyze_layout(engine='yolo')
# AI-powered extraction
data = page.extract_structured_data(MySchema)
# Visual debugging
page.find('text:contains("Important")').show()
Source code in natural_pdf/core/page.py
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 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 | |
Attributes
natural_pdf.Page.chars
property
Get all character elements on this page.
natural_pdf.Page.height
property
Get page height.
natural_pdf.Page.images
property
Get all embedded raster images on this page.
natural_pdf.Page.index
property
Get page index (0-based).
natural_pdf.Page.layout_analyzer
property
Get or create the layout analyzer for this page.
natural_pdf.Page.lines
property
Get all line elements on this page.
natural_pdf.Page.number
property
Get page number (1-based).
natural_pdf.Page.page_number
property
Get page number (1-based).
natural_pdf.Page.pdf
property
Provides public access to the parent PDF object.
natural_pdf.Page.rects
property
Get all rectangle elements on this page.
natural_pdf.Page.size
property
Get the size of the page in points.
natural_pdf.Page.skew_angle
property
Get the detected skew angle for this page (if calculated).
natural_pdf.Page.text_style_labels
property
Get a sorted list of unique text style labels found on the page.
Runs text style analysis with default options if it hasn't been run yet.
To use custom options, call analyze_text_styles(options=...) explicitly first.
Returns:
| Type | Description |
|---|---|
List[str]
|
A sorted list of unique style label strings. |
natural_pdf.Page.width
property
Get page width.
natural_pdf.Page.words
property
Get all word elements on this page.
Functions
natural_pdf.Page.__init__(page, parent, index, font_attrs=None, load_text=True)
Initialize a page wrapper.
Creates an enhanced Page object that wraps a pdfplumber page with additional functionality for spatial navigation, analysis, and AI-powered extraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
Page
|
The underlying pdfplumber page object that provides raw PDF data. |
required |
parent
|
PDF
|
Parent PDF object that contains this page and provides access to managers and global settings. |
required |
index
|
int
|
Zero-based index of this page in the PDF document. |
required |
font_attrs
|
List of font attributes to consider when grouping characters into words. Common attributes include ['fontname', 'size', 'flags']. If None, uses default character-to-word grouping rules. |
None
|
|
load_text
|
bool
|
If True, load and process text elements from the PDF's text layer. If False, skip text layer processing (useful for OCR-only workflows). |
True
|
Note
This constructor is typically called automatically when accessing pages through the PDF.pages collection. Direct instantiation is rarely needed.
Example
# Pages are usually accessed through the PDF object
pdf = npdf.PDF("document.pdf")
page = pdf.pages[0] # Page object created automatically
# Direct construction (advanced usage)
import pdfplumber
with pdfplumber.open("document.pdf") as plumber_pdf:
plumber_page = plumber_pdf.pages[0]
page = Page(plumber_page, pdf, 0, load_text=True)
Source code in natural_pdf/core/page.py
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 | |
natural_pdf.Page.__repr__()
String representation of the page.
Source code in natural_pdf/core/page.py
3034 3035 3036 | |
natural_pdf.Page.add_exclusion(exclusion_func_or_region, label=None, method='region')
Add an exclusion to the page. Text from these regions will be excluded from extraction. Ensures non-callable items are stored as Region objects if possible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exclusion_func_or_region
|
Union[Callable[[Page], Region], Region, List[Any], Tuple[Any, ...], Any]
|
Either a callable function returning a Region, a Region object, a list/tuple of regions or elements, or another object with a valid .bbox attribute. |
required |
label
|
Optional[str]
|
Optional label for this exclusion (e.g., 'header', 'footer'). |
None
|
method
|
str
|
Exclusion method - 'region' (exclude all elements in bounding box) or 'element' (exclude only the specific elements). Default: 'region'. |
'region'
|
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a non-callable, non-Region object without a valid bbox is provided. |
ValueError
|
If method is not 'region' or 'element'. |
Source code in natural_pdf/core/page.py
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 | |
natural_pdf.Page.add_highlight(bbox=None, color=None, label=None, use_color_cycling=False, element=None, annotate=None, existing='append')
Add a highlight to a bounding box or the entire page. Delegates to the central HighlightingService.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Optional[Tuple[float, float, float, float]]
|
Bounding box (x0, top, x1, bottom). If None, highlight entire page. |
None
|
color
|
Optional[Union[Tuple, str]]
|
RGBA color tuple/string for the highlight. |
None
|
label
|
Optional[str]
|
Optional label for the highlight. |
None
|
use_color_cycling
|
bool
|
If True and no label/color, use next cycle color. |
False
|
element
|
Optional[Any]
|
Optional original element being highlighted (for attribute extraction). |
None
|
annotate
|
Optional[List[str]]
|
List of attribute names from 'element' to display. |
None
|
existing
|
str
|
How to handle existing highlights ('append' or 'replace'). |
'append'
|
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining. |
Source code in natural_pdf/core/page.py
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 | |
natural_pdf.Page.add_highlight_polygon(polygon, color=None, label=None, use_color_cycling=False, element=None, annotate=None, existing='append')
Highlight a polygon shape on the page. Delegates to the central HighlightingService.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
polygon
|
List[Tuple[float, float]]
|
List of (x, y) points defining the polygon. |
required |
color
|
Optional[Union[Tuple, str]]
|
RGBA color tuple/string for the highlight. |
None
|
label
|
Optional[str]
|
Optional label for the highlight. |
None
|
use_color_cycling
|
bool
|
If True and no label/color, use next cycle color. |
False
|
element
|
Optional[Any]
|
Optional original element being highlighted (for attribute extraction). |
None
|
annotate
|
Optional[List[str]]
|
List of attribute names from 'element' to display. |
None
|
existing
|
str
|
How to handle existing highlights ('append' or 'replace'). |
'append'
|
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining. |
Source code in natural_pdf/core/page.py
2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 | |
natural_pdf.Page.add_region(region, name=None)
Add a region to the page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Region
|
Region object to add |
required |
name
|
Optional[str]
|
Optional name for the region |
None
|
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining |
Source code in natural_pdf/core/page.py
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 | |
natural_pdf.Page.add_regions(regions, prefix=None)
Add multiple regions to the page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regions
|
List[Region]
|
List of Region objects to add |
required |
prefix
|
Optional[str]
|
Optional prefix for automatic naming (regions will be named prefix_1, prefix_2, etc.) |
None
|
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining |
Source code in natural_pdf/core/page.py
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 | |
natural_pdf.Page.analyze_layout(engine=None, options=None, confidence=None, classes=None, exclude_classes=None, device=None, existing='replace', model_name=None, client=None)
Analyze the page layout using the configured LayoutManager. Adds detected Region objects to the page's element manager.
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
ElementCollection containing the detected Region objects. |
Source code in natural_pdf/core/page.py
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 | |
natural_pdf.Page.analyze_text_styles(options=None)
Analyze text elements by style, adding attributes directly to elements.
This method uses TextStyleAnalyzer to process text elements (typically words) on the page. It adds the following attributes to each processed element: - style_label: A descriptive or numeric label for the style group. - style_key: A hashable tuple representing the style properties used for grouping. - style_properties: A dictionary containing the extracted style properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
options
|
Optional[TextStyleOptions]
|
Optional TextStyleOptions to configure the analysis. If None, the analyzer's default options are used. |
None
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection containing all processed text elements with added style attributes. |
Source code in natural_pdf/core/page.py
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 | |
natural_pdf.Page.apply_ocr(engine=None, options=None, languages=None, min_confidence=None, device=None, resolution=None, detect_only=False, apply_exclusions=True, replace=True)
Apply OCR to THIS page and add results to page elements via PDF.apply_ocr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Optional[str]
|
Name of the OCR engine. |
None
|
options
|
Optional[OCROptions]
|
Engine-specific options object or dict. |
None
|
languages
|
Optional[List[str]]
|
List of engine-specific language codes. |
None
|
min_confidence
|
Optional[float]
|
Minimum confidence threshold. |
None
|
device
|
Optional[str]
|
Device to run OCR on. |
None
|
resolution
|
Optional[int]
|
DPI resolution for rendering page image before OCR. |
None
|
apply_exclusions
|
bool
|
If True (default), render page image for OCR with excluded areas masked (whited out). |
True
|
detect_only
|
bool
|
If True, only detect text bounding boxes, don't perform OCR. |
False
|
replace
|
bool
|
If True (default), remove any existing OCR elements before adding new ones. If False, add new OCR elements to existing ones. |
True
|
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining. |
Source code in natural_pdf/core/page.py
2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 | |
natural_pdf.Page.ask(question, min_confidence=0.1, model=None, debug=False, **kwargs)
Ask a question about the page content using document QA.
Source code in natural_pdf/core/page.py
3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 | |
natural_pdf.Page.clear_detected_layout_regions()
Removes all regions from this page that were added by layout analysis
(i.e., regions where source attribute is 'detected').
This clears the regions both from the page's internal _regions['detected'] list
and from the ElementManager's internal list of regions.
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining. |
Source code in natural_pdf/core/page.py
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 | |
natural_pdf.Page.clear_exclusions()
Clear all exclusions from the page.
Source code in natural_pdf/core/page.py
414 415 416 417 418 419 | |
natural_pdf.Page.clear_highlights()
Clear all highlights from this specific page via HighlightingService.
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining |
Source code in natural_pdf/core/page.py
2426 2427 2428 2429 2430 2431 2432 2433 2434 | |
natural_pdf.Page.create_region(x0, top, x1, bottom)
Create a region on this page with the specified coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x0
|
float
|
Left x-coordinate |
required |
top
|
float
|
Top y-coordinate |
required |
x1
|
float
|
Right x-coordinate |
required |
bottom
|
float
|
Bottom y-coordinate |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Region object for the specified coordinates |
Source code in natural_pdf/core/page.py
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 | |
natural_pdf.Page.crop(bbox=None, **kwargs)
Crop the page to the specified bounding box.
This is a direct wrapper around pdfplumber's crop method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox
|
Bounding box (x0, top, x1, bottom) or None |
None
|
|
**kwargs
|
Additional parameters (top, bottom, left, right) |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Cropped page object (pdfplumber.Page) |
Source code in natural_pdf/core/page.py
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 | |
natural_pdf.Page.deskew(resolution=300, angle=None, detection_resolution=72, **deskew_kwargs)
Creates and returns a deskewed PIL image of the page.
If angle is not provided, it will first try to detect the skew angle
using detect_skew_angle (or use the cached angle if available).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution
|
int
|
DPI resolution for the output deskewed image. |
300
|
angle
|
Optional[float]
|
The specific angle (in degrees) to rotate by. If None, detects automatically. |
None
|
detection_resolution
|
int
|
DPI resolution used for detection if |
72
|
**deskew_kwargs
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Image]
|
A deskewed PIL.Image.Image object, or None if rendering/rotation fails. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If the 'deskew' library is not installed. |
Source code in natural_pdf/core/page.py
3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 | |
natural_pdf.Page.detect_skew_angle(resolution=72, grayscale=True, force_recalculate=False, **deskew_kwargs)
Detects the skew angle of the page image and stores it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution
|
int
|
DPI resolution for rendering the page image for detection. |
72
|
grayscale
|
bool
|
Whether to convert the image to grayscale before detection. |
True
|
force_recalculate
|
bool
|
If True, recalculate even if an angle exists. |
False
|
**deskew_kwargs
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[float]
|
The detected skew angle in degrees, or None if detection failed. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If the 'deskew' library is not installed. |
Source code in natural_pdf/core/page.py
3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 | |
natural_pdf.Page.extract_ocr_elements(engine=None, options=None, languages=None, min_confidence=None, device=None, resolution=None)
Extract text elements using OCR without adding them to the page's elements. Uses the shared OCRManager instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Optional[str]
|
Name of the OCR engine. |
None
|
options
|
Optional[OCROptions]
|
Engine-specific options object or dict. |
None
|
languages
|
Optional[List[str]]
|
List of engine-specific language codes. |
None
|
min_confidence
|
Optional[float]
|
Minimum confidence threshold. |
None
|
device
|
Optional[str]
|
Device to run OCR on. |
None
|
resolution
|
Optional[int]
|
DPI resolution for rendering page image before OCR. |
None
|
Returns:
| Type | Description |
|---|---|
List[TextElement]
|
List of created TextElement objects derived from OCR results for this page. |
Source code in natural_pdf/core/page.py
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 | |
natural_pdf.Page.extract_table(method=None, table_settings=None, use_ocr=False, ocr_config=None, text_options=None, cell_extraction_func=None, show_progress=False, content_filter=None, verticals=None, horizontals=None)
Extract the largest table from this page using enhanced region-based extraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Optional[str]
|
Method to use: 'tatr', 'pdfplumber', 'text', 'stream', 'lattice', or None (auto-detect). |
None
|
table_settings
|
Optional[dict]
|
Settings for pdfplumber table extraction. |
None
|
use_ocr
|
bool
|
Whether to use OCR for text extraction (currently only applicable with 'tatr' method). |
False
|
ocr_config
|
Optional[dict]
|
OCR configuration parameters. |
None
|
text_options
|
Optional[Dict]
|
Dictionary of options for the 'text' method. |
None
|
cell_extraction_func
|
Optional[Callable[[Region], Optional[str]]]
|
Optional callable function that takes a cell Region object and returns its string content. For 'text' method only. |
None
|
show_progress
|
bool
|
If True, display a progress bar during cell text extraction for the 'text' method. |
False
|
content_filter
|
Optional content filter to apply during cell text extraction. Can be: - A regex pattern string (characters matching the pattern are EXCLUDED) - A callable that takes text and returns True to KEEP the character - A list of regex patterns (characters matching ANY pattern are EXCLUDED) |
None
|
|
verticals
|
Optional[List[float]]
|
Optional list of x-coordinates for explicit vertical table lines. |
None
|
horizontals
|
Optional[List[float]]
|
Optional list of y-coordinates for explicit horizontal table lines. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
TableResult |
TableResult
|
A sequence-like object containing table rows that also provides .to_df() for pandas conversion. |
Source code in natural_pdf/core/page.py
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 | |
natural_pdf.Page.extract_tables(method=None, table_settings=None, check_tatr=True)
Extract all tables from this page with enhanced method support.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Optional[str]
|
Method to use: 'pdfplumber', 'stream', 'lattice', or None (auto-detect). 'stream' uses text-based strategies, 'lattice' uses line-based strategies. Note: 'tatr' and 'text' methods are not supported for extract_tables. |
None
|
table_settings
|
Optional[dict]
|
Settings for pdfplumber table extraction. |
None
|
check_tatr
|
bool
|
If True (default), first check for TATR-detected table regions and extract from those before falling back to pdfplumber methods. |
True
|
Returns:
| Type | Description |
|---|---|
List[List[List[str]]]
|
List of tables, where each table is a list of rows, and each row is a list of cell values. |
Source code in natural_pdf/core/page.py
2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 | |
natural_pdf.Page.extract_text(preserve_whitespace=True, use_exclusions=True, debug_exclusions=False, content_filter=None, **kwargs)
Extract text from this page, respecting exclusions and using pdfplumber's layout engine (chars_to_textmap) if layout arguments are provided or default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
use_exclusions
|
Whether to apply exclusion regions (default: True). Note: Filtering logic is now always applied if exclusions exist. |
True
|
|
debug_exclusions
|
Whether to output detailed exclusion debugging info (default: False). |
False
|
|
content_filter
|
Optional content filter to exclude specific text patterns. Can be: - A regex pattern string (characters matching the pattern are EXCLUDED) - A callable that takes text and returns True to KEEP the character - A list of regex patterns (characters matching ANY pattern are EXCLUDED) |
None
|
|
**kwargs
|
Additional layout parameters passed directly to pdfplumber's
|
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Extracted text as string, potentially with layout-based spacing. |
Source code in natural_pdf/core/page.py
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 | |
natural_pdf.Page.filter_elements(elements, selector, **kwargs)
Filter a list of elements based on a selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elements
|
List[Element]
|
List of elements to filter |
required |
selector
|
str
|
CSS-like selector string |
required |
**kwargs
|
Additional filter parameters |
{}
|
Returns:
| Type | Description |
|---|---|
List[Element]
|
List of elements that match the selector |
Source code in natural_pdf/core/page.py
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 | |
natural_pdf.Page.find(selector=None, *, text=None, apply_exclusions=True, regex=False, case=True, **kwargs)
find(*, text: str, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> Optional[Any]
find(selector: str, *, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> Optional[Any]
Find first element on this page matching selector OR text content.
Provide EITHER selector OR text, but not both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string. |
None
|
text
|
Optional[str]
|
Text content to search for (equivalent to 'text:contains(...)'). |
None
|
apply_exclusions
|
bool
|
Whether to exclude elements in exclusion regions (default: True). |
True
|
regex
|
bool
|
Whether to use regex for text search ( |
False
|
case
|
bool
|
Whether to do case-sensitive text search ( |
True
|
**kwargs
|
Additional filter parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Any]
|
Element object or None if not found. |
Source code in natural_pdf/core/page.py
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 | |
natural_pdf.Page.find_all(selector=None, *, text=None, apply_exclusions=True, regex=False, case=True, **kwargs)
find_all(*, text: str, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
find_all(selector: str, *, apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
Find all elements on this page matching selector OR text content.
Provide EITHER selector OR text, but not both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string. |
None
|
text
|
Optional[str]
|
Text content to search for (equivalent to 'text:contains(...)'). |
None
|
apply_exclusions
|
bool
|
Whether to exclude elements in exclusion regions (default: True). |
True
|
regex
|
bool
|
Whether to use regex for text search ( |
False
|
case
|
bool
|
Whether to do case-sensitive text search ( |
True
|
**kwargs
|
Additional filter parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection with matching elements. |
Source code in natural_pdf/core/page.py
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 | |
natural_pdf.Page.get_content()
Returns the primary content object (self) for indexing (required by Indexable protocol). SearchService implementations decide how to process this (e.g., call extract_text).
Source code in natural_pdf/core/page.py
3213 3214 3215 3216 3217 3218 | |
natural_pdf.Page.get_content_hash()
Returns a SHA256 hash of the extracted text content (required by Indexable for sync).
Source code in natural_pdf/core/page.py
3220 3221 3222 3223 3224 3225 3226 3227 3228 | |
natural_pdf.Page.get_elements(apply_exclusions=True, debug_exclusions=False)
Get all elements on this page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
apply_exclusions
|
Whether to apply exclusion regions (default: True). |
True
|
|
debug_exclusions
|
bool
|
Whether to output detailed exclusion debugging info (default: False). |
False
|
Returns:
| Type | Description |
|---|---|
List[Element]
|
List of all elements on the page, potentially filtered by exclusions. |
Source code in natural_pdf/core/page.py
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 | |
natural_pdf.Page.get_id()
Returns a unique identifier for the page (required by Indexable protocol).
Source code in natural_pdf/core/page.py
3195 3196 3197 3198 3199 | |
natural_pdf.Page.get_metadata()
Returns metadata associated with the page (required by Indexable protocol).
Source code in natural_pdf/core/page.py
3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 | |
natural_pdf.Page.get_section_between(start_element=None, end_element=None, include_boundaries='both', orientation='vertical')
Get a section between two elements on this page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_element
|
Element marking the start of the section |
None
|
|
end_element
|
Element marking the end of the section |
None
|
|
include_boundaries
|
How to include boundary elements: 'start', 'end', 'both', or 'none' |
'both'
|
|
orientation
|
'vertical' (default) or 'horizontal' - determines section direction |
'vertical'
|
Returns:
| Type | Description |
|---|---|
Optional[Region]
|
Region representing the section |
Source code in natural_pdf/core/page.py
2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 | |
natural_pdf.Page.get_sections(start_elements=None, end_elements=None, include_boundaries='start', y_threshold=5.0, bounding_box=None, orientation='vertical')
Get sections of a page defined by start/end elements. Uses the page-level implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_elements
|
Elements or selector string that mark the start of sections |
None
|
|
end_elements
|
Elements or selector string that mark the end of sections |
None
|
|
include_boundaries
|
How to include boundary elements: 'start', 'end', 'both', or 'none' |
'start'
|
|
y_threshold
|
Threshold for vertical alignment (only used for vertical orientation) |
5.0
|
|
bounding_box
|
Optional bounding box to constrain sections |
None
|
|
orientation
|
'vertical' (default) or 'horizontal' - determines section direction |
'vertical'
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
An ElementCollection containing the found Region objects. |
Source code in natural_pdf/core/page.py
2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 | |
natural_pdf.Page.highlights(show=False)
Create a highlight context for accumulating highlights.
This allows for clean syntax to show multiple highlight groups:
Example
with page.highlights() as h: h.add(page.find_all('table'), label='tables', color='blue') h.add(page.find_all('text:bold'), label='bold text', color='red') h.show()
Or with automatic display
with page.highlights(show=True) as h: h.add(page.find_all('table'), label='tables') h.add(page.find_all('text:bold'), label='bold') # Automatically shows when exiting the context
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show
|
bool
|
If True, automatically show highlights when exiting context |
False
|
Returns:
| Type | Description |
|---|---|
HighlightContext
|
HighlightContext for accumulating highlights |
Source code in natural_pdf/core/page.py
3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 | |
natural_pdf.Page.inspect(limit=30)
Inspect all elements on this page with detailed tabular view. Equivalent to page.find_all('*').inspect().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
int
|
Maximum elements per type to show (default: 30) |
30
|
Returns:
| Type | Description |
|---|---|
InspectionSummary
|
InspectionSummary with element tables showing coordinates, |
InspectionSummary
|
properties, and other details for each element |
Source code in natural_pdf/core/page.py
3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 | |
natural_pdf.Page.region(left=None, top=None, right=None, bottom=None, width=None, height=None)
Create a region on this page with more intuitive named parameters, allowing definition by coordinates or by coordinate + dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
float
|
Left x-coordinate (default: 0 if width not used). |
None
|
top
|
float
|
Top y-coordinate (default: 0 if height not used). |
None
|
right
|
float
|
Right x-coordinate (default: page width if width not used). |
None
|
bottom
|
float
|
Bottom y-coordinate (default: page height if height not used). |
None
|
width
|
Union[str, float, None]
|
Width definition. Can be: - Numeric: The width of the region in points. Cannot be used with both left and right. - String 'full': Sets region width to full page width (overrides left/right). - String 'element' or None (default): Uses provided/calculated left/right, defaulting to page width if neither are specified. |
None
|
height
|
Optional[float]
|
Numeric height of the region. Cannot be used with both top and bottom. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Region object for the specified coordinates |
Raises:
| Type | Description |
|---|---|
ValueError
|
If conflicting arguments are provided (e.g., top, bottom, and height) or if width is an invalid string. |
Examples:
>>> page.region(top=100, height=50) # Region from y=100 to y=150, default width
>>> page.region(left=50, width=100) # Region from x=50 to x=150, default height
>>> page.region(bottom=500, height=50) # Region from y=450 to y=500
>>> page.region(right=200, width=50) # Region from x=150 to x=200
>>> page.region(top=100, bottom=200, width="full") # Explicit full width
Source code in natural_pdf/core/page.py
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 | |
natural_pdf.Page.remove_text_layer()
Remove all text elements from this page.
This removes all text elements (words and characters) from the page, effectively clearing the text layer.
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining |
Source code in natural_pdf/core/page.py
3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 | |
natural_pdf.Page.save_image(filename, width=None, labels=True, legend_position='right', render_ocr=False, include_highlights=True, resolution=144, **kwargs)
Save the page image to a file, rendering highlights via HighlightingService.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to save the image to. |
required |
width
|
Optional[int]
|
Optional width for the output image. |
None
|
labels
|
bool
|
Whether to include a legend. |
True
|
legend_position
|
str
|
Position of the legend. |
'right'
|
render_ocr
|
bool
|
Whether to render OCR text. |
False
|
include_highlights
|
bool
|
Whether to render highlights. |
True
|
resolution
|
float
|
Resolution in DPI for base image rendering (default: 144 DPI, equivalent to previous scale=2.0). |
144
|
**kwargs
|
Additional args for pdfplumber's internal to_image. |
{}
|
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining. |
Source code in natural_pdf/core/page.py
2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 | |
natural_pdf.Page.save_searchable(output_path, dpi=300, **kwargs)
Saves the PDF page with an OCR text layer, making content searchable.
Requires optional dependencies. Install with: pip install "natural-pdf[ocr-save]"
OCR must have been applied to the pages beforehand
(e.g., pdf.apply_ocr()).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
Union[str, Path]
|
Path to save the searchable PDF. |
required |
dpi
|
int
|
Resolution for rendering and OCR overlay (default 300). |
300
|
**kwargs
|
Additional keyword arguments passed to the exporter. |
{}
|
Source code in natural_pdf/core/page.py
3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 | |
natural_pdf.Page.show_preview(temporary_highlights, resolution=144, width=None, labels=True, legend_position='right', render_ocr=False)
Generates and returns a non-stateful preview image containing only the provided temporary highlights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temporary_highlights
|
List[Dict]
|
List of highlight data dictionaries (as prepared by ElementCollection._prepare_highlight_data). |
required |
resolution
|
float
|
Resolution in DPI for rendering (default: 144 DPI, equivalent to previous scale=2.0). |
144
|
width
|
Optional[int]
|
Optional width for the output image. |
None
|
labels
|
bool
|
Whether to include a legend. |
True
|
legend_position
|
str
|
Position of the legend. |
'right'
|
render_ocr
|
bool
|
Whether to render OCR text. |
False
|
Returns:
| Type | Description |
|---|---|
Optional[Image]
|
PIL Image object of the preview, or None if rendering fails. |
Source code in natural_pdf/core/page.py
3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 | |
natural_pdf.Page.split(divider, **kwargs)
Divides the page into sections based on the provided divider elements.
Source code in natural_pdf/core/page.py
2796 2797 2798 2799 2800 2801 2802 2803 2804 | |
natural_pdf.Page.until(selector, include_endpoint=True, **kwargs)
Select content from the top of the page until matching selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
str
|
CSS-like selector string |
required |
include_endpoint
|
bool
|
Whether to include the endpoint element in the region |
True
|
**kwargs
|
Additional selection parameters |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Region object representing the selected content |
Examples:
>>> page.until('text:contains("Conclusion")') # Select from top to conclusion
>>> page.until('line[width>=2]', include_endpoint=False) # Select up to thick line
Source code in natural_pdf/core/page.py
1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 | |
natural_pdf.Page.update_text(transform, selector='text', max_workers=None, progress_callback=None)
Applies corrections to text elements on this page using a user-provided callback function, potentially in parallel.
Finds text elements on this page matching the selector argument and
calls the transform for each, passing the element itself.
Updates the element's text if the callback returns a new string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable[[Any], Optional[str]]
|
A function accepting an element and returning
|
required |
selector
|
str
|
CSS-like selector string to match text elements. |
'text'
|
max_workers
|
Optional[int]
|
The maximum number of threads to use for parallel execution. If None or 0 or 1, runs sequentially. |
None
|
progress_callback
|
Optional[Callable[[], None]]
|
Optional callback function to call after processing each element. |
None
|
Returns:
| Type | Description |
|---|---|
Page
|
Self for method chaining. |
Source code in natural_pdf/core/page.py
3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 | |
natural_pdf.Page.viewer()
Creates and returns an interactive ipywidget for exploring elements on this page.
Uses InteractiveViewerWidget.from_page() to create the viewer.
Returns:
| Type | Description |
|---|---|
Optional[InteractiveViewerWidget]
|
A InteractiveViewerWidget instance ready for display in Jupyter, |
Optional[InteractiveViewerWidget]
|
or None if ipywidgets is not installed or widget creation fails. |
Raises:
| Type | Description |
|---|---|
# Optional
|
Could raise ImportError instead of returning None |
# ImportError
|
If required dependencies (ipywidgets) are missing. |
ValueError
|
If image rendering or data preparation fails within from_page. |
Source code in natural_pdf/core/page.py
3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 | |
natural_pdf.Page.without_exclusions()
Context manager that temporarily disables exclusion processing.
This prevents infinite recursion when exclusion callables themselves use find() operations. While in this context, all find operations will skip exclusion filtering.
Example
# This exclusion would normally cause infinite recursion:
page.add_exclusion(lambda p: p.find("text:contains('Header')").expand())
# But internally, it's safe because we use:
with page.without_exclusions():
region = exclusion_callable(page)
Yields:
| Type | Description |
|---|---|
|
The page object with exclusions temporarily disabled. |
Source code in natural_pdf/core/page.py
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 | |
natural_pdf.PageCollection
Bases: TextMixin, Generic[P], ApplyMixin, ShapeDetectionMixin, CheckboxDetectionMixin, Visualizable
Represents a collection of Page objects, often from a single PDF document. Provides methods for batch operations on these pages.
Source code in natural_pdf/core/page_collection.py
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 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 | |
Functions
natural_pdf.PageCollection.__getitem__(idx)
Support indexing and slicing.
Source code in natural_pdf/core/page_collection.py
107 108 109 110 111 | |
natural_pdf.PageCollection.__init__(pages)
Initialize a page collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pages
|
Union[List[P], Sequence[P]]
|
List or sequence of Page objects (can be lazy) |
required |
Source code in natural_pdf/core/page_collection.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
natural_pdf.PageCollection.__iter__()
Support iteration.
Source code in natural_pdf/core/page_collection.py
113 114 115 | |
natural_pdf.PageCollection.__len__()
Return the number of pages in the collection.
Source code in natural_pdf/core/page_collection.py
103 104 105 | |
natural_pdf.PageCollection.__repr__()
Return a string representation showing the page count.
Source code in natural_pdf/core/page_collection.py
117 118 119 | |
natural_pdf.PageCollection.analyze_layout(*args, **kwargs)
Analyzes the layout of each page in the collection.
This method iterates through each page, calls its analyze_layout method, and returns a single ElementCollection containing all the detected layout regions from all pages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments to pass to each page's analyze_layout method. |
()
|
|
**kwargs
|
Keyword arguments to pass to each page's analyze_layout method. A 'show_progress' kwarg can be included to show a progress bar. |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
An ElementCollection of all detected Region objects. |
Source code in natural_pdf/core/page_collection.py
1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 | |
natural_pdf.PageCollection.apply_ocr(engine=None, languages=None, min_confidence=None, device=None, resolution=None, apply_exclusions=True, replace=True, options=None)
Applies OCR to all pages within this collection using batch processing.
This delegates the work to the parent PDF object's apply_ocr method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Optional[str]
|
Name of the OCR engine (e.g., 'easyocr', 'paddleocr'). |
None
|
languages
|
Optional[List[str]]
|
List of language codes (e.g., ['en', 'fr'], ['en', 'ch']). Must be codes understood by the specific selected engine. No mapping is performed. |
None
|
min_confidence
|
Optional[float]
|
Minimum confidence threshold for detected text (0.0 to 1.0). |
None
|
device
|
Optional[str]
|
Device to run OCR on (e.g., 'cpu', 'cuda', 'mps'). |
None
|
resolution
|
Optional[int]
|
DPI resolution to render page images before OCR (e.g., 150, 300). |
None
|
apply_exclusions
|
bool
|
If True (default), render page images for OCR with excluded areas masked (whited out). If False, OCR the raw page images without masking exclusions. |
True
|
replace
|
bool
|
If True (default), remove any existing OCR elements before adding new ones. If False, add new OCR elements to existing ones. |
True
|
options
|
Optional[Any]
|
An engine-specific options object (e.g., EasyOCROptions) or dict. |
None
|
Returns:
| Type | Description |
|---|---|
PageCollection[P]
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If pages lack a parent PDF or parent lacks |
Source code in natural_pdf/core/page_collection.py
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 | |
natural_pdf.PageCollection.deskew(resolution=300, detection_resolution=72, force_overwrite=False, **deskew_kwargs)
Creates a new, in-memory PDF object containing deskewed versions of the pages in this collection.
This method delegates the actual processing to the parent PDF object's
deskew method.
Important: The returned PDF is image-based. Any existing text, OCR results, annotations, or other elements from the original pages will not be carried over.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution
|
int
|
DPI resolution for rendering the output deskewed pages. |
300
|
detection_resolution
|
int
|
DPI resolution used for skew detection if angles are not already cached on the page objects. |
72
|
force_overwrite
|
bool
|
If False (default), raises a ValueError if any target page already contains processed elements (text, OCR, regions) to prevent accidental data loss. Set to True to proceed anyway. |
False
|
**deskew_kwargs
|
Additional keyword arguments passed to |
{}
|
Returns:
| Type | Description |
|---|---|
PDF
|
A new PDF object representing the deskewed document. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If 'deskew' or 'img2pdf' libraries are not installed (raised by PDF.deskew). |
ValueError
|
If |
RuntimeError
|
If pages lack a parent PDF reference, or the parent PDF lacks the |
Source code in natural_pdf/core/page_collection.py
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 | |
natural_pdf.PageCollection.detect_checkboxes(*args, **kwargs)
Detects checkboxes on each page in the collection.
This method iterates through each page, calls its detect_checkboxes method, and returns a single ElementCollection containing all detected checkbox regions from all pages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Positional arguments to pass to each page's detect_checkboxes method. |
()
|
|
**kwargs
|
Keyword arguments to pass to each page's detect_checkboxes method. A 'show_progress' kwarg can be included to show a progress bar. |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
An ElementCollection of all detected checkbox Region objects. |
Source code in natural_pdf/core/page_collection.py
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 | |
natural_pdf.PageCollection.extract_text(keep_blank_chars=True, apply_exclusions=True, strip=None, **kwargs)
Extract text from all pages in the collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keep_blank_chars
|
bool
|
Whether to keep blank characters (default: True) |
True
|
apply_exclusions
|
bool
|
Whether to apply exclusion regions (default: True) |
True
|
strip
|
Optional[bool]
|
Whether to strip whitespace from the extracted text. |
None
|
**kwargs
|
Additional extraction parameters |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Combined text from all pages |
Source code in natural_pdf/core/page_collection.py
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 | |
natural_pdf.PageCollection.find(selector=None, *, text=None, overlap='full', apply_exclusions=True, regex=False, case=True, **kwargs)
find(*, text: str, overlap: str = 'full', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> Optional[T]
find(selector: str, *, overlap: str = 'full', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> Optional[T]
Find the first element matching the selector OR text across all pages in the collection.
Provide EITHER selector OR text, but not both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string. |
None
|
text
|
Optional[str]
|
Text content to search for (equivalent to 'text:contains(...)'). |
None
|
overlap
|
str
|
How to determine if elements overlap: 'full' (fully inside), 'partial' (any overlap), or 'center' (center point inside). (default: "full") |
'full'
|
apply_exclusions
|
bool
|
Whether to exclude elements in exclusion regions (default: True). |
True
|
regex
|
bool
|
Whether to use regex for text search ( |
False
|
case
|
bool
|
Whether to do case-sensitive text search ( |
True
|
**kwargs
|
Additional filter parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[T]
|
First matching element or None. |
Source code in natural_pdf/core/page_collection.py
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 | |
natural_pdf.PageCollection.find_all(selector=None, *, text=None, overlap='full', apply_exclusions=True, regex=False, case=True, **kwargs)
find_all(*, text: str, overlap: str = 'full', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
find_all(selector: str, *, overlap: str = 'full', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
Find all elements matching the selector OR text across all pages in the collection.
Provide EITHER selector OR text, but not both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string. |
None
|
text
|
Optional[str]
|
Text content to search for (equivalent to 'text:contains(...)'). |
None
|
overlap
|
str
|
How to determine if elements overlap: 'full' (fully inside), 'partial' (any overlap), or 'center' (center point inside). (default: "full") |
'full'
|
apply_exclusions
|
bool
|
Whether to exclude elements in exclusion regions (default: True). |
True
|
regex
|
bool
|
Whether to use regex for text search ( |
False
|
case
|
bool
|
Whether to do case-sensitive text search ( |
True
|
**kwargs
|
Additional filter parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection with matching elements from all pages. |
Source code in natural_pdf/core/page_collection.py
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 | |
natural_pdf.PageCollection.get_sections(start_elements=None, end_elements=None, new_section_on_page_break=False, include_boundaries='both', orientation='vertical')
Extract sections from a page collection based on start/end elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_elements
|
Elements or selector string that mark the start of sections (optional) |
None
|
|
end_elements
|
Elements or selector string that mark the end of sections (optional) |
None
|
|
new_section_on_page_break
|
Whether to start a new section at page boundaries (default: False) |
False
|
|
include_boundaries
|
How to include boundary elements: 'start', 'end', 'both', or 'none' (default: 'both') |
'both'
|
|
orientation
|
'vertical' (default) or 'horizontal' - determines section direction |
'vertical'
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
List of Region objects representing the extracted sections |
Note
You can provide only start_elements, only end_elements, or both. - With only start_elements: sections go from each start to the next start (or end of page) - With only end_elements: sections go from beginning of document/page to each end - With both: sections go from each start to the corresponding end
Source code in natural_pdf/core/page_collection.py
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 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 | |
natural_pdf.PageCollection.groupby(by, *, show_progress=True)
Group pages by selector text or callable result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
by
|
Union[str, Callable]
|
CSS selector string or callable function |
required |
show_progress
|
bool
|
Whether to show progress bar during computation (default: True) |
True
|
Returns:
| Type | Description |
|---|---|
PageGroupBy
|
PageGroupBy object supporting iteration and dict-like access |
Examples:
Group by header text
for title, pages in pdf.pages.groupby('text[size=16]'): print(f"Section: {title}")
Group by callable
for city, pages in pdf.pages.groupby(lambda p: p.find('text:contains("CITY")').extract_text()): process_city_pages(pages)
Quick exploration with indexing
grouped = pdf.pages.groupby('text[size=16]') grouped.info() # Show all groups first_section = grouped[0] # First group last_section = grouped[-1] # Last group
Dict-like access by name
madison_pages = grouped.get('CITY OF MADISON') madison_pages = grouped['CITY OF MADISON'] # Alternative
Disable progress bar for small collections
grouped = pdf.pages.groupby('text[size=16]', show_progress=False)
Source code in natural_pdf/core/page_collection.py
1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 | |
natural_pdf.PageCollection.highlights(show=False)
Create a highlight context for accumulating highlights.
This allows for clean syntax to show multiple highlight groups:
Example
with pages.highlights() as h: h.add(pages.find_all('table'), label='tables', color='blue') h.add(pages.find_all('text:bold'), label='bold text', color='red') h.show()
Or with automatic display
with pages.highlights(show=True) as h: h.add(pages.find_all('table'), label='tables') h.add(pages.find_all('text:bold'), label='bold') # Automatically shows when exiting the context
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
show
|
bool
|
If True, automatically show highlights when exiting context |
False
|
Returns:
| Type | Description |
|---|---|
HighlightContext
|
HighlightContext for accumulating highlights |
Source code in natural_pdf/core/page_collection.py
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 | |
natural_pdf.PageCollection.save_pdf(output_path, ocr=False, original=False, dpi=300)
Saves the pages in this collection to a new PDF file.
Choose one saving mode:
- ocr=True: Creates a new, image-based PDF using OCR results. This
makes the text generated during the natural-pdf session searchable,
but loses original vector content. Requires 'ocr-export' extras.
- original=True: Extracts the original pages from the source PDF,
preserving all vector content, fonts, and annotations. OCR results
from the natural-pdf session are NOT included. Requires 'ocr-export' extras.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_path
|
Union[str, Path]
|
Path to save the new PDF file. |
required |
ocr
|
bool
|
If True, save as a searchable, image-based PDF using OCR data. |
False
|
original
|
bool
|
If True, save the original, vector-based pages. |
False
|
dpi
|
int
|
Resolution (dots per inch) used only when ocr=True for rendering page images and aligning the text layer. |
300
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the collection is empty, if neither or both 'ocr' and 'original' are True, or if 'original=True' and pages originate from different PDFs. |
ImportError
|
If required libraries ('pikepdf', 'Pillow') are not installed for the chosen mode. |
RuntimeError
|
If an unexpected error occurs during saving. |
Source code in natural_pdf/core/page_collection.py
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 | |
natural_pdf.PageCollection.split(divider, **kwargs)
Divide this page collection into sections based on the provided divider elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
divider
|
Elements or selector string that mark section boundaries |
required | |
**kwargs
|
Additional parameters passed to get_sections() - include_boundaries: How to include boundary elements (default: 'start') - orientation: 'vertical' or 'horizontal' (default: 'vertical') - new_section_on_page_break: Whether to split at page boundaries (default: False) |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
ElementCollection of Region objects representing the sections |
Example
Split a PDF by chapter titles
chapters = pdf.pages.split("text[size>20]:contains('CHAPTER')")
Split by page breaks
page_sections = pdf.pages.split(None, new_section_on_page_break=True)
Split multi-page document by section headers
sections = pdf.pages[10:20].split("text:bold:contains('Section')")
Source code in natural_pdf/core/page_collection.py
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 | |
natural_pdf.PageCollection.to_flow(arrangement='vertical', alignment='start', segment_gap=0.0)
Convert this PageCollection to a Flow for cross-page operations.
This enables treating multiple pages as a continuous logical document structure, useful for multi-page tables, articles spanning columns, or any content requiring reading order across page boundaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arrangement
|
Literal['vertical', 'horizontal']
|
Primary flow direction ('vertical' or 'horizontal'). 'vertical' stacks pages top-to-bottom (most common). 'horizontal' arranges pages left-to-right. |
'vertical'
|
alignment
|
Literal['start', 'center', 'end', 'top', 'left', 'bottom', 'right']
|
Cross-axis alignment for pages of different sizes: For vertical: 'left'/'start', 'center', 'right'/'end' For horizontal: 'top'/'start', 'center', 'bottom'/'end' |
'start'
|
segment_gap
|
float
|
Virtual gap between pages in PDF points (default: 0.0). |
0.0
|
Returns:
| Type | Description |
|---|---|
Flow
|
Flow object that can perform operations across all pages in sequence. |
Example
Multi-page table extraction:
pdf = npdf.PDF("multi_page_report.pdf")
# Create flow for pages 2-4 containing a table
table_flow = pdf.pages[1:4].to_flow()
# Extract table as if it were continuous
table_data = table_flow.extract_table()
df = table_data.df
Cross-page element search:
# Find all headers across multiple pages
headers = pdf.pages[5:10].to_flow().find_all('text[size>12]:bold')
# Analyze layout across pages
regions = pdf.pages.to_flow().analyze_layout(engine='yolo')
Source code in natural_pdf/core/page_collection.py
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 | |
natural_pdf.PageCollection.update_text(transform, selector='text', max_workers=None)
Applies corrections to text elements across all pages
in this collection using a user-provided callback function, executed
in parallel if max_workers is specified.
This method delegates to the parent PDF's update_text method,
targeting all pages within this collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
Callable[[Any], Optional[str]]
|
A function that accepts a single argument (an element
object) and returns |
required |
selector
|
str
|
The attribute name to update. Default is 'text'. |
'text'
|
max_workers
|
Optional[int]
|
The maximum number of worker threads to use for parallel correction on each page. If None, defaults are used. |
None
|
Returns:
| Type | Description |
|---|---|
PageCollection[P]
|
Self for method chaining. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the collection is empty, pages lack a parent PDF reference,
or the parent PDF lacks the |
Source code in natural_pdf/core/page_collection.py
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 | |
natural_pdf.Region
Bases: TextMixin, DirectionalMixin, ClassificationMixin, ExtractionMixin, ShapeDetectionMixin, CheckboxDetectionMixin, DescribeMixin, VisualSearchMixin, Visualizable
Represents a rectangular region on a page.
Regions are fundamental building blocks in natural-pdf that define rectangular areas of a page for analysis, extraction, and navigation. They can be created manually or automatically through spatial navigation methods like .below(), .above(), .left(), and .right() from elements or other regions.
Regions integrate multiple analysis capabilities through mixins and provide: - Element filtering and collection within the region boundary - OCR processing for the region area - Table detection and extraction - AI-powered classification and structured data extraction - Visual rendering and debugging capabilities - Text extraction with spatial awareness
The Region class supports both rectangular and polygonal boundaries, making it suitable for complex document layouts and irregular shapes detected by layout analysis algorithms.
Attributes:
| Name | Type | Description |
|---|---|---|
page |
Page
|
Reference to the parent Page object. |
bbox |
Tuple[float, float, float, float]
|
Bounding box tuple (x0, top, x1, bottom) in PDF coordinates. |
x0 |
float
|
Left x-coordinate. |
top |
float
|
Top y-coordinate (minimum y). |
x1 |
float
|
Right x-coordinate. |
bottom |
float
|
Bottom y-coordinate (maximum y). |
width |
float
|
Region width (x1 - x0). |
height |
float
|
Region height (bottom - top). |
polygon |
List[Tuple[float, float]]
|
List of coordinate points for non-rectangular regions. |
label |
Optional descriptive label for the region. |
|
metadata |
Dict[str, Any]
|
Dictionary for storing analysis results and custom data. |
Example
Creating regions:
pdf = npdf.PDF("document.pdf")
page = pdf.pages[0]
# Manual region creation
header_region = page.region(0, 0, page.width, 100)
# Spatial navigation from elements
summary_text = page.find('text:contains("Summary")')
content_region = summary_text.below(until='text[size>12]:bold')
# Extract content from region
tables = content_region.extract_table()
text = content_region.get_text()
Advanced usage:
# OCR processing
region.apply_ocr(engine='easyocr', resolution=300)
# AI-powered extraction
data = region.extract_structured_data(MySchema)
# Visual debugging
region.show(highlights=['tables', 'text'])
Source code in natural_pdf/elements/region.py
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 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 | |
Attributes
natural_pdf.Region.bbox
property
Get the bounding box as (x0, top, x1, bottom).
natural_pdf.Region.bottom
property
Get the bottom coordinate.
natural_pdf.Region.endpoint
property
The element where this region stopped (if created with 'until' parameter).
natural_pdf.Region.has_polygon
property
Check if this region has polygon coordinates.
natural_pdf.Region.height
property
Get the height of the region.
natural_pdf.Region.origin
property
The element/region that created this region (if it was created via directional method).
natural_pdf.Region.page
property
Get the parent page.
natural_pdf.Region.polygon
property
Get polygon coordinates if available, otherwise return rectangle corners.
natural_pdf.Region.top
property
Get the top coordinate.
natural_pdf.Region.type
property
Element type.
natural_pdf.Region.width
property
Get the width of the region.
natural_pdf.Region.x0
property
Get the left coordinate.
natural_pdf.Region.x1
property
Get the right coordinate.
Functions
natural_pdf.Region.__add__(other)
Add regions/elements together to create an ElementCollection.
This allows intuitive combination of regions using the + operator:
complainant = section.find("text:contains(Complainant)").right(until='text')
dob = section.find("text:contains(DOB)").right(until='text')
combined = complainant + dob # Creates ElementCollection with both regions
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Union[Element, Region, ElementCollection]
|
Another Region, Element or ElementCollection to combine |
required |
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection containing all elements |
Source code in natural_pdf/elements/region.py
3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 | |
natural_pdf.Region.__init__(page, bbox, polygon=None, parent=None, label=None)
Initialize a region.
Creates a Region object that represents a rectangular or polygonal area on a page. Regions are used for spatial navigation, content extraction, and analysis operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page
|
Page
|
Parent Page object that contains this region and provides access to document elements and analysis capabilities. |
required |
bbox
|
Tuple[float, float, float, float]
|
Bounding box coordinates as (x0, top, x1, bottom) tuple in PDF coordinate system (points, with origin at bottom-left). |
required |
polygon
|
List[Tuple[float, float]]
|
Optional list of coordinate points [(x1,y1), (x2,y2), ...] for non-rectangular regions. If provided, the region will use polygon-based intersection calculations instead of simple rectangle overlap. |
None
|
parent
|
Optional parent region for hierarchical document structure. Useful for maintaining tree-like relationships between regions. |
None
|
|
label
|
Optional[str]
|
Optional descriptive label for the region, useful for debugging and identification in complex workflows. |
None
|
Example
pdf = npdf.PDF("document.pdf")
page = pdf.pages[0]
# Rectangular region
header = Region(page, (0, 0, page.width, 100), label="header")
# Polygonal region (from layout detection)
table_polygon = [(50, 100), (300, 100), (300, 400), (50, 400)]
table_region = Region(page, (50, 100, 300, 400),
polygon=table_polygon, label="table")
Note
Regions are typically created through page methods like page.region() or spatial navigation methods like element.below(). Direct instantiation is used mainly for advanced workflows or layout analysis integration.
Source code in natural_pdf/elements/region.py
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 | |
natural_pdf.Region.__radd__(other)
Right-hand addition to support ElementCollection + Region.
Source code in natural_pdf/elements/region.py
3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 | |
natural_pdf.Region.__repr__()
String representation of the region.
Source code in natural_pdf/elements/region.py
3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 | |
natural_pdf.Region.above(height=None, width='full', include_source=False, until=None, include_endpoint=True, offset=None, **kwargs)
Select region above this region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
|
Optional[float]
|
Height of the region above, in points |
None
|
width
|
str
|
Width mode - "full" for full page width or "element" for element width |
'full'
|
include_source
|
bool
|
Whether to include this region in the result (default: False) |
False
|
until
|
Optional[str]
|
Optional selector string to specify an upper boundary element |
None
|
include_endpoint
|
bool
|
Whether to include the boundary element in the region (default: True) |
True
|
offset
|
Optional[float]
|
Pixel offset when excluding source/endpoint (default: None, uses natural_pdf.options.layout.directional_offset) |
None
|
**kwargs
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
Region
|
Region object representing the area above |
Source code in natural_pdf/elements/region.py
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 | |
natural_pdf.Region.add_child(child)
Add a child region to this region.
Used for hierarchical document structure when using models like Docling that understand document hierarchy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
Region object to add as a child |
required |
Returns:
| Type | Description |
|---|---|
|
Self for method chaining |
Source code in natural_pdf/elements/region.py
3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 | |
natural_pdf.Region.analyze_text_table_structure(snap_tolerance=10, join_tolerance=3, min_words_vertical=3, min_words_horizontal=1, intersection_tolerance=3, expand_bbox=None, **kwargs)
Analyzes the text elements within the region (or slightly expanded area) to find potential table structure (lines, cells) using text alignment logic adapted from pdfplumber.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snap_tolerance
|
int
|
Tolerance for snapping parallel lines. |
10
|
join_tolerance
|
int
|
Tolerance for joining collinear lines. |
3
|
min_words_vertical
|
int
|
Minimum words needed to define a vertical line. |
3
|
min_words_horizontal
|
int
|
Minimum words needed to define a horizontal line. |
1
|
intersection_tolerance
|
int
|
Tolerance for detecting line intersections. |
3
|
expand_bbox
|
Optional[Dict[str, int]]
|
Optional dictionary to expand the search area slightly beyond the region's exact bounds (e.g., {'left': 5, 'right': 5}). |
None
|
**kwargs
|
Additional keyword arguments passed to find_text_based_tables (e.g., specific x/y tolerances). |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Dict]
|
A dictionary containing 'horizontal_edges', 'vertical_edges', 'cells' (list of dicts), |
Optional[Dict]
|
and 'intersections', or None if pdfplumber is unavailable or an error occurs. |
Source code in natural_pdf/elements/region.py
3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 | |
natural_pdf.Region.apply_custom_ocr(ocr_function, source_label='custom-ocr', replace=True, confidence=None, add_to_page=True)
Apply a custom OCR function to this region and create text elements from the results.
This is useful when you want to use a custom OCR method (e.g., an LLM API, specialized OCR service, or any custom logic) instead of the built-in OCR engines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ocr_function
|
Callable[[Region], Optional[str]]
|
A callable that takes a Region and returns the OCR'd text (or None). The function receives this region as its argument and should return the extracted text as a string, or None if no text was found. |
required |
source_label
|
str
|
Label to identify the source of these text elements (default: "custom-ocr"). This will be set as the 'source' attribute on created elements. |
'custom-ocr'
|
replace
|
bool
|
If True (default), removes existing OCR elements in this region before adding new ones. If False, adds new OCR elements alongside existing ones. |
True
|
confidence
|
Optional[float]
|
Optional confidence score for the OCR result (0.0-1.0). If None, defaults to 1.0 if text is returned, 0.0 if None is returned. |
None
|
add_to_page
|
bool
|
If True (default), adds the created text element to the page. If False, creates the element but doesn't add it to the page. |
True
|
Returns:
| Type | Description |
|---|---|
Region
|
Self for method chaining. |
Example
Using with an LLM
def ocr_with_llm(region): image = region.render(resolution=300, crop=True) # Call your LLM API here return llm_client.ocr(image)
region.apply_custom_ocr(ocr_with_llm)
Using with a custom OCR service
def ocr_with_service(region): img_bytes = region.render(crop=True).tobytes() response = ocr_service.process(img_bytes) return response.text
region.apply_custom_ocr(ocr_with_service, source_label="my-ocr-service")
Source code in natural_pdf/elements/region.py
2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 | |
natural_pdf.Region.apply_ocr(replace=True, **ocr_params)
Apply OCR to this region and return the created text elements.
This method supports two modes:
1. Built-in OCR Engines (default) – identical to previous behaviour. Pass typical
parameters like engine='easyocr' or languages=['en'] and the method will
route the request through :class:OCRManager.
2. Custom OCR Function – pass a callable under the keyword function (or
ocr_function). The callable will receive this Region instance and should
return the extracted text (str) or None. Internally the call is
delegated to :pymeth:apply_custom_ocr so the same logic (replacement, element
creation, etc.) is re-used.
Examples
def llm_ocr(region):
image = region.render(resolution=300, crop=True)
return my_llm_client.ocr(image)
region.apply_ocr(function=llm_ocr)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
replace
|
Whether to remove existing OCR elements first (default |
True
|
|
**ocr_params
|
Parameters for the built-in OCR manager or the special
|
{}
|
Returns
Self – for chaining.
Source code in natural_pdf/elements/region.py
2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 | |
natural_pdf.Region.ask(question, min_confidence=0.1, model=None, debug=False, **kwargs)
Ask a question about the region content using document QA.
This method uses a document question answering model to extract answers from the region content. It leverages both textual content and layout information for better understanding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
Union[str, List[str], Tuple[str, ...]]
|
The question to ask about the region content |
required |
min_confidence
|
float
|
Minimum confidence threshold for answers (0.0-1.0) |
0.1
|
model
|
str
|
Optional model name to use for QA (if None, uses default model) |
None
|
**kwargs
|
Additional parameters to pass to the QA engine |
{}
|
Returns:
| Type | Description |
|---|---|
Union[Dict[str, Any], List[Dict[str, Any]]]
|
Dictionary with answer details: { "answer": extracted text, "confidence": confidence score, "found": whether an answer was found, "page_num": page number, "region": reference to this region, "source_elements": list of elements that contain the answer (if found) |
Union[Dict[str, Any], List[Dict[str, Any]]]
|
} |
Source code in natural_pdf/elements/region.py
3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 | |
natural_pdf.Region.attr(name)
Get an attribute value from this region.
This method provides a consistent interface for attribute access that works on both individual regions/elements and collections. When called on a single region, it simply returns the attribute value. When called on collections, it extracts the attribute from all items.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The attribute name to retrieve (e.g., 'text', 'width', 'height') |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The attribute value, or None if the attribute doesn't exist |
Examples:
On a single region
region = page.find('text:contains("Title")').expand(10) width = region.attr('width') # Same as region.width
Consistent API across elements and regions
obj = page.find('*:contains("Title")') # Could be element or region text = obj.attr('text') # Works for both
Source code in natural_pdf/elements/region.py
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 | |
natural_pdf.Region.below(height=None, width='full', include_source=False, until=None, include_endpoint=True, offset=None, **kwargs)
Select region below this region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
height
|
Optional[float]
|
Height of the region below, in points |
None
|
width
|
str
|
Width mode - "full" for full page width or "element" for element width |
'full'
|
include_source
|
bool
|
Whether to include this region in the result (default: False) |
False
|
until
|
Optional[str]
|
Optional selector string to specify a lower boundary element |
None
|
include_endpoint
|
bool
|
Whether to include the boundary element in the region (default: True) |
True
|
offset
|
Optional[float]
|
Pixel offset when excluding source/endpoint (default: None, uses natural_pdf.options.layout.directional_offset) |
None
|
**kwargs
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
Region
|
Region object representing the area below |
Source code in natural_pdf/elements/region.py
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 | |
natural_pdf.Region.clip(obj=None, left=None, top=None, right=None, bottom=None)
Clip this region to specific bounds, either from another object with bbox or explicit coordinates.
The clipped region will be constrained to not exceed the specified boundaries. You can provide either an object with bounding box properties, specific coordinates, or both. When both are provided, explicit coordinates take precedence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Optional[Any]
|
Optional object with bbox properties (Region, Element, TextElement, etc.) |
None
|
left
|
Optional[float]
|
Optional left boundary (x0) to clip to |
None
|
top
|
Optional[float]
|
Optional top boundary to clip to |
None
|
right
|
Optional[float]
|
Optional right boundary (x1) to clip to |
None
|
bottom
|
Optional[float]
|
Optional bottom boundary to clip to |
None
|
Returns:
| Type | Description |
|---|---|
Region
|
New Region with bounds clipped to the specified constraints |
Examples:
Clip to another region's bounds
clipped = region.clip(container_region)
Clip to any element's bounds
clipped = region.clip(text_element)
Clip to specific coordinates
clipped = region.clip(left=100, right=400)
Mix object bounds with specific overrides
clipped = region.clip(obj=container, bottom=page.height/2)
Source code in natural_pdf/elements/region.py
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 | |
natural_pdf.Region.contains(element)
Check if this region completely contains an element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Element
|
Element to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the element is completely contained within the region, False otherwise |
Source code in natural_pdf/elements/region.py
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 | |
natural_pdf.Region.create_cells()
Create cell regions for a detected table by intersecting its row and column regions, and add them to the page.
Assumes child row and column regions are already present on the page.
Returns:
| Type | Description |
|---|---|
|
Self for method chaining. |
Source code in natural_pdf/elements/region.py
3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 | |
natural_pdf.Region.exclude()
Exclude this region from text extraction and other operations.
This excludes everything within the region's bounds.
Source code in natural_pdf/elements/region.py
816 817 818 819 820 821 822 | |
natural_pdf.Region.extract_table(method=None, table_settings=None, use_ocr=False, ocr_config=None, text_options=None, cell_extraction_func=None, show_progress=False, content_filter=None, apply_exclusions=True, verticals=None, horizontals=None)
Extract a table from this region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Optional[str]
|
Method to use: 'tatr', 'pdfplumber', 'text', 'stream', 'lattice', or None (auto-detect).
'stream' is an alias for 'pdfplumber' with text-based strategies (equivalent to
setting |
None
|
table_settings
|
Optional[dict]
|
Settings for pdfplumber table extraction (used with 'pdfplumber', 'stream', or 'lattice' methods). |
None
|
use_ocr
|
bool
|
Whether to use OCR for text extraction (currently only applicable with 'tatr' method). |
False
|
ocr_config
|
Optional[dict]
|
OCR configuration parameters. |
None
|
text_options
|
Optional[Dict]
|
Dictionary of options for the 'text' method, corresponding to arguments of analyze_text_table_structure (e.g., snap_tolerance, expand_bbox). |
None
|
cell_extraction_func
|
Optional[Callable[[Region], Optional[str]]]
|
Optional callable function that takes a cell Region object and returns its string content. Overrides default text extraction for the 'text' method. |
None
|
show_progress
|
bool
|
If True, display a progress bar during cell text extraction for the 'text' method. |
False
|
content_filter
|
Optional[Union[str, Callable[[str], bool], List[str]]]
|
Optional content filter to apply during cell text extraction. Can be: - A regex pattern string (characters matching the pattern are EXCLUDED) - A callable that takes text and returns True to KEEP the character - A list of regex patterns (characters matching ANY pattern are EXCLUDED) Works with all extraction methods by filtering cell content. |
None
|
apply_exclusions
|
bool
|
Whether to apply exclusion regions during text extraction (default: True). When True, text within excluded regions (e.g., headers/footers) will not be extracted. |
True
|
verticals
|
Optional[List]
|
Optional list of explicit vertical lines for table extraction. When provided, automatically sets vertical_strategy='explicit' and explicit_vertical_lines. |
None
|
horizontals
|
Optional[List]
|
Optional list of explicit horizontal lines for table extraction. When provided, automatically sets horizontal_strategy='explicit' and explicit_horizontal_lines. |
None
|
Returns:
| Type | Description |
|---|---|
TableResult
|
Table data as a list of rows, where each row is a list of cell values (str or None). |
Source code in natural_pdf/elements/region.py
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 | |
natural_pdf.Region.extract_tables(method=None, table_settings=None)
Extract all tables from this region using pdfplumber-based methods.
Note: Only 'pdfplumber', 'stream', and 'lattice' methods are supported for extract_tables. 'tatr' and 'text' methods are designed for single table extraction only.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
Optional[str]
|
Method to use: 'pdfplumber', 'stream', 'lattice', or None (auto-detect). 'stream' uses text-based strategies, 'lattice' uses line-based strategies. |
None
|
table_settings
|
Optional[dict]
|
Settings for pdfplumber table extraction. |
None
|
Returns:
| Type | Description |
|---|---|
List[List[List[str]]]
|
List of tables, where each table is a list of rows, and each row is a list of cell values. |
Source code in natural_pdf/elements/region.py
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 | |
natural_pdf.Region.extract_text(granularity='chars', apply_exclusions=True, debug=False, *, overlap='center', newlines=True, content_filter=None, **kwargs)
Extract text from this region, respecting page exclusions and using pdfplumber's layout engine (chars_to_textmap).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
granularity
|
str
|
Level of text extraction - 'chars' (default) or 'words'. - 'chars': Character-by-character extraction (current behavior) - 'words': Word-level extraction with configurable overlap |
'chars'
|
apply_exclusions
|
bool
|
Whether to apply exclusion regions defined on the parent page. |
True
|
debug
|
bool
|
Enable verbose debugging output for filtering steps. |
False
|
overlap
|
str
|
How to determine if words overlap with the region (only used when granularity='words'): - 'center': Word center point must be inside (default) - 'full': Word must be fully inside the region - 'partial': Any overlap includes the word |
'center'
|
newlines
|
Union[bool, str]
|
Whether to strip newline characters from the extracted text. |
True
|
content_filter
|
Optional content filter to exclude specific text patterns. Can be: - A regex pattern string (characters matching the pattern are EXCLUDED) - A callable that takes text and returns True to KEEP the character - A list of regex patterns (characters matching ANY pattern are EXCLUDED) |
None
|
|
**kwargs
|
Additional layout parameters passed directly to pdfplumber's
|
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Extracted text as string, potentially with layout-based spacing. |
Source code in natural_pdf/elements/region.py
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 | |
natural_pdf.Region.find(selector=None, *, text=None, overlap='full', apply_exclusions=True, regex=False, case=True, **kwargs)
find(*, text: str, overlap: str = 'full', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> Optional[Element]
find(selector: str, *, overlap: str = 'full', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> Optional[Element]
Find the first element in this region matching the selector OR text content.
Provide EITHER selector OR text, but not both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string. |
None
|
text
|
Optional[str]
|
Text content to search for (equivalent to 'text:contains(...)'). |
None
|
overlap
|
str
|
How to determine if elements overlap with the region: 'full' (fully inside), 'partial' (any overlap), or 'center' (center point inside). (default: "full") |
'full'
|
apply_exclusions
|
bool
|
Whether to exclude elements in exclusion regions (default: True). |
True
|
regex
|
bool
|
Whether to use regex for text search ( |
False
|
case
|
bool
|
Whether to do case-sensitive text search ( |
True
|
**kwargs
|
Additional parameters for element filtering. |
{}
|
Returns:
| Type | Description |
|---|---|
Optional[Element]
|
First matching element or None. |
Source code in natural_pdf/elements/region.py
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 | |
natural_pdf.Region.find_all(selector=None, *, text=None, overlap='full', apply_exclusions=True, regex=False, case=True, **kwargs)
find_all(*, text: str, overlap: str = 'full', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
find_all(selector: str, *, overlap: str = 'full', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
Find all elements in this region matching the selector OR text content.
Provide EITHER selector OR text, but not both.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
CSS-like selector string. |
None
|
text
|
Optional[str]
|
Text content to search for (equivalent to 'text:contains(...)'). |
None
|
overlap
|
str
|
How to determine if elements overlap with the region: 'full' (fully inside), 'partial' (any overlap), or 'center' (center point inside). (default: "full") |
'full'
|
apply_exclusions
|
bool
|
Whether to exclude elements in exclusion regions (default: True). |
True
|
regex
|
bool
|
Whether to use regex for text search ( |
False
|
case
|
bool
|
Whether to do case-sensitive text search ( |
True
|
**kwargs
|
Additional parameters for element filtering. |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection
|
ElementCollection with matching elements. |
Source code in natural_pdf/elements/region.py
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 | |
natural_pdf.Region.get_children(selector=None)
Get immediate child regions, optionally filtered by selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional selector to filter children |
None
|
Returns:
| Type | Description |
|---|---|
|
List of child regions matching the selector |
Source code in natural_pdf/elements/region.py
3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 | |
natural_pdf.Region.get_descendants(selector=None)
Get all descendant regions (children, grandchildren, etc.), optionally filtered by selector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional selector to filter descendants |
None
|
Returns:
| Type | Description |
|---|---|
|
List of descendant regions matching the selector |
Source code in natural_pdf/elements/region.py
3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 | |
natural_pdf.Region.get_elements(selector=None, apply_exclusions=True, **kwargs)
Get all elements within this region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selector
|
Optional[str]
|
Optional selector to filter elements |
None
|
apply_exclusions
|
Whether to apply exclusion regions |
True
|
|
**kwargs
|
Additional parameters for element filtering |
{}
|
Returns:
| Type | Description |
|---|---|
List[Element]
|
List of elements in the region |
Source code in natural_pdf/elements/region.py
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 | |
natural_pdf.Region.get_section_between(start_element=None, end_element=None, include_boundaries='both', orientation='vertical')
Get a section between two elements within this region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_element
|
Element marking the start of the section |
None
|
|
end_element
|
Element marking the end of the section |
None
|
|
include_boundaries
|
How to include boundary elements: 'start', 'end', 'both', or 'none' |
'both'
|
|
orientation
|
'vertical' (default) or 'horizontal' - determines section direction |
'vertical'
|
Returns:
| Type | Description |
|---|---|
|
Region representing the section |
Source code in natural_pdf/elements/region.py
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 | |
natural_pdf.Region.get_sections(start_elements=None, end_elements=None, include_boundaries='both', orientation='vertical')
Get sections within this region based on start/end elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_elements
|
Elements or selector string that mark the start of sections |
None
|
|
end_elements
|
Elements or selector string that mark the end of sections |
None
|
|
include_boundaries
|
How to include boundary elements: 'start', 'end', 'both', or 'none' |
'both'
|
|
orientation
|
'vertical' (default) or 'horizontal' - determines section direction |
'vertical'
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
List of Region objects representing the extracted sections |
Source code in natural_pdf/elements/region.py
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 | |
natural_pdf.Region.get_text_table_cells(snap_tolerance=10, join_tolerance=3, min_words_vertical=3, min_words_horizontal=1, intersection_tolerance=3, expand_bbox=None, **kwargs)
Analyzes text alignment to find table cells and returns them as temporary Region objects without adding them to the page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
snap_tolerance
|
int
|
Tolerance for snapping parallel lines. |
10
|
join_tolerance
|
int
|
Tolerance for joining collinear lines. |
3
|
min_words_vertical
|
int
|
Minimum words needed to define a vertical line. |
3
|
min_words_horizontal
|
int
|
Minimum words needed to define a horizontal line. |
1
|
intersection_tolerance
|
int
|
Tolerance for detecting line intersections. |
3
|
expand_bbox
|
Optional[Dict[str, int]]
|
Optional dictionary to expand the search area slightly beyond the region's exact bounds (e.g., {'left': 5, 'right': 5}). |
None
|
**kwargs
|
Additional keyword arguments passed to find_text_based_tables (e.g., specific x/y tolerances). |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
An ElementCollection containing temporary Region objects for each detected cell, |
ElementCollection[Region]
|
or an empty ElementCollection if no cells are found or an error occurs. |
Source code in natural_pdf/elements/region.py
3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 | |
natural_pdf.Region.highlight(label=None, color=None, use_color_cycling=False, annotate=None, existing='append')
Highlight this region on the page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
Optional[str]
|
Optional label for the highlight |
None
|
color
|
Optional[Union[Tuple, str]]
|
Color tuple/string for the highlight, or None to use automatic color |
None
|
use_color_cycling
|
bool
|
Force color cycling even with no label (default: False) |
False
|
annotate
|
Optional[List[str]]
|
List of attribute names to display on the highlight (e.g., ['confidence', 'type']) |
None
|
existing
|
str
|
How to handle existing highlights ('append' or 'replace'). |
'append'
|
Returns:
| Type | Description |
|---|---|
Region
|
Self for method chaining |
Source code in natural_pdf/elements/region.py
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 | |
natural_pdf.Region.intersects(element)
Check if this region intersects with an element (any overlap).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Element
|
Element to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the element overlaps with the region at all, False otherwise |
Source code in natural_pdf/elements/region.py
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 814 | |
natural_pdf.Region.is_element_center_inside(element)
Check if the center point of an element's bounding box is inside this region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
element
|
Element
|
Element to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the element's center point is inside the region, False otherwise. |
Source code in natural_pdf/elements/region.py
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 | |
natural_pdf.Region.is_point_inside(x, y)
Check if a point is inside this region using ray casting algorithm for polygons.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
X coordinate of the point |
required |
y
|
float
|
Y coordinate of the point |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the point is inside the region |
Source code in natural_pdf/elements/region.py
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 | |
natural_pdf.Region.left(width=None, height='element', include_source=False, until=None, include_endpoint=True, offset=None, **kwargs)
Select region to the left of this region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
Optional[float]
|
Width of the region to the left, in points |
None
|
height
|
str
|
Height mode - "full" for full page height or "element" for element height |
'element'
|
include_source
|
bool
|
Whether to include this region in the result (default: False) |
False
|
until
|
Optional[str]
|
Optional selector string to specify a left boundary element |
None
|
include_endpoint
|
bool
|
Whether to include the boundary element in the region (default: True) |
True
|
offset
|
Optional[float]
|
Pixel offset when excluding source/endpoint (default: None, uses natural_pdf.options.layout.directional_offset) |
None
|
**kwargs
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
Region
|
Region object representing the area to the left |
Source code in natural_pdf/elements/region.py
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 | |
natural_pdf.Region.region(left=None, top=None, right=None, bottom=None, width=None, height=None, relative=False)
Create a sub-region within this region using the same API as Page.region().
By default, coordinates are absolute (relative to the page), matching Page.region(). Set relative=True to use coordinates relative to this region's top-left corner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left
|
float
|
Left x-coordinate (absolute by default, or relative to region if relative=True) |
None
|
top
|
float
|
Top y-coordinate (absolute by default, or relative to region if relative=True) |
None
|
right
|
float
|
Right x-coordinate (absolute by default, or relative to region if relative=True) |
None
|
bottom
|
float
|
Bottom y-coordinate (absolute by default, or relative to region if relative=True) |
None
|
width
|
Union[str, float, None]
|
Width definition (same as Page.region()) |
None
|
height
|
Optional[float]
|
Height of the region (same as Page.region()) |
None
|
relative
|
bool
|
If True, coordinates are relative to this region's top-left (0,0). If False (default), coordinates are absolute page coordinates. |
False
|
Returns:
| Type | Description |
|---|---|
Region
|
Region object for the specified coordinates, clipped to this region's bounds |
Examples:
Absolute coordinates (default) - same as page.region()
sub = region.region(left=100, top=200, width=50, height=30)
Relative to region's top-left
sub = region.region(left=10, top=10, width=50, height=30, relative=True)
Mix relative positioning with this region's bounds
sub = region.region(left=region.x0 + 10, width=50, height=30)
Source code in natural_pdf/elements/region.py
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 | |
natural_pdf.Region.right(width=None, height='element', include_source=False, until=None, include_endpoint=True, offset=None, **kwargs)
Select region to the right of this region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
width
|
Optional[float]
|
Width of the region to the right, in points |
None
|
height
|
str
|
Height mode - "full" for full page height or "element" for element height |
'element'
|
include_source
|
bool
|
Whether to include this region in the result (default: False) |
False
|
until
|
Optional[str]
|
Optional selector string to specify a right boundary element |
None
|
include_endpoint
|
bool
|
Whether to include the boundary element in the region (default: True) |
True
|
offset
|
Optional[float]
|
Pixel offset when excluding source/endpoint (default: None, uses natural_pdf.options.layout.directional_offset) |
None
|
**kwargs
|
Additional parameters |
{}
|
Returns:
| Type | Description |
|---|---|
Region
|
Region object representing the area to the right |
Source code in natural_pdf/elements/region.py
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 | |
natural_pdf.Region.save(filename, resolution=None, labels=True, legend_position='right')
Save the page with this region highlighted to an image file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to save the image to |
required |
resolution
|
Optional[float]
|
Resolution in DPI for rendering (default: uses global options, fallback to 144 DPI) |
None
|
labels
|
bool
|
Whether to include a legend for labels |
True
|
legend_position
|
str
|
Position of the legend |
'right'
|
Returns:
| Type | Description |
|---|---|
Region
|
Self for method chaining |
Source code in natural_pdf/elements/region.py
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 | |
natural_pdf.Region.save_image(filename, resolution=None, crop=False, include_highlights=True, **kwargs)
Save an image of just this region to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Path to save the image to |
required |
resolution
|
Optional[float]
|
Resolution in DPI for rendering (default: uses global options, fallback to 144 DPI) |
None
|
crop
|
bool
|
If True, only crop the region without highlighting its boundaries |
False
|
include_highlights
|
bool
|
Whether to include existing highlights (default: True) |
True
|
**kwargs
|
Additional parameters for rendering |
{}
|
Returns:
| Type | Description |
|---|---|
Region
|
Self for method chaining |
Source code in natural_pdf/elements/region.py
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 | |
natural_pdf.Region.split(divider, **kwargs)
Divide this region into sections based on the provided divider elements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
divider
|
Elements or selector string that mark section boundaries |
required | |
**kwargs
|
Additional parameters passed to get_sections() - include_boundaries: How to include boundary elements (default: 'start') - orientation: 'vertical' or 'horizontal' (default: 'vertical') |
{}
|
Returns:
| Type | Description |
|---|---|
ElementCollection[Region]
|
ElementCollection of Region objects representing the sections |
Example
Split a region by bold text
sections = region.split("text:bold")
Split horizontally by vertical lines
sections = region.split("line[orientation=vertical]", orientation="horizontal")
Source code in natural_pdf/elements/region.py
3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 | |
natural_pdf.Region.to_text_element(text_content=None, source_label='derived_from_region', object_type='word', default_font_size=10.0, default_font_name='RegionContent', confidence=None, add_to_page=False)
Creates a new TextElement object based on this region's geometry.
The text for the new TextElement can be provided directly, generated by a callback function, or left as None.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text_content
|
Optional[Union[str, Callable[[Region], Optional[str]]]]
|
|
None
|
source_label
|
str
|
The 'source' attribute for the new TextElement. |
'derived_from_region'
|
object_type
|
str
|
The 'object_type' for the TextElement's data dict (e.g., "word", "char"). |
'word'
|
default_font_size
|
float
|
Placeholder font size if text is generated. |
10.0
|
default_font_name
|
str
|
Placeholder font name if text is generated. |
'RegionContent'
|
confidence
|
Optional[float]
|
Confidence score for the text. If text_content is None, defaults to 0.0. If text is provided/generated, defaults to 1.0 unless specified. |
None
|
add_to_page
|
bool
|
If True, the created TextElement will be added to the region's parent page. (Default: False) |
False
|
Returns:
| Type | Description |
|---|---|
TextElement
|
A new TextElement instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the region does not have a valid 'page' attribute. |
Source code in natural_pdf/elements/region.py
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 | |
natural_pdf.Region.trim(padding=1, threshold=0.95, resolution=None, pre_shrink=0.5)
Trim visual whitespace from the edges of this region.
Similar to Python's string .strip() method, but for visual whitespace in the region image. Uses pixel analysis to detect rows/columns that are predominantly whitespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
padding
|
int
|
Number of pixels to keep as padding after trimming (default: 1) |
1
|
threshold
|
float
|
Threshold for considering a row/column as whitespace (0.0-1.0, default: 0.95) Higher values mean more strict whitespace detection. E.g., 0.95 means if 95% of pixels in a row/column are white, consider it whitespace. |
0.95
|
resolution
|
Optional[float]
|
Resolution for image rendering in DPI (default: uses global options, fallback to 144 DPI) |
None
|
pre_shrink
|
float
|
Amount to shrink region before trimming, then expand back after (default: 0.5) This helps avoid detecting box borders/slivers as content. |
0.5
|
Returns
New Region with visual whitespace trimmed from all edges
Examples
# Basic trimming with 1 pixel padding and 0.5px pre-shrink
trimmed = region.trim()
# More aggressive trimming with no padding and no pre-shrink
tight = region.trim(padding=0, threshold=0.9, pre_shrink=0)
# Conservative trimming with more padding
loose = region.trim(padding=3, threshold=0.98)
Source code in natural_pdf/elements/region.py
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 | |
natural_pdf.Region.update_text(transform, *, selector='text', apply_exclusions=False)
Apply transform to every text element matched by selector inside this region.
The heavy lifting is delegated to :py:meth:TextMixin.update_text; this
override simply ensures the search is scoped to the region.
Source code in natural_pdf/elements/region.py
3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 | |
natural_pdf.Region.viewer(*, resolution=150, include_chars=False, include_attributes=None)
Create an interactive ipywidget viewer for this specific region.
The method renders the region to an image (cropped to the region bounds) and
overlays all elements that intersect the region (optionally excluding noisy
character-level elements). The resulting widget offers the same zoom / pan
experience as :py:meth:Page.viewer but scoped to the region.
Parameters
resolution : int, default 150 Rendering resolution (DPI). This should match the value used by the page-level viewer so element scaling is accurate. include_chars : bool, default False Whether to include individual char elements in the overlay. These are often too dense for a meaningful visualisation so are skipped by default. include_attributes : list[str], optional Additional element attributes to expose in the info panel (on top of the default set used by the page viewer).
Returns
InteractiveViewerWidget | None
The widget instance, or None if ipywidgets is not installed or
an error occurred during creation.
Source code in natural_pdf/elements/region.py
4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 | |
natural_pdf.Region.within()
Context manager that constrains directional operations to this region.
When used as a context manager, all directional navigation operations (above, below, left, right) will be constrained to the bounds of this region.
Returns:
| Name | Type | Description |
|---|---|---|
RegionContext |
A context manager that yields this region |
Examples:
# Create a column region
left_col = page.region(right=page.width/2)
# All directional operations are constrained to left_col
with left_col.within() as col:
header = col.find("text[size>14]")
content = header.below(until="text[size>14]")
# content will only include elements within left_col
# Operations outside the context are not constrained
full_page_below = header.below() # Searches full page
Source code in natural_pdf/elements/region.py
4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 | |
Functions
natural_pdf.configure_logging(level=logging.INFO, handler=None)
Configure logging for the natural_pdf package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Logging level (e.g., logging.INFO, logging.DEBUG) |
INFO
|
|
handler
|
Optional custom handler. Defaults to a StreamHandler. |
None
|
Source code in natural_pdf/__init__.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | |
natural_pdf.set_option(name, value)
Set a global Natural PDF option.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Option name in dot notation (e.g., 'layout.auto_multipage') |
required |
value
|
New value for the option |
required |
Example
import natural_pdf as npdf npdf.set_option('layout.auto_multipage', True) npdf.set_option('ocr.engine', 'surya')
Source code in natural_pdf/__init__.py
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 | |