A comprehensive Python document processing library that renders structured documents to PDF with advanced typography and customizable styling
—
Comprehensive styling framework providing CSS-like functionality with selectors, inheritance, attribute validation, and stylesheet management. The styling system enables fine-grained control over document appearance while maintaining consistency and maintainability.
Foundation classes for the styling system including Style objects, styled elements, and attribute management.
class Style(AttributesDictionary):
"""
Style definition with validated attributes and inheritance.
Parameters:
- base: Style, optional base style to inherit from
- **attributes: style attribute key-value pairs
"""
def __init__(self, base=None, **attributes): ...
def copy(self): ... # Create deep copy
class Styled(DocumentElement):
"""
Base class for elements that can be styled.
Attributes:
- style: Style object or None for element styling
"""
style = Attribute(Style, None)
def get_style(self, attribute, flowable_target=None): ...
def get_config_value(self, attribute, document=None): ...
class StyledMeta(type):
"""Metaclass for styled elements providing style attribute registration."""StyleSheet system for organizing and applying styles across documents with selector-based targeting.
class StyleSheet(dict):
"""
Collection of styles with selectors for element targeting.
Parameters:
- name: str, stylesheet name
- base: StyleSheet, optional base stylesheet to inherit from
- matcher: StyledMatcher, custom matching system
"""
def __init__(self, name, base=None, matcher=None): ...
def __getitem__(self, styled_class): ... # Get style for element type
def __setitem__(self, selector, style): ... # Set style for selector
def match(self, styled, container): ... # Find matching style
class StyleSheetFile(StyleSheet):
"""
StyleSheet loaded from configuration file.
Parameters:
- file: str or file-like, path to or open file containing stylesheet
"""
def __init__(self, file): ...CSS-like selectors for targeting specific elements and contexts within documents.
class ClassSelector:
"""
Select elements by their class/type.
Parameters:
- styled_class: class to select
- style: Style to apply to matching elements
"""
def __init__(self, styled_class, style): ...
def match(self, styled, container): ... # Check if element matches
class ContextSelector:
"""
Select elements based on their context/ancestors.
Parameters:
- *selectors: sequence of parent selectors
- style: Style to apply to matching elements
"""
def __init__(self, *selectors, style): ...
# Predefined constants
PARENT_STYLE = ... # Reference to parent element's styleAdvanced style matching system for resolving styles based on element context and selectors.
class StyledMatcher:
"""
System for matching elements to styles based on selectors.
Parameters:
- stylesheet: StyleSheet containing style rules
"""
def __init__(self, stylesheet): ...
def match(self, styled, container): ... # Find best matching style
def get_matches(self, styled, container): ... # Get all matching stylesType-safe attribute system with validation, defaults, and inheritance support.
class AttributeType:
"""
Base class for style attribute types with validation.
Parameters:
- default_value: default value for this attribute type
"""
def __init__(self, default_value=None): ...
def __call__(self, value): ... # Validate and convert value
def __repr__(self): ...
class AcceptNoneAttributeType(AttributeType):
"""AttributeType that accepts None as a valid value."""
class Attribute:
"""
Descriptor for validated style attributes.
Parameters:
- attribute_type: AttributeType for validation
- default_value: default value (overrides type default)
- accepted_type: type or tuple of types for isinstance checking
"""
def __init__(self, attribute_type, default_value=None, accepted_type=None): ...
def __get__(self, instance, owner): ...
def __set__(self, instance, value): ...
class OverrideDefault:
"""Override default value in style inheritance."""
def __init__(self, value): ...Predefined attribute types for common styling properties with validation and type conversion.
class Bool(AttributeType):
"""
Boolean attribute type accepting 'true'/'false' strings.
"""
def __call__(self, value): ... # Convert to boolean
class Integer(AttributeType):
"""
Integer attribute type with validation.
Parameters:
- min_value: int, minimum allowed value
- max_value: int, maximum allowed value
"""
def __init__(self, min_value=None, max_value=None): ...
class OptionSet(AttributeType):
"""
Enumeration-like attribute type with predefined valid values.
Parameters:
- *values: allowed string values
"""
def __init__(self, *values): ...
class OptionSetMeta(type):
"""Metaclass for creating OptionSet classes."""Rule-based configuration system for complex styling scenarios and document settings.
class Configurable:
"""
Base class for objects with configurable attributes.
Parameters:
- **configuration: configuration key-value pairs
"""
def __init__(self, **configuration): ...
def get_config_value(self, attribute, document=None): ...
class RuleSet(list):
"""
Collection of configuration rules with matching conditions.
Parameters:
- name: str, ruleset name
- base: RuleSet, optional base ruleset to inherit from
"""
def __init__(self, name, base=None): ...
def __getitem__(self, descriptor): ... # Get value for descriptor
def __setitem__(self, descriptor, value): ... # Set rule value
class RuleSetFile(RuleSet):
"""
RuleSet loaded from configuration file.
Parameters:
- file: str or file-like, path to or open file containing rules
"""
def __init__(self, file): ...
class Var:
"""
Variable reference in configuration files.
Parameters:
- name: str, variable name to reference
"""
def __init__(self, name): ...Specialized dictionary classes for attribute management with validation and inheritance.
class AttributesDictionary(dict):
"""
Dictionary with attribute validation and inheritance support.
Parameters:
- base_attributes: dict, base attributes to inherit from
- **attributes: additional attributes
"""
def __init__(self, base_attributes=None, **attributes): ...
def __getitem__(self, name): ... # Get attribute value
def __setitem__(self, name, value): ... # Set validated attribute
def copy(self): ... # Create deep copy
def update(self, other): ... # Update with validationfrom rinohtype.style import Style, StyleSheet
from rinohtype.color import Color
from rinohtype.dimension import PT
from rinohtype.paragraph import Paragraph, ParagraphStyle
# Create individual styles
heading_style = Style(
font_size=16*PT,
font_weight='bold',
text_color=Color(0, 0, 0.5),
space_above=12*PT,
space_below=6*PT
)
body_style = Style(
font_size=11*PT,
line_spacing=1.2,
text_align='justify',
space_below=6*PT
)
# Apply styles to elements
heading = Paragraph("Chapter Title", style=heading_style)
content = Paragraph("This is the body text...", style=body_style)from rinohtype.style import StyleSheet, ClassSelector
from rinohtype.paragraph import ParagraphStyle
from rinohtype.structure import Heading
# Create stylesheet with selectors
stylesheet = StyleSheet('custom')
# Define styles for different element types
stylesheet[Paragraph] = ParagraphStyle(
font_family='serif',
font_size=11*PT,
line_spacing=1.2
)
stylesheet[Heading] = ParagraphStyle(
base=stylesheet[Paragraph], # Inherit from paragraph
font_size=16*PT,
font_weight='bold',
space_above=12*PT
)
# Context-sensitive styling
from rinohtype.style import ContextSelector
from rinohtype.structure import ListItem
stylesheet[ContextSelector(List, Paragraph)] = ParagraphStyle(
base=stylesheet[Paragraph],
space_below=3*PT # Less space in lists
)from rinohtype.attribute import AttributeType, OptionSet
class FontWeight(OptionSet):
"""Font weight attribute type."""
values = ('normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900')
class TextAlign(OptionSet):
"""Text alignment attribute type."""
values = ('left', 'center', 'right', 'justify')
# Use in style definitions
custom_style = Style(
font_weight=FontWeight('bold'),
text_align=TextAlign('center')
)from rinohtype.attribute import RuleSet, Var
# Create configuration rules
config = RuleSet('document_config')
# Set variables
config['title_font_size'] = 18*PT
config['body_font_size'] = 11*PT
# Use variables in styles
title_style = Style(
font_size=Var('title_font_size'),
font_weight='bold'
)
# Conditional rules based on document properties
config[('paper_size', 'A4')] = {
'margin_left': 25*MM,
'margin_right': 25*MM
}
config[('paper_size', 'LETTER')] = {
'margin_left': 1*INCH,
'margin_right': 1*INCH
}from rinohtype.attribute import ParseError
try:
# Invalid style attribute
invalid_style = Style(font_size='invalid')
except ParseError as e:
print(f"Style parsing error: {e}")
try:
# Load stylesheet from file
stylesheet = StyleSheetFile('styles.conf')
except (FileNotFoundError, ParseError) as e:
print(f"Stylesheet loading error: {e}")Install with Tessl CLI
npx tessl i tessl/pypi-rinohtype