Table Renderer 📊
English | 繁體中文
table-renderer is a powerful and lightweight Python library designed to convert structured tables into high-quality images (PNG, JPG, WebP) or HTML. By combining the layout power of CSS with an intuitive Python API, it's perfect for automated reporting, Kubernetes CronJobs, or any data visualization task.
✨ Features
- Broad Compatibility: Fully supports Python 3.12, 3.13, and 3.14.
- Business Friendly: Uses
pypdfium2(Apache-2.0) andWeasyPrintfor a 100% MIT-compatible dependency chain. - High-Quality Rendering: Powered by WeasyPrint (CSS engine), supporting complex text wrapping, alignment, and cell merging.
- Auto-Crop: Automatically crops images to the actual table area using visual detection, eliminating unnecessary white space.
- Continuous Long Tables: Automatically stitches multiple PDF pages into a single continuous long image, preventing content from being cut off.
- Object-Oriented API: Clean
Table->Row/Column->Cellhierarchy with fluent interface support. - Cascading Styles: Manage visual styles effortlessly with logical precedence:
Cell>Row>Column>Table. - Multi-language Font Support: Support for font stacks (e.g., different fonts for English and Chinese).
- Customizable Resolution: Adjustable DPI for high-resolution output (e.g., 300 DPI for print).
- Lightweight Deployment: No heavy browser dependencies (like Chromium), making it ideal for Docker/K8s.
🖼 Visual Example
Here is an example table generated using the code in the Quick Start section:
🚀 Quick Start
Installation
If you are developing within this project, use uv:
uv sync
To install it in another project:
pip install .
Basic Usage
from table_renderer import Table
# 1. Initialize a 3x3 table
table = Table(3, 3)
table.set_border(width=1, color="black", style="solid")
table.set_width(400)
# 2. Set header style (Row 0)
header = table.get_row(0)
header.set_background("#333").set_font(color="white", weight="bold", size=16)
header.set_align(horizontal="center")
table.cell(0, 0).set_text("Product")
table.cell(0, 1).set_text("Category")
table.cell(0, 2).set_text("Price")
# 3. Add content and demonstrate merging
table.cell(1, 0).set_text("MacBook Pro").set_span(rows=1, cols=2)
table.cell(1, 2).set_text("$2,000")
table.cell(2, 0).set_text("Magic Mouse")
table.cell(2, 1).set_text("Peripherals")
table.cell(2, 2).set_text("$79")
# 4. Global column styling (align prices to the right)
table.get_column(2).set_align(horizontal="right").set_width(100)
# 5. Export to image with custom DPI, padding, and background color
table.to_image("report.png", dpi=300, padding=20, background_color="white")
🛠 Advanced Features
1. Cell Merging
Use the anchor-based design to merge cells by specifying the starting cell and the span.
# Merge first row, first two columns (1 row, 2 columns)
table.cell(0, 0).set_span(rows=1, cols=2).set_text("Merged Header")
2. Font Management
Load local font files and define font stacks for character-level fallback.
# Load local font files (.ttf / .otf)
table.add_font("./fonts/Roboto-Regular.ttf")
table.add_font("./fonts/NotoSansTC-Medium.otf")
# Define a font stack: English prefers Roboto, Chinese falls back to Noto Sans
table.set_font(family="Roboto-Regular, NotoSansTC-Medium")
3. Embedding Images in Cells
You can embed both local images and remote URLs into any cell.
# Set a local image
table.cell(1, 0).set_image("path/to/logo.png", width=50)
# Set a remote image URL
table.cell(1, 1).set_image("https://example.com/image.jpg", width=100)
4. Layout & Sizing
Mix fixed widths, heights, percentages, and auto-sizing.
table.set_width(800) # Total table width
table.get_column(0).set_width(200) # Fixed column width (200px)
table.get_column(1).set_width("auto") # Distribute remaining space
table.get_row(0).set_height(100) # Fixed row height (100px)
Handling Extremely Wide Tables: The library dynamically estimates canvas width to prevent clipping. However, if your table contains unbreakable strings (like long URLs) and relies purely on auto-layout, you can manually override the canvas width. The auto-crop feature will automatically trim any excess white space.
# Force a massive canvas width; auto-crop will cleanly trim the excess
table.set_width(15000)
table.to_image("wide_table.png")
5. DPI, Formats & Padding
Fine-tune your output image quality and margins.
# High-res JPG with custom 20px padding
table.to_image("output.jpg", dpi=300, padding=20)
# PNG with a solid white background (instead of the default transparent)
table.to_image("output_white.png", background_color="white")
# Lightweight WebP with tight margins
table.to_image("output.webp", dpi=72, padding=0)
🐳 Docker / Kubernetes Deployment
Since weasyprint relies on system-level C libraries, your Dockerfile should include these dependencies:
FROM python:3.12-slim
# Install system dependencies for WeasyPrint rendering and fonts
RUN apt-get update && apt-get install -y --no-install-recommends \
libpango-1.0-0 \
libpangoft2-1.0-0 \
libharfbuzz-subset0 \
fonts-noto-cjk \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY . .
RUN pip install .
CMD ["python", "main.py"]
🧪 Development
Running Tests
We use pytest for testing. Ensure you have installed the development dependencies.
uv run pytest --cov=src/table_renderer tests/
Linting & Formatting
We use ruff to maintain code quality.
uv run ruff check .
uv run ruff format .
🤝 Contributing
Contributions are welcome! If you find a bug or have a feature request, please open an issue or submit a pull request.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📜 License
Distributed under the MIT License. See LICENSE for more information.
Metadata
Release files for table-renderer 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| table_renderer-0.6.0.tar.gz | 20.6 kB | Without provenance | Details for table_renderer-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| table_renderer-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for table_renderer-0.6.0-py3-none-any.whl |
Total release size: 46.4 kB
Release files / table_renderer-0.6.0.tar.gz
| Download URL | table_renderer-0.6.0.tar.gz |
|---|---|
| Size | 20.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2c8d207447ac628ec54ddaae6fe4424d97ea74aa4c8b7d75d91e40172ad6d20d
|
|
BLAKE2b-256 checksum How to use checksums |
45584c17e6d3ea422ec8d62d0b3c9d526e9c2587ae674f481c53c095ecc26025
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / table_renderer-0.6.0-py3-none-any.whl
| Download URL | table_renderer-0.6.0-py3-none-any.whl |
|---|---|
| Size | 25.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d8acc8be550538e7ed04d3877652851946494056955d07121b21c443236ecd2d
|
|
BLAKE2b-256 checksum How to use checksums |
0c2b445cba69b931519cb33610ffe930d579669f00c1d80ff66ce3e4c472584e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for table-renderer 0.6.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.