5.1.4.2. Base writer classes of text formats

class pytablewriter.writer.text._text_writer.TextTableWriter(**kwargs: Any)[source]

Bases: AbstractTableWriter, TextWriterInterface

A base class for table writer with text formats.

table_char

Character attributes that compose a table

column_delimiter: str

A column delimiter of a table.

char_left_side_row: str

A character of a left side of a row.

char_right_side_row: str

A character of a right side of a row.

char_cross_point: str

A character of the crossing point of column delimiter and row delimiter.

char_opening_row: str

A character of the first line of a table.

char_header_row_separator: str

A character of a separator line of the header and the body of the table.

char_value_row_separator: str

A character of a row separator line of the table.

char_closing_row: str

A character of the last line of a table.

is_write_header_separator_row: bool

Write a header separator line of the table if the value is True.

is_write_value_separator_row: bool

Write row separator line(s) of the table if the value is True.

is_write_opening_row: bool

Write an opening line of the table if the value is True.

is_write_closing_row: bool

Write a closing line of the table if the value is True.

is_write_null_line_after_table: bool

Write a blank line of after writing a table if the value is True.

margin: int

Margin size for each cells

add_col_separator_style_filter(style_filter: ColSeparatorStyleFilterFunc) None[source]

Add a style filter function for columns to the writer.

Parameters:

style_filter

A function that called for each cell in the table to apply a style to table cells. The function will be required to implement the following Protocol:

class ColSeparatorStyleFilterFunc(Protocol):
    def __call__(
        self, left_cell: Optional[Cell], right_cell: Optional[Cell], **kwargs: Any
    ) -> Optional[Style]:
        ...

If more than one style filter function is added to the writer, it will be called from the last one added. These style functions should return None when not needed to apply styles. If all of the style functions returned None, default_style will be applied.

You can pass keyword arguments to style filter functions via style_filter_kwargs. In default, the attribute includes:

  • writer: the writer instance that the caller of a style_filter function

clear_theme() None[source]

Remove all of the style filters.

dump(output: str | IO, close_after_write: bool = True, **kwargs: Any) None[source]

Write data to the output with tabular format.

During the executing this method, enable_ansi_escape attribute will be temporarily set to False.

Parameters:
  • output – The value must either an output stream or a path to an output file.

  • close_after_write – Close the output after write. Defaults to True.

dumps(**kwargs: Any) str[source]

Get rendered tabular text from the table data.

Only available for text format table writers.

Parameters:

**kwargs – Optional arguments that the writer takes.

Returns:

Rendered tabular text.

Return type:

str

write_null_line() None[source]

Write a null line to the stream.

write_table(**kwargs: Any) None[source]

Write a table to the stream.

Note

  • None values are written as an empty string.

class pytablewriter.writer.text._text_writer.IndentationTextTableWriter(**kwargs: Any)[source]

Bases: TextTableWriter, IndentationInterface

A base class for table writer with indentation text formats.

Parameters:

indent_level (int) – Indentation level. Defaults to 0.

indent_string

Indentation string for each level.

dec_indent_level() None[source]

Decrement the indentation level.

inc_indent_level() None[source]

Increment the indentation level.

set_indent_level(indent_level: int) None[source]

Set the indentation level.

Parameters:

indent_level (int) – New indentation level.

write_table(**kwargs: Any) None[source]

Write a table to the stream.

Parameters:

indent (Optional[int]) – Indent level of an output. Interpretation of indent level value differ format to format. Some writer classes may ignore this value.

Note

  • None values are written as an empty string.