5.1.4.1. Abstract classes

class pytablewriter.writer._interface.TableWriterInterface[source]

Interface class for writing a table.

abstract property format_name

Format name for the writer.

Returns

str

abstract property support_split_write

Represents the writer class supported iterative table writing (write_table_iter method).

Returns

True if the writer supported iterative table writing.

Return type

bool

abstract write_table(**kwargs) → None[source]

Write a table to the stream.

write_table_iter(**kwargs) → None[source]

Write a table with iteration. “Iteration” means that divide the table writing into multiple processes. This method is useful, especially for large data. The following are premises to execute this method:

  • set iterator to the value_matrix

  • set the number of iterations to the iteration_length attribute

Call back function (Optional): Callback function is called when for each of the iteration of writing a table is completed. To set call back function, set a callback function to the write_callback attribute.

Raises

pytablewriter.NotSupportedError – If the class does not support this method.

Note

Following classes do not support this method: HtmlTableWriter, RstGridTableWriter, RstSimpleTableWriter. support_split_write attribute return True if the class is supporting this method.

class pytablewriter.AbstractTableWriter[source]

Bases: pytablewriter.writer._interface.TableWriterInterface

An abstract base class of table writer classes.

stream

Stream to write tables. You can use arbitrary stream which supported write method such as sys.stdout, file stream, StringIO, and so forth. Defaults to sys.stdout.

Example

Configure Output Stream

is_write_header: bool

Write headers of a table if the value is True.

is_padding: bool

Padding for each item in the table if the value is True.

iteration_length: int

The number of iterations to write a table. This value used in write_table_iter() method. (defaults to -1 which means number of iterations is indefinite)

style_filter_kwargs: Dict[str, Any]

Extra keyword arguments for style filter functions. These arguments will passing to filter functions added by add_style_filter() or add_col_separator_style_filter()

colorize_terminal: bool = True

[Only for text format writers] [experimental] If True, colorize text outputs with Style.

enable_ansi_escape: bool = True

[Only for text format writers] If True, applies ANSI escape sequences to terminal’s text outputs with Style.

write_callback

The value expected to a function. The function called when for each of the iteration of writing a table completed. (defaults to None) Example, callback function definition is as follows:

def callback_example(iter_count, iter_length):
    print("{:d}/{:d}".format(iter_count, iter_length))

Arguments that passed to the callback is:

  • first argument: current iteration number (start from 1)

  • second argument: a total number of iteration

add_style_filter(style_filter: pytablewriter.writer._table_writer.StyleFilterFunc) → None[source]

Add a style filter function.

Parameters

style_filter

A function for filtering table cells, the function required to implement the following Protocol:

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

close() → None[source]

Close the current stream.

property column_styles

Output Style for each column.

Returns

Return type

list of Style

property default_style

Default Style for each cell.

from_csv(csv_source: str, delimiter: str = ',') → None[source]

Set tabular attributes to the writer from a character-separated values (CSV) data source. Following attributes are set to the writer by the method:

table_name also be set if the CSV data source is a file. In that case, table_name is as same as the filename.

Parameters

csv_source (str) – Input CSV data source either can be designated CSV text or CSV file path.

Examples

Using CSV as tabular data source

Dependency Packages
from_dataframe(dataframe, add_index_column: bool = False) → None[source]

Set tabular attributes to the writer from pandas.DataFrame. Following attributes are set by the method:

Parameters
  • dataframe (pandas.DataFrame or str) – Input pandas.DataFrame object or pickle.

  • add_index_column (bool, optional) – If True, add a column of index of the dataframe. Defaults to False.

Example

Using pandas DataFrame as tabular data source

from_series(series, add_index_column: bool = True) → None[source]

Set tabular attributes to the writer from pandas.Series. Following attributes are set by the method:

Parameters
  • series (pandas.Series) – Input pandas.Series object.

  • add_index_column (bool, optional) – If True, add a column of index of the series. Defaults to True.

from_tabledata(value: tabledata._core.TableData, is_overwrite_table_name: bool = True) → None[source]

Set tabular attributes to the writer from TableData. Following attributes are configured:

TableData can be created from various data formats by pytablereader. More detailed information can be found in https://pytablereader.rtfd.io/en/latest/

Parameters

value (tabledata.TableData) – Input table data.

from_tablib(tablib_dataset) → None[source]

Set tabular attributes to the writer from tablib.Dataset.

from_writer(writer: pytablewriter.writer._table_writer.AbstractTableWriter, is_overwrite_table_name: bool = True) → None[source]

Set tabular attributes to the writer from an another table writer class incetance.

property headers

List of table header to write.

set_style(column: Union[str, int], style: pytablewriter.style._style.Style) → None[source]

Set Style for a specific column.

Parameters
  • column (int or str) – Column specifier. column index or header name correlated with the column.

  • style (Style) – Style value to be set to the column.

Raises

ValueError – If the column specifier is invalid.

property table_name

Name of the table.

property tabledata

Get tabular data of the writer.

Returns

Return type

tabledata.TableData

property type_hints

Type hints for each column of the tabular data. Writers convert data for each column using the type hints information before writing tables when you call write_xxx methods.

Acceptable values are as follows:

  • None (automatically detect column type from values in the column)

  • pytablewriter.typehint.Bool

  • pytablewriter.typehint.DateTime

  • pytablewriter.typehint.Dictionary

  • pytablewriter.typehint.Infinity

  • pytablewriter.typehint.Integer

  • pytablewriter.typehint.IpAddress

  • pytablewriter.typehint.List

  • pytablewriter.typehint.Nan

  • pytablewriter.typehint.NoneType

  • pytablewriter.typehint.NullString

  • pytablewriter.typehint.RealNumber

  • pytablewriter.typehint.String

If a type-hint value is not None, the writer tries to convert data for each data in a column to type-hint class. If the type-hint value is None or failed to convert data, the writer automatically detect column data type from the column data.

If type_hints is None, the writer detects data types for all of the columns automatically and writes a table by using detected column types.

Defaults to None.

Examples
property value_matrix

Tabular data to write.

write_table(**kwargs) → None[source]

Write a table to the stream.