natural-pdf 0.2.10__py3-none-any.whl → 0.2.12__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- natural_pdf/analyzers/guides.py +318 -73
- natural_pdf/core/page.py +56 -8
- natural_pdf/elements/region.py +5 -3
- {natural_pdf-0.2.10.dist-info → natural_pdf-0.2.12.dist-info}/METADATA +1 -1
- {natural_pdf-0.2.10.dist-info → natural_pdf-0.2.12.dist-info}/RECORD +29 -9
- {natural_pdf-0.2.10.dist-info → natural_pdf-0.2.12.dist-info}/top_level.txt +1 -0
- temp/debug_cell_extraction.py +42 -0
- temp/debug_exclusion_overlap.py +43 -0
- temp/debug_exclusions_guides.py +67 -0
- temp/debug_extra_guide.py +41 -0
- temp/debug_outer_boundaries.py +46 -0
- temp/debug_st_search.py +33 -0
- temp/fix_page_exclusions.py +42 -0
- temp/test_exclusion_with_debug.py +30 -0
- temp/test_find_exclusions_fix.py +53 -0
- temp/test_find_exclusions_fix_no_recursion.py +97 -0
- temp/test_fix_real_pdf.py +48 -0
- temp/test_fix_working.py +55 -0
- temp/test_fixed_pdf_exclusions.py +67 -0
- temp/test_horizontal_top_bottom.py +53 -0
- temp/test_marker_order.py +45 -0
- temp/test_original_exclusions_now_work.py +56 -0
- temp/test_pdf_exclusions_with_guides.py +84 -0
- temp/test_region_exclusions_detailed.py +25 -0
- temp/test_stripes_real_pdf.py +62 -0
- temp/test_vertical_stripes.py +55 -0
- {natural_pdf-0.2.10.dist-info → natural_pdf-0.2.12.dist-info}/WHEEL +0 -0
- {natural_pdf-0.2.10.dist-info → natural_pdf-0.2.12.dist-info}/entry_points.txt +0 -0
- {natural_pdf-0.2.10.dist-info → natural_pdf-0.2.12.dist-info}/licenses/LICENSE +0 -0
natural_pdf/analyzers/guides.py
CHANGED
@@ -185,7 +185,9 @@ class GuidesList(UserList):
|
|
185
185
|
self,
|
186
186
|
markers: Union[str, List[str], "ElementCollection", Callable, None],
|
187
187
|
obj: Optional[Union["Page", "Region", "FlowRegion"]] = None,
|
188
|
-
align:
|
188
|
+
align: Union[
|
189
|
+
Literal["left", "right", "center", "between"], Literal["top", "bottom"]
|
190
|
+
] = "left",
|
189
191
|
outer: bool = True,
|
190
192
|
tolerance: float = 5,
|
191
193
|
*,
|
@@ -203,7 +205,10 @@ class GuidesList(UserList):
|
|
203
205
|
- Callable: function that takes a page and returns markers
|
204
206
|
- None: no markers
|
205
207
|
obj: Page/Region/FlowRegion to search (uses parent's context if None)
|
206
|
-
align: How to align guides relative to found elements
|
208
|
+
align: How to align guides relative to found elements:
|
209
|
+
- For vertical guides: 'left', 'right', 'center', 'between'
|
210
|
+
- For horizontal guides: 'top', 'bottom', 'center', 'between'
|
211
|
+
- Note: 'left'/'right' also work for horizontal (mapped to top/bottom)
|
207
212
|
outer: Whether to add outer boundary guides
|
208
213
|
tolerance: Tolerance for snapping to element edges
|
209
214
|
apply_exclusions: Whether to apply exclusion zones when searching for text
|
@@ -224,19 +229,25 @@ class GuidesList(UserList):
|
|
224
229
|
self._callable = None
|
225
230
|
actual_markers = markers
|
226
231
|
|
232
|
+
# Normalize alignment for horizontal guides
|
233
|
+
if self._axis == "horizontal":
|
234
|
+
if align == "top":
|
235
|
+
align = "left"
|
236
|
+
elif align == "bottom":
|
237
|
+
align = "right"
|
238
|
+
|
227
239
|
# Check if parent is in flow mode
|
228
240
|
if self._parent.is_flow_region:
|
229
241
|
# Create guides across all constituent regions
|
230
242
|
all_guides = []
|
231
243
|
for region in self._parent.context.constituent_regions:
|
232
|
-
#
|
233
|
-
marker_texts = _normalize_markers(actual_markers, region)
|
244
|
+
# Pass markers directly - from_content will handle them properly
|
234
245
|
|
235
246
|
# Create guides for this region
|
236
247
|
region_guides = Guides.from_content(
|
237
248
|
obj=region,
|
238
249
|
axis=self._axis,
|
239
|
-
markers=
|
250
|
+
markers=actual_markers, # Pass original markers, not normalized text
|
240
251
|
align=align,
|
241
252
|
outer=outer,
|
242
253
|
tolerance=tolerance,
|
@@ -312,14 +323,14 @@ class GuidesList(UserList):
|
|
312
323
|
return self._parent
|
313
324
|
|
314
325
|
# Original single-region logic
|
315
|
-
#
|
316
|
-
|
326
|
+
# Pass markers directly to from_content which will handle them properly
|
327
|
+
# (no need to normalize here since from_content now handles ElementCollection)
|
317
328
|
|
318
329
|
# Create guides for this axis
|
319
330
|
new_guides = Guides.from_content(
|
320
331
|
obj=target_obj,
|
321
332
|
axis=self._axis,
|
322
|
-
markers=
|
333
|
+
markers=actual_markers, # Pass original markers, not normalized text
|
323
334
|
align=align,
|
324
335
|
outer=outer,
|
325
336
|
tolerance=tolerance,
|
@@ -930,6 +941,82 @@ class GuidesList(UserList):
|
|
930
941
|
self.data.clear()
|
931
942
|
return self._parent
|
932
943
|
|
944
|
+
def from_stripes(
|
945
|
+
self,
|
946
|
+
stripes=None,
|
947
|
+
color=None, # Explicitly specify stripe color
|
948
|
+
) -> "Guides":
|
949
|
+
"""Create guides from striped table rows or columns.
|
950
|
+
|
951
|
+
Creates guides at both edges of stripe elements (e.g., colored table rows).
|
952
|
+
Perfect for zebra-striped tables where you need guides at every row boundary.
|
953
|
+
|
954
|
+
Args:
|
955
|
+
stripes: Elements representing stripes. If None, auto-detects.
|
956
|
+
color: Specific color to look for (e.g., '#00ffff'). If None, finds most common.
|
957
|
+
|
958
|
+
Examples:
|
959
|
+
# Auto-detect zebra stripes
|
960
|
+
guides.horizontal.from_stripes()
|
961
|
+
|
962
|
+
# Specific color
|
963
|
+
guides.horizontal.from_stripes(color='#00ffff')
|
964
|
+
|
965
|
+
# Manual selection
|
966
|
+
stripes = page.find_all('rect[fill=#00ffff]')
|
967
|
+
guides.horizontal.from_stripes(stripes)
|
968
|
+
|
969
|
+
# Vertical stripes
|
970
|
+
guides.vertical.from_stripes(color='#e0e0e0')
|
971
|
+
|
972
|
+
Returns:
|
973
|
+
Parent Guides object for chaining
|
974
|
+
"""
|
975
|
+
from collections import defaultdict
|
976
|
+
|
977
|
+
target_obj = self._parent.context
|
978
|
+
if target_obj is None:
|
979
|
+
raise ValueError("No context available for stripe detection")
|
980
|
+
|
981
|
+
if stripes is None:
|
982
|
+
if color:
|
983
|
+
# User specified color
|
984
|
+
stripes = target_obj.find_all(f"rect[fill={color}]")
|
985
|
+
else:
|
986
|
+
# Auto-detect most common non-white fill
|
987
|
+
all_rects = target_obj.find_all("rect[fill]")
|
988
|
+
|
989
|
+
# Group by fill color
|
990
|
+
fill_counts = defaultdict(list)
|
991
|
+
for rect in all_rects:
|
992
|
+
if rect.fill and rect.fill not in ["#ffffff", "white", "none", "transparent"]:
|
993
|
+
fill_counts[rect.fill].append(rect)
|
994
|
+
|
995
|
+
if not fill_counts:
|
996
|
+
return self._parent # No stripes found
|
997
|
+
|
998
|
+
# Find most common fill color
|
999
|
+
stripes = max(fill_counts.values(), key=len)
|
1000
|
+
|
1001
|
+
if not stripes:
|
1002
|
+
return self._parent
|
1003
|
+
|
1004
|
+
# Get both edges of each stripe
|
1005
|
+
edges = []
|
1006
|
+
if self._axis == "horizontal":
|
1007
|
+
for stripe in stripes:
|
1008
|
+
edges.extend([stripe.top, stripe.bottom])
|
1009
|
+
else:
|
1010
|
+
for stripe in stripes:
|
1011
|
+
edges.extend([stripe.x0, stripe.x1])
|
1012
|
+
|
1013
|
+
# Remove duplicates and sort
|
1014
|
+
edges = sorted(set(edges))
|
1015
|
+
|
1016
|
+
# Add guides
|
1017
|
+
self.extend(edges)
|
1018
|
+
return self._parent
|
1019
|
+
|
933
1020
|
def __add__(self, other):
|
934
1021
|
"""Handle addition of GuidesList objects by returning combined data."""
|
935
1022
|
if isinstance(other, GuidesList):
|
@@ -1459,7 +1546,9 @@ class Guides:
|
|
1459
1546
|
obj: Union["Page", "Region", "FlowRegion"],
|
1460
1547
|
axis: Literal["vertical", "horizontal"] = "vertical",
|
1461
1548
|
markers: Union[str, List[str], "ElementCollection", None] = None,
|
1462
|
-
align:
|
1549
|
+
align: Union[
|
1550
|
+
Literal["left", "right", "center", "between"], Literal["top", "bottom"]
|
1551
|
+
] = "left",
|
1463
1552
|
outer: bool = True,
|
1464
1553
|
tolerance: float = 5,
|
1465
1554
|
apply_exclusions: bool = True,
|
@@ -1475,7 +1564,9 @@ class Guides:
|
|
1475
1564
|
- List[str]: list of selectors or literal text strings
|
1476
1565
|
- ElementCollection: collection of elements to extract text from
|
1477
1566
|
- None: no markers
|
1478
|
-
align: Where to place guides relative to found text
|
1567
|
+
align: Where to place guides relative to found text:
|
1568
|
+
- For vertical guides: 'left', 'right', 'center', 'between'
|
1569
|
+
- For horizontal guides: 'top', 'bottom', 'center', 'between'
|
1479
1570
|
outer: Whether to add guides at the boundaries
|
1480
1571
|
tolerance: Maximum distance to search for text
|
1481
1572
|
apply_exclusions: Whether to apply exclusion zones when searching for text
|
@@ -1483,6 +1574,13 @@ class Guides:
|
|
1483
1574
|
Returns:
|
1484
1575
|
New Guides object aligned to text content
|
1485
1576
|
"""
|
1577
|
+
# Normalize alignment for horizontal guides
|
1578
|
+
if axis == "horizontal":
|
1579
|
+
if align == "top":
|
1580
|
+
align = "left"
|
1581
|
+
elif align == "bottom":
|
1582
|
+
align = "right"
|
1583
|
+
|
1486
1584
|
# Handle FlowRegion
|
1487
1585
|
if hasattr(obj, "constituent_regions"):
|
1488
1586
|
guides = cls(context=obj)
|
@@ -1530,39 +1628,51 @@ class Guides:
|
|
1530
1628
|
elif hasattr(obj, "width"):
|
1531
1629
|
bounds = (0, 0, obj.width, obj.height)
|
1532
1630
|
|
1533
|
-
#
|
1534
|
-
|
1631
|
+
# Handle different marker types
|
1632
|
+
elements_to_process = []
|
1535
1633
|
|
1536
|
-
#
|
1537
|
-
|
1538
|
-
|
1539
|
-
|
1540
|
-
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1549
|
-
# For between, collect left edges for processing later
|
1550
|
-
guides_coords.append(element.x0)
|
1551
|
-
else: # horizontal
|
1552
|
-
if align == "left": # top for horizontal
|
1553
|
-
guides_coords.append(element.top)
|
1554
|
-
elif align == "right": # bottom for horizontal
|
1555
|
-
guides_coords.append(element.bottom)
|
1556
|
-
elif align == "center":
|
1557
|
-
guides_coords.append((element.top + element.bottom) / 2)
|
1558
|
-
elif align == "between":
|
1559
|
-
# For between, collect top edges for processing later
|
1560
|
-
guides_coords.append(element.top)
|
1634
|
+
# Check if markers is an ElementCollection or has elements attribute
|
1635
|
+
if hasattr(markers, "elements") or hasattr(markers, "_elements"):
|
1636
|
+
# It's an ElementCollection - use elements directly
|
1637
|
+
elements_to_process = getattr(markers, "elements", getattr(markers, "_elements", []))
|
1638
|
+
elif hasattr(markers, "__iter__") and not isinstance(markers, str):
|
1639
|
+
# Check if it's an iterable of elements (not strings)
|
1640
|
+
try:
|
1641
|
+
markers_list = list(markers)
|
1642
|
+
if markers_list and hasattr(markers_list[0], "x0"):
|
1643
|
+
# It's a list of elements
|
1644
|
+
elements_to_process = markers_list
|
1645
|
+
except:
|
1646
|
+
pass
|
1561
1647
|
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1565
|
-
|
1648
|
+
if elements_to_process:
|
1649
|
+
# Process elements directly without text search
|
1650
|
+
for element in elements_to_process:
|
1651
|
+
if axis == "vertical":
|
1652
|
+
if align == "left":
|
1653
|
+
guides_coords.append(element.x0)
|
1654
|
+
elif align == "right":
|
1655
|
+
guides_coords.append(element.x1)
|
1656
|
+
elif align == "center":
|
1657
|
+
guides_coords.append((element.x0 + element.x1) / 2)
|
1658
|
+
elif align == "between":
|
1659
|
+
# For between, collect left edges for processing later
|
1660
|
+
guides_coords.append(element.x0)
|
1661
|
+
else: # horizontal
|
1662
|
+
if align == "left": # top for horizontal
|
1663
|
+
guides_coords.append(element.top)
|
1664
|
+
elif align == "right": # bottom for horizontal
|
1665
|
+
guides_coords.append(element.bottom)
|
1666
|
+
elif align == "center":
|
1667
|
+
guides_coords.append((element.top + element.bottom) / 2)
|
1668
|
+
elif align == "between":
|
1669
|
+
# For between, collect top edges for processing later
|
1670
|
+
guides_coords.append(element.top)
|
1671
|
+
else:
|
1672
|
+
# Fall back to text-based search
|
1673
|
+
marker_texts = _normalize_markers(markers, obj)
|
1674
|
+
|
1675
|
+
# Find each marker and determine guide position
|
1566
1676
|
for marker in marker_texts:
|
1567
1677
|
if hasattr(obj, "find"):
|
1568
1678
|
element = obj.find(
|
@@ -1570,9 +1680,52 @@ class Guides:
|
|
1570
1680
|
)
|
1571
1681
|
if element:
|
1572
1682
|
if axis == "vertical":
|
1573
|
-
|
1683
|
+
if align == "left":
|
1684
|
+
guides_coords.append(element.x0)
|
1685
|
+
elif align == "right":
|
1686
|
+
guides_coords.append(element.x1)
|
1687
|
+
elif align == "center":
|
1688
|
+
guides_coords.append((element.x0 + element.x1) / 2)
|
1689
|
+
elif align == "between":
|
1690
|
+
# For between, collect left edges for processing later
|
1691
|
+
guides_coords.append(element.x0)
|
1574
1692
|
else: # horizontal
|
1575
|
-
|
1693
|
+
if align == "left": # top for horizontal
|
1694
|
+
guides_coords.append(element.top)
|
1695
|
+
elif align == "right": # bottom for horizontal
|
1696
|
+
guides_coords.append(element.bottom)
|
1697
|
+
elif align == "center":
|
1698
|
+
guides_coords.append((element.top + element.bottom) / 2)
|
1699
|
+
elif align == "between":
|
1700
|
+
# For between, collect top edges for processing later
|
1701
|
+
guides_coords.append(element.top)
|
1702
|
+
|
1703
|
+
# Handle 'between' alignment - find midpoints between adjacent markers
|
1704
|
+
if align == "between" and len(guides_coords) >= 2:
|
1705
|
+
# We need to get the right and left edges of each marker
|
1706
|
+
marker_bounds = []
|
1707
|
+
|
1708
|
+
if elements_to_process:
|
1709
|
+
# Use elements directly
|
1710
|
+
for element in elements_to_process:
|
1711
|
+
if axis == "vertical":
|
1712
|
+
marker_bounds.append((element.x0, element.x1))
|
1713
|
+
else: # horizontal
|
1714
|
+
marker_bounds.append((element.top, element.bottom))
|
1715
|
+
else:
|
1716
|
+
# Fall back to text search
|
1717
|
+
if "marker_texts" not in locals():
|
1718
|
+
marker_texts = _normalize_markers(markers, obj)
|
1719
|
+
for marker in marker_texts:
|
1720
|
+
if hasattr(obj, "find"):
|
1721
|
+
element = obj.find(
|
1722
|
+
f'text:contains("{marker}")', apply_exclusions=apply_exclusions
|
1723
|
+
)
|
1724
|
+
if element:
|
1725
|
+
if axis == "vertical":
|
1726
|
+
marker_bounds.append((element.x0, element.x1))
|
1727
|
+
else: # horizontal
|
1728
|
+
marker_bounds.append((element.top, element.bottom))
|
1576
1729
|
|
1577
1730
|
# Sort markers by their left edge (or top edge for horizontal)
|
1578
1731
|
marker_bounds.sort(key=lambda x: x[0])
|
@@ -4246,12 +4399,26 @@ class _ColumnAccessor:
|
|
4246
4399
|
"""Return number of columns (vertical guides - 1)."""
|
4247
4400
|
return max(0, len(self._guides.vertical) - 1)
|
4248
4401
|
|
4249
|
-
def __getitem__(self, index: int) -> "Region":
|
4250
|
-
"""Get column at the specified index."""
|
4251
|
-
|
4252
|
-
|
4253
|
-
|
4254
|
-
|
4402
|
+
def __getitem__(self, index: Union[int, slice]) -> Union["Region", "ElementCollection"]:
|
4403
|
+
"""Get column at the specified index or slice."""
|
4404
|
+
from natural_pdf.elements.element_collection import ElementCollection
|
4405
|
+
|
4406
|
+
if isinstance(index, slice):
|
4407
|
+
# Handle slice notation - return multiple columns
|
4408
|
+
columns = []
|
4409
|
+
num_cols = len(self)
|
4410
|
+
|
4411
|
+
# Convert slice to range of indices
|
4412
|
+
start, stop, step = index.indices(num_cols)
|
4413
|
+
for i in range(start, stop, step):
|
4414
|
+
columns.append(self._guides.column(i))
|
4415
|
+
|
4416
|
+
return ElementCollection(columns)
|
4417
|
+
else:
|
4418
|
+
# Handle negative indexing
|
4419
|
+
if index < 0:
|
4420
|
+
index = len(self) + index
|
4421
|
+
return self._guides.column(index)
|
4255
4422
|
|
4256
4423
|
|
4257
4424
|
class _RowAccessor:
|
@@ -4264,12 +4431,26 @@ class _RowAccessor:
|
|
4264
4431
|
"""Return number of rows (horizontal guides - 1)."""
|
4265
4432
|
return max(0, len(self._guides.horizontal) - 1)
|
4266
4433
|
|
4267
|
-
def __getitem__(self, index: int) -> "Region":
|
4268
|
-
"""Get row at the specified index."""
|
4269
|
-
|
4270
|
-
|
4271
|
-
|
4272
|
-
|
4434
|
+
def __getitem__(self, index: Union[int, slice]) -> Union["Region", "ElementCollection"]:
|
4435
|
+
"""Get row at the specified index or slice."""
|
4436
|
+
from natural_pdf.elements.element_collection import ElementCollection
|
4437
|
+
|
4438
|
+
if isinstance(index, slice):
|
4439
|
+
# Handle slice notation - return multiple rows
|
4440
|
+
rows = []
|
4441
|
+
num_rows = len(self)
|
4442
|
+
|
4443
|
+
# Convert slice to range of indices
|
4444
|
+
start, stop, step = index.indices(num_rows)
|
4445
|
+
for i in range(start, stop, step):
|
4446
|
+
rows.append(self._guides.row(i))
|
4447
|
+
|
4448
|
+
return ElementCollection(rows)
|
4449
|
+
else:
|
4450
|
+
# Handle negative indexing
|
4451
|
+
if index < 0:
|
4452
|
+
index = len(self) + index
|
4453
|
+
return self._guides.row(index)
|
4273
4454
|
|
4274
4455
|
|
4275
4456
|
class _CellAccessor:
|
@@ -4278,33 +4459,82 @@ class _CellAccessor:
|
|
4278
4459
|
def __init__(self, guides: "Guides"):
|
4279
4460
|
self._guides = guides
|
4280
4461
|
|
4281
|
-
def __getitem__(self, key) -> Union["Region", "_CellRowAccessor"]:
|
4462
|
+
def __getitem__(self, key) -> Union["Region", "_CellRowAccessor", "ElementCollection"]:
|
4282
4463
|
"""
|
4283
4464
|
Get cell(s) at the specified position.
|
4284
4465
|
|
4285
4466
|
Supports:
|
4286
|
-
- guides.cells[row, col] -
|
4287
|
-
- guides.cells[row][col] - nested
|
4467
|
+
- guides.cells[row, col] - single cell
|
4468
|
+
- guides.cells[row][col] - single cell (nested)
|
4469
|
+
- guides.cells[row, :] - all cells in a row
|
4470
|
+
- guides.cells[:, col] - all cells in a column
|
4471
|
+
- guides.cells[:, :] - all cells
|
4472
|
+
- guides.cells[row][:] - all cells in a row (nested)
|
4288
4473
|
"""
|
4474
|
+
from natural_pdf.elements.element_collection import ElementCollection
|
4475
|
+
|
4289
4476
|
if isinstance(key, tuple) and len(key) == 2:
|
4290
|
-
# Direct tuple access: guides.cells[row, col]
|
4291
4477
|
row, col = key
|
4292
|
-
|
4293
|
-
|
4294
|
-
|
4295
|
-
|
4296
|
-
|
4297
|
-
|
4478
|
+
|
4479
|
+
# Handle slices for row and/or column
|
4480
|
+
if isinstance(row, slice) or isinstance(col, slice):
|
4481
|
+
cells = []
|
4482
|
+
num_rows = len(self._guides.rows)
|
4483
|
+
num_cols = len(self._guides.columns)
|
4484
|
+
|
4485
|
+
# Convert slices to ranges
|
4486
|
+
if isinstance(row, slice):
|
4487
|
+
row_indices = range(*row.indices(num_rows))
|
4488
|
+
else:
|
4489
|
+
# Single row index
|
4490
|
+
if row < 0:
|
4491
|
+
row = num_rows + row
|
4492
|
+
row_indices = [row]
|
4493
|
+
|
4494
|
+
if isinstance(col, slice):
|
4495
|
+
col_indices = range(*col.indices(num_cols))
|
4496
|
+
else:
|
4497
|
+
# Single column index
|
4498
|
+
if col < 0:
|
4499
|
+
col = num_cols + col
|
4500
|
+
col_indices = [col]
|
4501
|
+
|
4502
|
+
# Collect all cells in the specified ranges
|
4503
|
+
for r in row_indices:
|
4504
|
+
for c in col_indices:
|
4505
|
+
cells.append(self._guides.cell(r, c))
|
4506
|
+
|
4507
|
+
return ElementCollection(cells)
|
4508
|
+
else:
|
4509
|
+
# Both are integers - single cell access
|
4510
|
+
# Handle negative indexing for both row and col
|
4511
|
+
if row < 0:
|
4512
|
+
row = len(self._guides.rows) + row
|
4513
|
+
if col < 0:
|
4514
|
+
col = len(self._guides.columns) + col
|
4515
|
+
return self._guides.cell(row, col)
|
4516
|
+
elif isinstance(key, slice):
|
4517
|
+
# First level slice: guides.cells[:] - return all rows as accessors
|
4518
|
+
# For now, let's return all cells flattened
|
4519
|
+
cells = []
|
4520
|
+
num_rows = len(self._guides.rows)
|
4521
|
+
row_indices = range(*key.indices(num_rows))
|
4522
|
+
|
4523
|
+
for r in row_indices:
|
4524
|
+
for c in range(len(self._guides.columns)):
|
4525
|
+
cells.append(self._guides.cell(r, c))
|
4526
|
+
|
4527
|
+
return ElementCollection(cells)
|
4298
4528
|
elif isinstance(key, int):
|
4299
4529
|
# First level of nested access: guides.cells[row]
|
4300
4530
|
# Handle negative indexing for row
|
4301
4531
|
if key < 0:
|
4302
4532
|
key = len(self._guides.rows) + key
|
4303
|
-
# Return a row accessor that allows [col] indexing
|
4533
|
+
# Return a row accessor that allows [col] or [:] indexing
|
4304
4534
|
return _CellRowAccessor(self._guides, key)
|
4305
4535
|
else:
|
4306
4536
|
raise TypeError(
|
4307
|
-
f"Cell indices must be integers or tuple of two integers, got {type(key)}"
|
4537
|
+
f"Cell indices must be integers, slices, or tuple of two integers/slices, got {type(key)}"
|
4308
4538
|
)
|
4309
4539
|
|
4310
4540
|
|
@@ -4315,9 +4545,24 @@ class _CellRowAccessor:
|
|
4315
4545
|
self._guides = guides
|
4316
4546
|
self._row = row
|
4317
4547
|
|
4318
|
-
def __getitem__(self, col: int) -> "Region":
|
4319
|
-
"""Get cell at [row][col]."""
|
4320
|
-
|
4321
|
-
|
4322
|
-
|
4323
|
-
|
4548
|
+
def __getitem__(self, col: Union[int, slice]) -> Union["Region", "ElementCollection"]:
|
4549
|
+
"""Get cell at [row][col] or all cells in row with [row][:]."""
|
4550
|
+
from natural_pdf.elements.element_collection import ElementCollection
|
4551
|
+
|
4552
|
+
if isinstance(col, slice):
|
4553
|
+
# Handle slice notation - return all cells in this row
|
4554
|
+
cells = []
|
4555
|
+
num_cols = len(self._guides.columns)
|
4556
|
+
|
4557
|
+
# Convert slice to range of indices
|
4558
|
+
start, stop, step = col.indices(num_cols)
|
4559
|
+
for c in range(start, stop, step):
|
4560
|
+
cells.append(self._guides.cell(self._row, c))
|
4561
|
+
|
4562
|
+
return ElementCollection(cells)
|
4563
|
+
else:
|
4564
|
+
# Handle single column index
|
4565
|
+
# Handle negative indexing for column
|
4566
|
+
if col < 0:
|
4567
|
+
col = len(self._guides.columns) + col
|
4568
|
+
return self._guides.cell(self._row, col)
|
natural_pdf/core/page.py
CHANGED
@@ -815,11 +815,38 @@ class Page(
|
|
815
815
|
if debug:
|
816
816
|
print(f" ✗ Empty iterable returned from callable '{label}'")
|
817
817
|
elif region_result:
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
if
|
822
|
-
|
818
|
+
# Check if it's a single Element that can be converted to a Region
|
819
|
+
from natural_pdf.elements.base import Element
|
820
|
+
|
821
|
+
if isinstance(region_result, Element) or (
|
822
|
+
hasattr(region_result, "bbox") and hasattr(region_result, "expand")
|
823
|
+
):
|
824
|
+
try:
|
825
|
+
# Convert Element to Region using expand()
|
826
|
+
expanded_region = region_result.expand()
|
827
|
+
if isinstance(expanded_region, Region):
|
828
|
+
expanded_region.label = label
|
829
|
+
regions.append(expanded_region)
|
830
|
+
if debug:
|
831
|
+
print(
|
832
|
+
f" ✓ Converted Element to Region from callable '{label}': {expanded_region}"
|
833
|
+
)
|
834
|
+
else:
|
835
|
+
if debug:
|
836
|
+
print(
|
837
|
+
f" ✗ Element.expand() did not return a Region: {type(expanded_region)}"
|
838
|
+
)
|
839
|
+
except Exception as e:
|
840
|
+
if debug:
|
841
|
+
print(f" ✗ Failed to convert Element to Region: {e}")
|
842
|
+
else:
|
843
|
+
logger.warning(
|
844
|
+
f"Callable exclusion '{exclusion_label}' returned non-Region object: {type(region_result)}. Skipping."
|
845
|
+
)
|
846
|
+
if debug:
|
847
|
+
print(
|
848
|
+
f" ✗ Callable returned non-Region/None: {type(region_result)}"
|
849
|
+
)
|
823
850
|
else:
|
824
851
|
if debug:
|
825
852
|
print(
|
@@ -839,6 +866,27 @@ class Page(
|
|
839
866
|
if debug:
|
840
867
|
print(f" - Added direct region '{label}': {exclusion_item}")
|
841
868
|
|
869
|
+
# Process direct Element objects - convert to Region
|
870
|
+
elif hasattr(exclusion_item, "bbox") and hasattr(exclusion_item, "expand"):
|
871
|
+
try:
|
872
|
+
# Convert Element to Region using expand()
|
873
|
+
expanded_region = exclusion_item.expand()
|
874
|
+
if isinstance(expanded_region, Region):
|
875
|
+
expanded_region.label = label
|
876
|
+
regions.append(expanded_region)
|
877
|
+
if debug:
|
878
|
+
print(
|
879
|
+
f" - Converted direct Element to Region '{label}': {expanded_region}"
|
880
|
+
)
|
881
|
+
else:
|
882
|
+
if debug:
|
883
|
+
print(
|
884
|
+
f" - Element.expand() did not return a Region: {type(expanded_region)}"
|
885
|
+
)
|
886
|
+
except Exception as e:
|
887
|
+
if debug:
|
888
|
+
print(f" - Failed to convert Element to Region: {e}")
|
889
|
+
|
842
890
|
# Process string selectors (from PDF-level exclusions)
|
843
891
|
elif isinstance(exclusion_item, str):
|
844
892
|
selector_str = exclusion_item
|
@@ -1081,7 +1129,7 @@ class Page(
|
|
1081
1129
|
) # _apply_selector doesn't filter
|
1082
1130
|
|
1083
1131
|
# Filter the results based on exclusions if requested
|
1084
|
-
if apply_exclusions and
|
1132
|
+
if apply_exclusions and results_collection:
|
1085
1133
|
filtered_elements = self._filter_elements_by_exclusions(results_collection.elements)
|
1086
1134
|
# Return the first element from the filtered list
|
1087
1135
|
return filtered_elements[0] if filtered_elements else None
|
@@ -1176,7 +1224,7 @@ class Page(
|
|
1176
1224
|
) # _apply_selector doesn't filter
|
1177
1225
|
|
1178
1226
|
# Filter the results based on exclusions if requested
|
1179
|
-
if apply_exclusions and
|
1227
|
+
if apply_exclusions and results_collection:
|
1180
1228
|
filtered_elements = self._filter_elements_by_exclusions(results_collection.elements)
|
1181
1229
|
return ElementCollection(filtered_elements)
|
1182
1230
|
else:
|
@@ -1548,7 +1596,7 @@ class Page(
|
|
1548
1596
|
all_elements = self._element_mgr.get_all_elements()
|
1549
1597
|
|
1550
1598
|
# Apply exclusions if requested
|
1551
|
-
if apply_exclusions
|
1599
|
+
if apply_exclusions:
|
1552
1600
|
return self._filter_elements_by_exclusions(
|
1553
1601
|
all_elements, debug_exclusions=debug_exclusions
|
1554
1602
|
)
|
natural_pdf/elements/region.py
CHANGED
@@ -1270,7 +1270,8 @@ class Region(
|
|
1270
1270
|
# 3. Get Relevant Exclusions (overlapping this region)
|
1271
1271
|
apply_exclusions_flag = kwargs.get("apply_exclusions", apply_exclusions)
|
1272
1272
|
exclusion_regions = []
|
1273
|
-
if apply_exclusions_flag
|
1273
|
+
if apply_exclusions_flag:
|
1274
|
+
# Always call _get_exclusion_regions to get both page and PDF level exclusions
|
1274
1275
|
all_page_exclusions = self._page._get_exclusion_regions(
|
1275
1276
|
include_callable=True, debug=debug
|
1276
1277
|
)
|
@@ -1281,10 +1282,11 @@ class Region(
|
|
1281
1282
|
exclusion_regions = overlapping_exclusions
|
1282
1283
|
if debug:
|
1283
1284
|
logger.debug(
|
1284
|
-
f"Region {self.bbox}:
|
1285
|
+
f"Region {self.bbox}: Found {len(all_page_exclusions)} total exclusions, "
|
1286
|
+
f"{len(exclusion_regions)} overlapping this region."
|
1285
1287
|
)
|
1286
1288
|
elif debug:
|
1287
|
-
logger.debug(f"Region {self.bbox}: Not applying exclusions.")
|
1289
|
+
logger.debug(f"Region {self.bbox}: Not applying exclusions (apply_exclusions=False).")
|
1288
1290
|
|
1289
1291
|
# 4. Spatially Filter Characters using Utility
|
1290
1292
|
# Pass self as the target_region for precise polygon checks etc.
|