Skip to content

table_to_csv

Convert a table object to a CSV string.

引数:

名前 タイプ デスクリプション デフォルト
table TableStructureRecognizerSchema

The table object to convert. It should have a cells attribute

必須
padding bool

If True, pad the span cells with the same contents. Defaults to False.

False
drop_empty bool

If True, drop empty rows and columns. Defaults to True.

True

戻り値:

名前 タイプ デスクリプション
str str

The CSV string representation of the table.

ソースコード位置: src/yomitoku/utils/misc.py
def table_to_csv(table, padding=False, drop_empty=True) -> str:
    """
    Convert a table object to a CSV string.

    Args:
        table (TableStructureRecognizerSchema): The table object to convert. It should have a `cells` attribute
        padding (bool, optional): If True, pad the span cells with the same contents. Defaults to False.
        drop_empty (bool, optional): If True, drop empty rows and columns. Defaults to True.

    Returns:
        str: The CSV string representation of the table.
    """

    table_array = convert_table_array(table, padding=padding, drop_empty=drop_empty)
    csv_lines = []

    for row in table_array:
        csv_lines.append(",".join(f'"{cell}"' for cell in row))

    return "\n".join(csv_lines)