table-to-image 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- table_to_image/__init__.py +3 -0
- table_to_image/cli.py +30 -0
- table_to_image/core.py +43 -0
- table_to_image/render.py +102 -0
- table_to_image-0.1.0.dist-info/METADATA +83 -0
- table_to_image-0.1.0.dist-info/RECORD +10 -0
- table_to_image-0.1.0.dist-info/WHEEL +5 -0
- table_to_image-0.1.0.dist-info/entry_points.txt +2 -0
- table_to_image-0.1.0.dist-info/licenses/LICENSE +21 -0
- table_to_image-0.1.0.dist-info/top_level.txt +1 -0
table_to_image/cli.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
import typer
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from .core import render
|
|
5
|
+
|
|
6
|
+
app = typer.Typer()
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@app.command()
|
|
10
|
+
def convert(
|
|
11
|
+
input_file: Path = typer.Argument(..., help="Input CSV, Excel, or markdown table file"),
|
|
12
|
+
output_file: Path = typer.Argument(..., help="Output PNG image file"),
|
|
13
|
+
font_size: int = typer.Option(14, "-s", "--size", help="Font size"),
|
|
14
|
+
):
|
|
15
|
+
"""Convert CSV/Excel/markdown table file to PNG image."""
|
|
16
|
+
# Detect file type and read accordingly
|
|
17
|
+
if input_file.suffix.lower() in ['.xlsx', '.xls']:
|
|
18
|
+
df = pd.read_excel(input_file)
|
|
19
|
+
elif input_file.suffix.lower() == '.md':
|
|
20
|
+
# Read markdown table using pandas
|
|
21
|
+
df = pd.read_csv(input_file, sep='|', skipinitialspace=True)
|
|
22
|
+
else:
|
|
23
|
+
df = pd.read_csv(input_file)
|
|
24
|
+
|
|
25
|
+
render(df, output_file, font_size=font_size)
|
|
26
|
+
typer.echo(f"Image saved to {output_file}")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
if __name__ == "__main__":
|
|
30
|
+
app()
|
table_to_image/core.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Union
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from .render import render
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class DataFrameToImage:
|
|
8
|
+
"""Convert pandas DataFrame to PNG image."""
|
|
9
|
+
|
|
10
|
+
def __init__(
|
|
11
|
+
self,
|
|
12
|
+
font_size: int = 14,
|
|
13
|
+
header_bg_color: tuple = (70, 130, 180), # Steel blue
|
|
14
|
+
header_text_color: tuple = (255, 255, 255),
|
|
15
|
+
cell_bg_color: tuple = (255, 255, 255),
|
|
16
|
+
cell_text_color: tuple = (0, 0, 0),
|
|
17
|
+
margin: int = 10,
|
|
18
|
+
):
|
|
19
|
+
self.font_size = font_size
|
|
20
|
+
self.header_bg_color = header_bg_color
|
|
21
|
+
self.header_text_color = header_text_color
|
|
22
|
+
self.cell_bg_color = cell_bg_color
|
|
23
|
+
self.cell_text_color = cell_text_color
|
|
24
|
+
self.margin = margin
|
|
25
|
+
|
|
26
|
+
def convert(
|
|
27
|
+
self,
|
|
28
|
+
df: pd.DataFrame,
|
|
29
|
+
output_path: Union[str, Path],
|
|
30
|
+
**kwargs
|
|
31
|
+
):
|
|
32
|
+
"""Convert DataFrame to image and save."""
|
|
33
|
+
render(
|
|
34
|
+
df,
|
|
35
|
+
output_path,
|
|
36
|
+
font_size=self.font_size,
|
|
37
|
+
header_bg_color=self.header_bg_color,
|
|
38
|
+
header_text_color=self.header_text_color,
|
|
39
|
+
cell_bg_color=self.cell_bg_color,
|
|
40
|
+
cell_text_color=self.cell_text_color,
|
|
41
|
+
margin=self.margin,
|
|
42
|
+
**kwargs
|
|
43
|
+
)
|
table_to_image/render.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Union
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from PIL import Image, ImageDraw, ImageFont
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def render(
|
|
8
|
+
df: pd.DataFrame,
|
|
9
|
+
output_path: Union[str, Path],
|
|
10
|
+
font_size: int = 14,
|
|
11
|
+
header_bg_color: tuple = (70, 130, 180),
|
|
12
|
+
header_text_color: tuple = (255, 255, 255),
|
|
13
|
+
cell_bg_color: tuple = (255, 255, 255),
|
|
14
|
+
cell_text_color: tuple = (0, 0, 0),
|
|
15
|
+
margin: int = 10,
|
|
16
|
+
):
|
|
17
|
+
"""Render a pandas DataFrame as a PNG image."""
|
|
18
|
+
# Calculate dimensions
|
|
19
|
+
num_rows, num_cols = df.shape
|
|
20
|
+
|
|
21
|
+
# Get column widths based on content
|
|
22
|
+
col_widths = []
|
|
23
|
+
for col in df.columns:
|
|
24
|
+
max_width = len(str(col))
|
|
25
|
+
for val in df[col]:
|
|
26
|
+
max_width = max(max_width, len(str(val)))
|
|
27
|
+
col_widths.append(max_width)
|
|
28
|
+
|
|
29
|
+
# Get row height
|
|
30
|
+
row_height = font_size + 10
|
|
31
|
+
|
|
32
|
+
# Calculate image dimensions
|
|
33
|
+
cell_padding = 10
|
|
34
|
+
header_height = row_height
|
|
35
|
+
total_width = sum(col_widths) + (num_cols + 1) * cell_padding
|
|
36
|
+
total_height = (num_rows + 1) * row_height + (num_rows + 2) * margin
|
|
37
|
+
|
|
38
|
+
# Create image
|
|
39
|
+
img = Image.new('RGB', (total_width, total_height), color=(255, 255, 255))
|
|
40
|
+
draw = ImageDraw.Draw(img)
|
|
41
|
+
|
|
42
|
+
# Try to load a font, fall back to default if not available
|
|
43
|
+
try:
|
|
44
|
+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", font_size)
|
|
45
|
+
except IOError:
|
|
46
|
+
font = ImageFont.load_default()
|
|
47
|
+
|
|
48
|
+
# Draw headers
|
|
49
|
+
x_offset = margin + cell_padding
|
|
50
|
+
y_offset = margin
|
|
51
|
+
|
|
52
|
+
for i, col in enumerate(df.columns):
|
|
53
|
+
# Draw header background
|
|
54
|
+
header_left = x_offset + sum(col_widths[:i])
|
|
55
|
+
header_top = y_offset
|
|
56
|
+
header_right = header_left + col_widths[i]
|
|
57
|
+
header_bottom = header_top + header_height
|
|
58
|
+
draw.rectangle(
|
|
59
|
+
[header_left, header_top, header_right, header_bottom],
|
|
60
|
+
fill=header_bg_color
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
# Draw header text
|
|
64
|
+
text_x = header_left + cell_padding // 2
|
|
65
|
+
text_y = header_top + (header_height - font_size) // 2
|
|
66
|
+
draw.text((text_x, text_y), str(col), fill=header_text_color, font=font)
|
|
67
|
+
|
|
68
|
+
# Draw cells
|
|
69
|
+
for row_idx, (_, row) in enumerate(df.iterrows()):
|
|
70
|
+
y_offset += row_height
|
|
71
|
+
for col_idx, value in enumerate(row):
|
|
72
|
+
cell_left = margin + cell_padding + sum(col_widths[:col_idx])
|
|
73
|
+
cell_top = y_offset
|
|
74
|
+
cell_right = cell_left + col_widths[col_idx]
|
|
75
|
+
cell_bottom = cell_top + row_height
|
|
76
|
+
|
|
77
|
+
# Draw cell background (alternating colors for readability)
|
|
78
|
+
if row_idx % 2 == 0:
|
|
79
|
+
cell_color = cell_bg_color
|
|
80
|
+
else:
|
|
81
|
+
cell_color = (245, 245, 245)
|
|
82
|
+
draw.rectangle(
|
|
83
|
+
[cell_left, cell_top, cell_right, cell_bottom],
|
|
84
|
+
fill=cell_color
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
# Draw cell border
|
|
88
|
+
draw.rectangle(
|
|
89
|
+
[cell_left, cell_top, cell_right, cell_bottom],
|
|
90
|
+
outline=(200, 200, 200),
|
|
91
|
+
width=1
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
# Draw cell text
|
|
95
|
+
text_x = cell_left + cell_padding // 2
|
|
96
|
+
text_y = cell_top + (row_height - font_size) // 2
|
|
97
|
+
draw.text((text_x, text_y), str(value), fill=cell_text_color, font=font)
|
|
98
|
+
|
|
99
|
+
# Save image
|
|
100
|
+
output_path = Path(output_path)
|
|
101
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
102
|
+
img.save(output_path, 'PNG')
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: table-to-image
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Convert markdown tables to PNG images
|
|
5
|
+
Requires-Python: >=3.8
|
|
6
|
+
Description-Content-Type: text/markdown
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: pandas>=2.0.0
|
|
9
|
+
Requires-Dist: pillow>=10.0.0
|
|
10
|
+
Requires-Dist: typer>=0.9.0
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# table-to-image
|
|
14
|
+
|
|
15
|
+
Convert pandas DataFrame tables to PNG images.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install table-to-image
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### As a Python Library
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
import pandas as pd
|
|
29
|
+
from table_to_image import render
|
|
30
|
+
|
|
31
|
+
# Create a DataFrame
|
|
32
|
+
df = pd.DataFrame({
|
|
33
|
+
'Name': ['Alice', 'Bob', 'Charlie'],
|
|
34
|
+
'Age': [25, 30, 35],
|
|
35
|
+
'City': ['NYC', 'LA', 'Chicago']
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
# Convert to image
|
|
39
|
+
render(df, 'output.png')
|
|
40
|
+
|
|
41
|
+
# With custom options
|
|
42
|
+
render(df, 'output.png', font_size=16, header_bg_color=(255, 0, 0))
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Using the Class
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
import pandas as pd
|
|
49
|
+
from table_to_image import DataFrameToImage
|
|
50
|
+
|
|
51
|
+
df = pd.DataFrame({'A': [1, 2], 'B': ['x', 'y']})
|
|
52
|
+
|
|
53
|
+
converter = DataFrameToImage(font_size=18, header_bg_color=(0, 128, 0))
|
|
54
|
+
converter.convert(df, 'table.png')
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### CLI
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# Convert CSV to PNG
|
|
61
|
+
table-to-image input.csv output.png
|
|
62
|
+
|
|
63
|
+
# Convert with custom font size
|
|
64
|
+
table-to-image input.csv output.png --size 20
|
|
65
|
+
|
|
66
|
+
# Convert Excel file
|
|
67
|
+
table-to-image data.xlsx output.png
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Options
|
|
71
|
+
|
|
72
|
+
| Option | Description | Default |
|
|
73
|
+
|--------|-------------|---------|
|
|
74
|
+
| `font_size` | Font size for text | 14 |
|
|
75
|
+
| `header_bg_color` | Header background color (RGB tuple) | (70, 130, 180) |
|
|
76
|
+
| `header_text_color` | Header text color (RGB tuple) | (255, 255, 255) |
|
|
77
|
+
| `cell_bg_color` | Cell background color (RGB tuple) | (255, 255, 255) |
|
|
78
|
+
| `cell_text_color` | Cell text color (RGB tuple) | (0, 0, 0) |
|
|
79
|
+
| `margin` | Margin around the table | 10 |
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
MIT
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
table_to_image/__init__.py,sha256=vNB38SklMQFWYr5LavNHY6Fv8vYkWTKe3x0fseer0cQ,85
|
|
2
|
+
table_to_image/cli.py,sha256=PEmZF6w7xwUZOXgfx5AbMYVTU0RR_HXzfZd1oXcqsTk,919
|
|
3
|
+
table_to_image/core.py,sha256=VWsh-xSeYRwgpGP5DqBQzOhSUa77XXeCsRYxeHAEvxY,1256
|
|
4
|
+
table_to_image/render.py,sha256=1CZk0O6DW5NNN8faifC0qfFaueOo0I7BraoSbuqtm7Q,3389
|
|
5
|
+
table_to_image-0.1.0.dist-info/licenses/LICENSE,sha256=dUhuoK-TCRQMpuLEAdfme-qPSJI0TlcH9jlNxeg9_EQ,1056
|
|
6
|
+
table_to_image-0.1.0.dist-info/METADATA,sha256=flieqROV2eQ95sUMLcO892ux21b8r7fwN6I1nB-kqsk,1760
|
|
7
|
+
table_to_image-0.1.0.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
|
|
8
|
+
table_to_image-0.1.0.dist-info/entry_points.txt,sha256=C8d88K_QUOqPLdyMLz5W-tWWk9mYiWHbw6YKUABFKq0,58
|
|
9
|
+
table_to_image-0.1.0.dist-info/top_level.txt,sha256=--s1Nx9pOjssxfrHJnkxHfOv2u1LFPawFkT6QBj3QdQ,15
|
|
10
|
+
table_to_image-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
table_to_image
|