all2md.renderers.csv
CSV rendering from AST.
This module provides the CsvRenderer class which converts AST table nodes to CSV (Comma-Separated Values) format. The renderer supports: - Table selection by index or heading proximity - Multi-table handling (first, all, or error) - Merged cell handling - Configurable CSV dialects - Excel compatibility (BOM support)
The rendering process extracts table data from the AST and writes it to CSV using Python’s csv module with full control over formatting options.
- class all2md.renderers.csv.CsvRenderer
Bases:
BaseRendererRender AST table nodes to CSV format.
This class extracts table data from an AST document and renders it to CSV format with configurable options for table selection, merged cell handling, and CSV dialect settings.
- Parameters:
options (CsvRendererOptions or None, default = None) – CSV rendering options
Examples
Basic usage (export first table):
>>> from all2md.ast import Document, Table, TableRow, TableCell, Text >>> from all2md.renderers.csv import CsvRenderer >>> from all2md.options.csv import CsvRendererOptions >>> table = Table( ... header=TableRow(cells=[ ... TableCell(content=[Text(content="Name")]), ... TableCell(content=[Text(content="Age")]) ... ]), ... rows=[ ... TableRow(cells=[ ... TableCell(content=[Text(content="Alice")]), ... TableCell(content=[Text(content="30")]) ... ]) ... ] ... ) >>> doc = Document(children=[table]) >>> renderer = CsvRenderer() >>> csv_text = renderer.render_to_string(doc) >>> print(csv_text) Name,Age Alice,30
Extract table by heading:
>>> options = CsvRendererOptions(table_heading="Results") >>> renderer = CsvRenderer(options)
Export all tables:
>>> options = CsvRendererOptions( ... multi_table_mode="all", ... include_table_headings=True ... ) >>> renderer = CsvRenderer(options)
Initialize the CSV renderer with options.
- __init__(options: CsvRendererOptions | None = None)
Initialize the CSV renderer with options.
- render(doc: Document, output: str | Path | IO[bytes] | IO[str]) None
Render AST document to CSV file.
- Parameters:
doc (Document) – AST Document node to render
output (str, Path, IO[bytes], or IO[str]) – Output destination
- Raises:
RenderingError – If no tables found or rendering fails
- render_to_string(doc: Document) str
Render AST document to CSV string.
- Parameters:
doc (Document) – AST Document node to render
- Returns:
Rendered CSV content
- Return type:
str
- Raises:
RenderingError – If no tables found or rendering fails
- visit_comment(node: Comment) None
Render a Comment node (strip in CSV).
- Parameters:
node (Comment) – Comment to render
- visit_comment_inline(node: CommentInline) None
Render a CommentInline node (strip in CSV).
- Parameters:
node (CommentInline) – Inline comment to render