Skip to main content

Table2HTML

A Python package that converts table images into HTML format using Object Detection model and OCR.

Installation

pip install table2html

Usage

Initialize

from table2html import Table2HTML

table_config = {
    "model_path": r"table2html\models\det_table_v1.pt",
    "confidence_threshold": 0.25,
    "iou_threshold": 0.7,
}

row_config = {
    "model_path": r"table2html\models\det_row_v0.pt",
    "confidence_threshold": 0.25,
    "iou_threshold": 0.7,
    "task": "detect",
}

column_config = {
    "model_path": r"table2html\models\det_col_v0.pt",
    "confidence_threshold": 0.25,
    "iou_threshold": 0.7,
    "task": "detect",
}

table2html = Table2HTML(table_config, row_config, column_config)

Table Detection

image = cv2.imread(r"table2html\images\sample.jpg")
detection_data = table2html.TableDetect(image)
# Output: [{"table_bbox": Tuple[int]}]

# Visualize table detection (first table)
from table2html.source import visualize_boxes
cv2.imwrite(
    "table_detection.jpg", 
    visualize_boxes(
        image, 
        [detection_data[0]["table_bbox"]], 
        color=(0, 0, 255),
        thickness=1
    )
)

Table detection result:

Table Detection Example

Structure Detection

data = table2html.StructureDetect(image)
# Output: {
#   "cells": List[Dict],
#   "num_rows": int,
#   "num_cols": int,
#   "html": str
# }

# Visualize structure detection
from table2html.source import visualize_boxes
cv2.imwrite(
    "structure_detection.jpg", 
    visualize_boxes(
        image, 
        [cell['box'] for cell in data['cells']], 
        color=(0, 255, 0),
        thickness=1
    )
)

# Write HTML output
with open('table.html', 'w') as f:
    f.write(data["html"])

Structure detection result:

Structure Detection Example

HTML output: extracted html.

Full Pipeline

Note: The cell coordinates are relative to the cropped table image.

table_crop_padding = 15
detection_data = table2html(image, table_crop_padding)
# Output: [{
#   "table_bbox": Tuple[int],
#   "cells": List[Dict],
#   "num_rows": int,
#   "num_cols": int,
#   "html": str
# }]

for i, data in enumerate(detection_data):
    table_image = crop_image(image, data["table_bbox"], table_crop_padding)
    cv2.imwrite(
        "table_detection.jpg",
        visualize_boxes(
            image,
            [data["table_bbox"]],
            color=(0, 0, 255),
            thickness=1
        )
    )
    cv2.imwrite(
        "structure_detection.jpg",
        visualize_boxes(
            table_image,
            [cell['box'] for cell in data['cells']],
            color=(0, 255, 0),
            thickness=1
        )
    )

    with open(f"table_{i}.html", "w") as f:
        f.write(data["html"])

Input

  • image: numpy.ndarray (OpenCV/cv2 image format)

Outputs

A list of extracted tables in structured:

  1. table_bbox: Tuple[int] - Bounding box coordinates (x1, y1, x2, y2) of the table
  2. cells: List[Dict] - List of cell dictionaries, where each dictionary contains:
    • row: int - Row index
    • column: int - Column index
    • box: Tuple[int] - Bounding box coordinates (x1, y1, x2, y2)
    • text: str - Cell text content
  3. num_rows: int - Number of rows in the table
  4. num_cols: int - Number of columns in the table
  5. html: str - HTML representation of the table

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Release files for table2html 1.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for table2html 1.4.2
File Size Uploaded
table2html-1.4.2.tar.gz 92.5 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for table2html 1.4.2
File Interpreter ABI Platform
table2html-1.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 185.0 MB

Release files / table2html-1.4.2.tar.gz

Download URL table2html-1.4.2.tar.gz
Size 92.5 MB
Tags Source
SHA-256 checksum
How to use checksums
d7e442602395e202a2bed95e2b5ad8559906ac5494dff70551dc66473a2bc56c
BLAKE2b-256 checksum
How to use checksums
6d16ac85a157102bf5c0fda8e4743040560696fb03cafbc74fa6fba804addf4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2024.

Transparency log

Release files / table2html-1.4.2-py3-none-any.whl

Download URL table2html-1.4.2-py3-none-any.whl
Size 92.5 MB
Tags Python 3
SHA-256 checksum
How to use checksums
3783c0701342f7e046a3b64fb507e25471f4e9dd053ea8c6c45a7dbe2f8998e2
BLAKE2b-256 checksum
How to use checksums
1f4c90ce3c91a68544a322f2a7acf99f402eb46a88a750ed70aa73e0f4e76c1e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2024.

Transparency log

Release history Release notifications | RSS feed

This release

1.4.2 This release

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page