Extract and customize table data
mainTables are identified as layout spans with the label "table" and are accessible via the Doc._.tables shortcut. Each table span provides a pandas.DataFrame of its contents through the Span._.data attribute.
By default, the text in doc.text for a table is a placeholder "TABLE". You can customize this by providing a display_table callback to spaCyLayout. This callback receives the pandas.DataFrame and should return a string representation of the table.
import pandas as pd
# Customizing table rendering in doc.text
def display_table(df: pd.DataFrame) -> str:
return f"Table with columns: {', '.join(df.columns.tolist())}"
layout = spaCyLayout(nlp, display_table=display_table)
doc = layout("./starcraft.pdf")
# Accessing table data
for table in doc._.tables:
# Token position and bounding box
print(table.start, table.end, table._.layout)
# pandas.DataFrame of contents
print(table._.data)