API Documentation

pytextable.tostring(data: Sequence[Sequence[Any]], *, header: Sequence[str] = None, table: bool = True, centering: bool = True, caption: str = '', label: str = '', alignment: str = '', fmt: str = '', indentation: int = 4, booktabs: bool = True, midrule_condition: Callable[[Sequence[Any], Sequence[Any]], bool] = <function _no_extra_midrule>) → str

Convert python data to a valid tex table string.

This is the heart of pytextable and does all the heavy lifting. You must pass the data argument which containts the rows and columns of the table to create. The other keyword-only arguments allow additional formatting and customization.

Parameters:
  • data – Sequence of sequences containing the rows and columns of the table. Note that the number of columns in each row must match.
  • header – Column header to add as sequence of strings. The number of elements must match the number of columns of your data.
  • table – Add a surrounding table environment to the latex tabular.
  • centering – Add a centering statement to the table. This is only valid if table=True.
  • caption – Add this caption to the table. This is only valid if table=True.
  • label – Add this label to the table. This is only valid if table=True.
  • alignment

    String converted to the full table alignment. Examples:

    >>> _table_alignment("", 3)  # The default
    "ccc"
    
    >>> _table_alignment("l", 3)  # Left-align everything instead
    "lll"
    
    >>> _table_alignment("l|", 3)  # Left-align and add separators in the table
    "l|l|l"
    
    >>> _table_alignment("|l|", 3)  # Left-align and add separators everywhere
    "|l|l|l|"
    
    >>> _table_alignment("llc", 3)  # Valid-formatter is just accepted
    "llc"
    
    >>> _table_alignment("|ll|l|", 3)  # Separators are fine as-well
    "|ll|l|"
    
  • fmt – Format string to apply to every element in the table data. Example: ‘.3f’.
  • indentation – Number of spaces used for environment indentation.
  • booktabs – Use the booktabs module to neatly format the table.
  • midrule_condition

    Callback to check for additional inserted midrules. This function is called with the current and previous row and should return a boolean. If it returns True, a \midrule is applied before the current row. Example:

    >>> def second_elem_changed(row, last_row):
        return row[1] != last_row[1]
    

    This is useful to separate the current row from the previous one in case something changed. Only valid with booktabs=True.

Returns:

The latex table as formatted string.

pytextable.write(data: Sequence[Sequence[Any]], outfile: str, *, writemode: str = 'w', encoding: str = 'utf-8', **kwargs) → None

Write python data to file as formatted latex table.

Calls tostring() to convert the data to a valid tex table passing any additional keyword-arguments on. The retrieved string is then written to the file passed.

Parameters:
  • data – Sequence of sequences containing the rows and columns of the table. Note that the number of columns in each row must match.
  • outfile – File to write the data to.
  • writemode – Writemode to use when opening the file. Note that the mode must support writing, passing r will fail horribly for obvious reasons. This argument exists to give the option to append to a file with a.
  • encoding – Encoding to use when opening the file. The default of utf-8 works well with latex code using \usepackage[utf8]{inputenc}.
  • kwargs – Keyword arguments for additional formatting passed to tostring().