> ## Documentation Index
> Fetch the complete documentation index at: https://parserport.outerport.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Tables to CSV

> Extract table data from documents and format as CSV for further processing.

## Load Document

```python theme={null}
from parseport.document.pdf_document import PDFDocument


from PIL import Image
import asyncio
from parseport.layout_parser.simple_layout_parser import SimpleLayoutParser
from parseport.tools.layout_detector.paddle_layout_detector import PaddleLayoutDetector
from parseport.tools.layout_detector.yolo_layout_detector import YoloLayoutDetector
from parseport.tools.vlm_generator.openai_vlm_generator import OpenAIVLMGenerator
from parseport.content_reader.vlm_reader import VLMDocumentReader
from parseport.formatter.vlm_formatter import VLMFormatter
from parseport.struct import ComponentType
```

Load your document containing tables.

```python theme={null}
doc = PDFDocument.from_path('tables.pdf', include_visuals=True)
```

## Detect Table Components

Use layout detection to identify table regions in the document.

```python theme={null}
detector = PaddleLayoutDetector(device='cpu')
parser = SimpleLayoutParser(detector)
components = parser.parse_document(doc)
```

## Visualize Results (Optional)

See how the layout detection performed on your document.

```python theme={null}
image = document.render_image_with_component_blocks(4, components) # page 5
display(Image.fromarray(image))
```

<img src="https://mintcdn.com/parseport/KZLF7iZjsiOLCgyQ/images/table_to_csv_detection.png?fit=max&auto=format&n=KZLF7iZjsiOLCgyQ&q=85&s=a2fa3eb78462375dd6fa7d4a08e0d42c" alt="Image" width="2550" height="3301" data-path="images/table_to_csv_detection.png" />

## Extract Text from Tables

Extract text from detected table components and filter for table types only.

```python theme={null}
vlm_generator = OpenAIVLMGenerator()
reader = VLMDocumentReader(vlm_generator)
components = await reader.extract_texts(doc, components, show_progress=True)

tables = [component for component in components if component.type == ComponentType.TABLE]
print(f'Found {len(tables)} tables')
```

```text theme={null}
number of tables: 29
```

## Convert to CSV Format

Convert each table component into clean CSV format.

```python theme={null}
formatter = VLMFormatter(vlm_generator)
semaphore = asyncio.Semaphore(10)

async def format_table(table_component):
    async with semaphore:
        return await formatter.format_component(
            doc,
            table_component,
            "Extract the table into a CSV format. Output just CSV text, no other text. No quotes unless it's in the table."
        )

formatted_tables = await asyncio.gather(*[format_table(table) for table in tables])
```

## Save Results

Process and display the CSV-formatted tables, ready for export or further analysis.

```python theme={null}
for i, formatted_table in enumerate(formatted_tables):
    print(f"Table {i+1}:")
    print(formatted_table)
    print()
```

```text theme={null}
Role,Actor
Main character,Daniel Radcliffe
Sidekick 1,Rupert Grint
Sidekick 2,Emma Watson
Lovable ogre,Robbie Coltrane
Professor,Maggie Smith
Headmaster,Richard Harris

Name,2008 Entered,2008 Completed,2009 Entered,2009 Completed
Bob,22,21,20,19
Sue,44,12,12,10

Rainfall (inches),Americas,Asia,Europe,Africa
Average,133,244,155,166
24 hour high,27,28,29,20
12 hour high,11,12,13,16
...
```
