Extracts HTML tables and converts them to Markdown, preserving structure via row/colspan handling.
Project description
html-table-rescuer
A robust Python tool to extract complex HTML tables and convert them into clean Markdown, JSON, or CSV formats.
Unlike simple formatters, this library features a Grid Logic Solver that correctly interprets rowspan and colspan attributes, normalizing complex HTML grids into perfectly aligned data structures.
How is this different from other table2md packages?
There is an existing package called table2md on PyPI.
-
The existing package is a formatter. You feed it Python lists/dicts, and it draws a Markdown table.
-
This package is an extractor and parser. It takes raw HTML source code, uses
BeautifulSoupto parse the tags, mathematically resolves complex cell spans (rowspans/colspans), and builds an internal representation before exporting to Markdown.
The Problem:
Most parsers turn this <td rowspan="2"> into a misaligned mess:
| Header | Value |
|---|---|
| Spanned | Row 1 |
| Row 2 | | <-- Everything shifts!
The Solution:
html-table-rescuer uses a grid solver to correctly normalize the matrix:
| Header | Value |
|---|---|
| Spanned | Row 1 |
| dito (Spanned) | Row 2 |
Architecture
Our pipeline ensures that complex HTML structures are safely converted without data loss or misalignment:
graph TD
n1["HTML Input"] --> n2["BeautifulSoup Parser"]
n2 --> n3["Grid Logic<br>(Rowspan/Colspan Solver)"]
n3 --> n4["ParsedTable Data Object"]
n4 --> n5["Markdown Export"]
n4 --> n6["JSON/CSV Export"]
n4 --> n7["LangChain/LlamaIndex Wrappers"]
Installation
pip install html-table-rescuer
Quick Start
from html_table_rescuer import TableParser
html_content = """
<table border="1">
<tr>
<th colspan="2">Header</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
</table>
"""
# Initialize parser with your HTML
parser = TableParser(html_content)
# Parse all tables in the HTML
tables = parser.parse()
if tables:
table = tables[0]
# Export to Markdown (perfect for LLM context windows)
print(table.to_markdown())
# Export to JSON
# print(table.to_json())
# Export to CSV
# print(table.to_csv())
Command Line
The package installs an html-table-rescuer command that reads from a file, a URL, or stdin:
# From a file
html-table-rescuer page.html
# From a URL
html-table-rescuer https://example.com/page.html
# From stdin (pipe or '-')
curl -s https://example.com/page.html | html-table-rescuer
cat page.html | html-table-rescuer - --format json
Options:
| Option | Description |
|---|---|
--format, -f |
Output format: markdown (default), json, csv |
--strategy, -s |
Rowspan fill strategy: fill_dito (default), repeat, empty |
--dito-prefix |
Prefix used by the fill_dito strategy |
--table, -t |
Extract only the table with this index (0-based) |
--output, -o |
Write to a file instead of stdout; with CSV and multiple tables, writes name_1.csv, name_2.csv, … |
--no-links / --no-bold / --no-italic |
Strip the respective inline formatting |
--parser |
BeautifulSoup backend (lxml default, or html.parser) |
Features
- HTML parsing via
BeautifulSoup - Recursive inline-tag formatting (keeps links, bold, and italic tags alive even if nested in divs)
- Complex
rowspanandcolspangrid resolution (using flexible strategies like filling cells with "dito" to preserve context for LLMs) - Clean Markdown export
- Data Exports: JSON and CSV serialization from the
ParsedTableobject - CLI:
html-table-rescuercommand with file/URL/stdin input and Markdown/JSON/CSV output - AI Integrations: Includes a ready-to-use
LangChainDocument Loader - Robust against broken real-world HTML: invalid
colspan/rowspanvalues, HTML comments, and oversized spans are handled gracefully
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
Blog-Posts:
** Read the 1. blog:** How to Stop LLMs from Hallucinating on Complex HTML Tables ** Read the 2. blog:** How to Stop LLMs from Hallucinating on Complex HTML Tables
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file html_table_rescuer-0.2.1.tar.gz.
File metadata
- Download URL: html_table_rescuer-0.2.1.tar.gz
- Upload date:
- Size: 17.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a98d62e4f7ccc3d2b0291a34285008d2697914d7d2557c91f461ba5e81ff9fbf
|
|
| MD5 |
393cfb9d1f35d00ac1f752811a0048b4
|
|
| BLAKE2b-256 |
ff6a49de83a6d448e150abcdc700d6369cb50b93cd8aea7d0e1024e822e6443d
|
Provenance
The following attestation bundles were made for html_table_rescuer-0.2.1.tar.gz:
Publisher:
publish.yml on Encephos/html-table-rescuer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
html_table_rescuer-0.2.1.tar.gz -
Subject digest:
a98d62e4f7ccc3d2b0291a34285008d2697914d7d2557c91f461ba5e81ff9fbf - Sigstore transparency entry: 2289063809
- Sigstore integration time:
-
Permalink:
Encephos/html-table-rescuer@ba3e928fe835afe7e1a5010de321fa6e4e224d8a -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Encephos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ba3e928fe835afe7e1a5010de321fa6e4e224d8a -
Trigger Event:
release
-
Statement type:
File details
Details for the file html_table_rescuer-0.2.1-py3-none-any.whl.
File metadata
- Download URL: html_table_rescuer-0.2.1-py3-none-any.whl
- Upload date:
- Size: 12.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0f7d66c787ddd08480c63e9feaa63a663f95c8b4bf0a36802db2db33ed3ca69
|
|
| MD5 |
cb96b68472199d52de97ef5aa025e01f
|
|
| BLAKE2b-256 |
5c92ffdd25fe90274ab56a1dc3f1e14b54ad5f91fed736ccb75bba4399badac8
|
Provenance
The following attestation bundles were made for html_table_rescuer-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on Encephos/html-table-rescuer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
html_table_rescuer-0.2.1-py3-none-any.whl -
Subject digest:
c0f7d66c787ddd08480c63e9feaa63a663f95c8b4bf0a36802db2db33ed3ca69 - Sigstore transparency entry: 2289063810
- Sigstore integration time:
-
Permalink:
Encephos/html-table-rescuer@ba3e928fe835afe7e1a5010de321fa6e4e224d8a -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Encephos
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ba3e928fe835afe7e1a5010de321fa6e4e224d8a -
Trigger Event:
release
-
Statement type: