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.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 |
|
natural_pdf.PDF
Bases: ExtractionMixin
, ExportMixin
, ClassificationMixin
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
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 |
|
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
1738 1739 1740 |
|
natural_pdf.PDF.__exit__(exc_type, exc_val, exc_tb)
Context manager exit.
Source code in natural_pdf/core/pdf.py
1742 1743 1744 |
|
natural_pdf.PDF.__getitem__(key)
Access pages by index or slice.
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 |
|
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
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 |
|
natural_pdf.PDF.__len__()
Return the number of pages in the PDF.
Source code in natural_pdf/core/pdf.py
1687 1688 1689 1690 1691 |
|
natural_pdf.PDF.__repr__()
Return a string representation of the PDF object.
Source code in natural_pdf/core/pdf.py
1746 1747 1748 1749 1750 1751 1752 1753 1754 |
|
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
|
Callable[[Page], Optional[Region]]
|
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
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 |
|
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 |
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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
natural_pdf.PDF.close()
Close the underlying PDF file and clean up any temporary files.
Source code in natural_pdf/core/pdf.py
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 |
|
natural_pdf.PDF.correct_ocr(correction_callback, pages=None, max_workers=None, progress_callback=None)
Applies corrections to OCR text elements using a callback function. Applies corrections to OCR text elements using a callback function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
correction_callback
|
Callable[[Any], Optional[str]]
|
Function that takes an element and returns corrected text or None |
required |
correction_callback
|
Callable[[Any], Optional[str]]
|
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
|
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
|
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 |
PDF
|
Self for method chaining |
Source code in natural_pdf/core/pdf.py
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
natural_pdf.PDF.get_id()
Get unique identifier for this PDF.
Source code in natural_pdf/core/pdf.py
1756 1757 1758 1759 |
|
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
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 |
|
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
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 |
|
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
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 |
|
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
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 |
|
natural_pdf.Page
Bases: ClassificationMixin
, ExtractionMixin
, ShapeDetectionMixin
, DescribeMixin
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
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 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 |
|
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
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 |
|
natural_pdf.Page.__repr__()
String representation of the page.
Source code in natural_pdf/core/page.py
2482 2483 2484 |
|
natural_pdf.Page.add_exclusion(exclusion_func_or_region, label=None)
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, Any]
|
Either a callable function returning a Region, a Region object, or another object with a valid .bbox attribute. |
required |
label
|
Optional[str]
|
Optional label for this exclusion (e.g., 'header', 'footer'). |
None
|
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. |
Source code in natural_pdf/core/page.py
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 |
|
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
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 |
|
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
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
|
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
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.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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
natural_pdf.Page.clear_exclusions()
Clear all exclusions from the page.
Source code in natural_pdf/core/page.py
304 305 306 307 308 309 |
|
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
1706 1707 1708 1709 1710 1711 1712 1713 1714 |
|
natural_pdf.Page.correct_ocr(correction_callback, selector='text[source=ocr]', max_workers=None, progress_callback=None)
Applies corrections to OCR-generated text elements on this page using a user-provided callback function, potentially in parallel.
Finds text elements on this page whose 'source' attribute starts
with 'ocr' and calls the correction_callback
for each, passing the
element itself. Updates the element's text if the callback returns
a new string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
correction_callback
|
Callable[[Any], Optional[str]]
|
A function accepting an element and returning
|
required |
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
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 |
|
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
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 |
|
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
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 |
|
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
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 |
|
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
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 |
|
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
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 |
|
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)
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
|
Returns:
Type | Description |
---|---|
List[List[Optional[str]]]
|
Table data as a list of rows, where each row is a list of cell values (str or None). |
Source code in natural_pdf/core/page.py
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 |
|
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
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 |
|
natural_pdf.Page.extract_text(preserve_whitespace=True, use_exclusions=True, debug_exclusions=False, **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
|
|
**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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
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
2661 2662 2663 2664 2665 2666 |
|
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
2668 2669 2670 2671 2672 2673 2674 2675 2676 |
|
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
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 |
|
natural_pdf.Page.get_id()
Returns a unique identifier for the page (required by Indexable protocol).
Source code in natural_pdf/core/page.py
2643 2644 2645 2646 2647 |
|
natural_pdf.Page.get_metadata()
Returns metadata associated with the page (required by Indexable protocol).
Source code in natural_pdf/core/page.py
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 |
|
natural_pdf.Page.get_section_between(start_element=None, end_element=None, boundary_inclusion='both')
Get a section between two elements on this page.
Source code in natural_pdf/core/page.py
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 |
|
natural_pdf.Page.get_sections(start_elements=None, end_elements=None, boundary_inclusion='start', y_threshold=5.0, bounding_box=None)
Get sections of a page defined by start/end elements. Uses the page-level implementation.
Returns:
Type | Description |
---|---|
ElementCollection[Region]
|
An ElementCollection containing the found Region objects. |
Source code in natural_pdf/core/page.py
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 |
|
natural_pdf.Page.highlight(bbox=None, color=None, label=None, use_color_cycling=False, element=None, include_attrs=None, existing='append')
Highlight 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
|
include_attrs
|
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
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 |
|
natural_pdf.Page.highlight_polygon(polygon, color=None, label=None, use_color_cycling=False, element=None, include_attrs=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
|
include_attrs
|
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
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 |
|
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
3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 |
|
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
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 |
|
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
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 |
|
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 to_image. |
{}
|
Returns:
Type | Description |
---|---|
Page
|
Self for method chaining. |
Source code in natural_pdf/core/page.py
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 |
|
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
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 |
|
natural_pdf.Page.show(resolution=144, width=None, labels=True, legend_position='right', render_ocr=False)
Generates and returns an image of the page with persistent highlights rendered.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
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 for labels. |
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 page with highlights, or None if rendering fails. |
Source code in natural_pdf/core/page.py
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 |
|
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
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 |
|
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
2329 2330 2331 2332 2333 2334 2335 2336 2337 |
|
natural_pdf.Page.to_image(path=None, width=None, labels=True, legend_position='right', render_ocr=False, resolution=None, include_highlights=True, exclusions=None, **kwargs)
Generate a PIL image of the page, using HighlightingService if needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Optional[str]
|
Optional path to save the image to. |
None
|
width
|
Optional[int]
|
Optional width for the output image. |
None
|
labels
|
bool
|
Whether to include a legend for highlights. |
True
|
legend_position
|
str
|
Position of the legend. |
'right'
|
render_ocr
|
bool
|
Whether to render OCR text on highlights. |
False
|
resolution
|
Optional[float]
|
Resolution in DPI for base page image. If None, uses global setting or defaults to 144 DPI. |
None
|
include_highlights
|
bool
|
Whether to render highlights. |
True
|
exclusions
|
Optional[str]
|
Accepts one of the following: • None – no masking (default) • "mask" – mask using solid white (back-compat) • CSS/HTML colour string (e.g. "red", "#ff0000", "#ff000080") • Tuple of RGB or RGBA values (ints 0-255 or floats 0-1) All excluded regions are filled with this colour. |
None
|
**kwargs
|
Additional parameters for pdfplumber.to_image. |
{}
|
Returns:
Type | Description |
---|---|
Optional[Image]
|
PIL Image of the page, or None if rendering fails. |
Source code in natural_pdf/core/page.py
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 |
|
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
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 |
|
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
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 |
|
natural_pdf.Region
Bases: DirectionalMixin
, ClassificationMixin
, ExtractionMixin
, ShapeDetectionMixin
, DescribeMixin
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
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 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 |
|
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.has_polygon
property
Check if this region has polygon coordinates.
natural_pdf.Region.height
property
Get the height of the region.
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.__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
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 |
|
natural_pdf.Region.__repr__()
String representation of the region.
Source code in natural_pdf/elements/region.py
2938 2939 2940 2941 2942 2943 2944 |
|
natural_pdf.Region.above(height=None, width='full', include_source=False, until=None, include_endpoint=True, **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
|
**kwargs
|
Additional parameters |
{}
|
Returns:
Type | Description |
---|---|
Region
|
Region object representing the area above |
Source code in natural_pdf/elements/region.py
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 |
|
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
2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 |
|
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
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 |
|
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.to_image(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.to_image(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
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 |
|
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.to_image(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
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 |
|
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
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 |
|
natural_pdf.Region.below(height=None, width='full', include_source=False, until=None, include_endpoint=True, **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
|
**kwargs
|
Additional parameters |
{}
|
Returns:
Type | Description |
---|---|
Region
|
Region object representing the area below |
Source code in natural_pdf/elements/region.py
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 |
|
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
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 |
|
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
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 |
|
natural_pdf.Region.correct_ocr(correction_callback)
Applies corrections to OCR-generated text elements within this region using a user-provided callback function.
Finds text elements within this region whose 'source' attribute starts
with 'ocr' and calls the correction_callback
for each, passing the
element itself.
The correction_callback
should contain the logic to:
1. Determine if the element needs correction.
2. Perform the correction (e.g., call an LLM).
3. Return the new text (str
) or None
.
If the callback returns a string, the element's .text
is updated.
Metadata updates (source, confidence, etc.) should happen within the callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
correction_callback
|
Callable[[Any], Optional[str]]
|
A function accepting an element and returning
|
required |
Returns:
Type | Description |
---|---|
Region
|
Self for method chaining. |
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 |
|
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
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 |
|
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)
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
|
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
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 |
|
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
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 |
|
natural_pdf.Region.extract_text(apply_exclusions=True, debug=False, **kwargs)
Extract text from this region, respecting page exclusions and using pdfplumber's layout engine (chars_to_textmap).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
apply_exclusions
|
Whether to apply exclusion regions defined on the parent page. |
True
|
|
debug
|
Enable verbose debugging output for filtering steps. |
False
|
|
**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
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 |
|
natural_pdf.Region.find(selector=None, *, text=None, contains='all', apply_exclusions=True, regex=False, case=True, **kwargs)
find(*, text: str, contains: str = 'all', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> Optional[Element]
find(selector: str, *, contains: str = 'all', 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
|
contains
|
str
|
How to determine if elements are inside: 'all' (fully inside), 'any' (any overlap), or 'center' (center point inside). (default: "all") |
'all'
|
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
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 |
|
natural_pdf.Region.find_all(selector=None, *, text=None, contains='all', apply_exclusions=True, regex=False, case=True, **kwargs)
find_all(*, text: str, contains: str = 'all', apply_exclusions: bool = True, regex: bool = False, case: bool = True, **kwargs) -> ElementCollection
find_all(selector: str, *, contains: str = 'all', 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
|
contains
|
str
|
How to determine if elements are inside: 'all' (fully inside), 'any' (any overlap), or 'center' (center point inside). (default: "all") |
'all'
|
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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
natural_pdf.Region.get_section_between(start_element=None, end_element=None, boundary_inclusion='both')
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
|
|
boundary_inclusion
|
How to include boundary elements: 'start', 'end', 'both', or 'none' |
'both'
|
Returns:
Type | Description |
---|---|
Region representing the section |
Source code in natural_pdf/elements/region.py
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 |
|
natural_pdf.Region.get_sections(start_elements=None, end_elements=None, boundary_inclusion='both')
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
|
|
boundary_inclusion
|
How to include boundary elements: 'start', 'end', 'both', or 'none' |
'both'
|
Returns:
Type | Description |
---|---|
ElementCollection[Region]
|
List of Region objects representing the extracted sections |
Source code in natural_pdf/elements/region.py
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 |
|
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
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.highlight(label=None, color=None, use_color_cycling=False, include_attrs=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
|
include_attrs
|
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
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 |
|
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
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 |
|
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
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 |
|
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
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 |
|
natural_pdf.Region.left(width=None, height='full', include_source=False, until=None, include_endpoint=True, **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 |
'full'
|
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
|
**kwargs
|
Additional parameters |
{}
|
Returns:
Type | Description |
---|---|
Region
|
Region object representing the area to the left |
Source code in natural_pdf/elements/region.py
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 |
|
natural_pdf.Region.right(width=None, height='full', include_source=False, until=None, include_endpoint=True, **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 |
'full'
|
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
|
**kwargs
|
Additional parameters |
{}
|
Returns:
Type | Description |
---|---|
Region
|
Region object representing the area to the right |
Source code in natural_pdf/elements/region.py
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 |
|
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
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 |
|
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 page.to_image() |
{}
|
Returns:
Type | Description |
---|---|
Region
|
Self for method chaining |
Source code in natural_pdf/elements/region.py
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 |
|
natural_pdf.Region.show(resolution=None, labels=True, legend_position='right', color='blue', label=None, width=None, crop=False)
Show the page with just this region highlighted temporarily.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
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'
|
color
|
Optional[Union[Tuple, str]]
|
Color to highlight this region (default: blue) |
'blue'
|
label
|
Optional[str]
|
Optional label for this region in the legend |
None
|
width
|
Optional[int]
|
Optional width for the output image in pixels |
None
|
crop
|
bool
|
If True, crop the rendered image to this region's bounding box (with a small margin handled inside HighlightingService) before legends/overlays are added. |
False
|
Returns:
Type | Description |
---|---|
Image
|
PIL Image of the page with only this region highlighted |
Source code in natural_pdf/elements/region.py
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 |
|
natural_pdf.Region.to_image(resolution=None, crop=False, include_highlights=True, **kwargs)
Generate an image of just this region.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
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 page.to_image() |
{}
|
Returns:
Type | Description |
---|---|
Image
|
PIL Image of just this region |
Source code in natural_pdf/elements/region.py
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 |
|
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
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 |
|
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
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 |
|
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 |
|