Skip to content

Selector Reference

Every pseudo-class and attribute operator registered in the selector engine. Generated from natural_pdf/selectors/registry.py, natural_pdf/selectors/_clauses.py, and the parser dispatch tables in natural_pdf/selectors/parser.py.

Applied with a colon, e.g. text:bold. These filter individual elements.

NameAliasesWhat it matchesExample
:boldText drawn with a bold font variant.text:bold
:checkedCheckbox-like element whose is_checked is true.checkbox:checked
:containsElement text contains the given string. Case-sensitive by default; pass case=False to find/find_all for case-insensitive, regex=True to treat the argument as a regular expression.text:contains("Invoice")
:emptyElement has no text, or only whitespace.text:empty
:endswith:ends-withElement text ends with the given string.text:endswith("%")
:first-childElement is the first child of its parent.text:first-child
:highlight:highlightedText covered by a highlight annotation.text:highlight
:horizontalElement reports is_horizontal (mainly lines).line:horizontal
:italicText drawn with an italic font variant.text:italic
:last-childElement is the last child of its parent.text:last-child
:not-boldText that is not bold.text:not-bold
:not-emptyElement has non-whitespace text.text:not-empty
:not-italicText that is not italic.text:not-italic
:regexElement text matches the regular expression (via re.search). case=False adds re.IGNORECASE.text:regex("\d{4}-\d{2}")
:startswith:starts-withElement text starts with the given string.text:startswith("Total")
:strike:strikethrough, :strikeoutText with strikethrough decoration.text:strike
:uncheckedCheckbox-like element whose is_checked is false.checkbox:unchecked
:underline:underlinedText with underline decoration.text:underline
:verticalElement reports is_vertical (mainly lines).line:vertical

Applied after element filtering; they operate on the whole result list (in document order).

NameAliasesWhat it doesExample
:firstKeep only the first matched element.text:bold:first
:lastKeep only the last matched element.text:bold:last
:limitKeep at most the first N matches.text:limit(10)
:nthKeep the element at the given 0-based index; negative indices count from the end.text:nth(2)
:sliceSlice the matches with Python slice semantics: :slice(stop), :slice(start, stop), or :slice(start, stop, step).text:slice(0, 5)

Take a reference selector as an argument and keep elements positioned relative to its first match.

NameAliasesWhat it doesExample
:aboveKeep elements entirely above the first element matched by the reference selector.text:above(line:horizontal)
:belowKeep elements entirely below the first element matched by the reference selector.text:below(text:contains("Header"))
:left-ofKeep elements entirely left of the first element matched by the reference selector.text:left-of(rect)
:nearKeep elements whose center is within near_threshold points (default 50) of the reference element’s center.text:near(text:contains("Total"))
:right-ofKeep elements entirely right of the first element matched by the reference selector.text:right-of(rect)

Handled by the selector parser/executor itself rather than the clause registry.

NameAliasesWhat it doesExample
:closestRank matches by text similarity to the argument; @threshold sets the minimum similarity (0-1). Applied during selector execution.text:closest(Invoice Number@0.7)
:notInvert an inner selector.text:not(:bold)
:ocrOCR-tolerant text match that forgives commonly-confused characters (handled during selector execution).text:ocr("Invoice")

Applied in brackets, e.g. text[size>12]. Attribute names use CSS-style hyphens or Python-style underscores interchangeably.

OperatorAliasesMeaningExample
(bare)Attribute exists and is not None.text[fontname]
!=Not equal.text[fontname!=Courier]
$=String ends with the value.text[fontname$=Bold]
*=String contains the value (case-insensitive for fontname).text[fontname*=narrow]
<Numeric less-than.text[width<100]
<=Numeric less-than-or-equal.text[size<=8]
=Equal. Colors compare with a small perceptual tolerance; boolean attributes accept true/false/1/0/yes.text[size=12]
>Numeric greater-than.text[size>12]
>=Numeric greater-than-or-equal.text[size>=14]
^=String starts with the value.text[fontname^=Arial]
~=Approximately equal — colors match within a small perceptual distance ([color~=red] accepts near-reds). Numeric values are rejected at parse time as ambiguous; use an explicit range like [size>=11][size<=13].text[color~=red]

Third-party packages can add clauses with natural_pdf.selectors.register_pseudo, register_attribute, register_post_pseudo, and register_relational_pseudo.