table-to-image 0.1.0__tar.gz

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.
@@ -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,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,71 @@
1
+ # table-to-image
2
+
3
+ Convert pandas DataFrame tables to PNG images.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pip install table-to-image
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### As a Python Library
14
+
15
+ ```python
16
+ import pandas as pd
17
+ from table_to_image import render
18
+
19
+ # Create a DataFrame
20
+ df = pd.DataFrame({
21
+ 'Name': ['Alice', 'Bob', 'Charlie'],
22
+ 'Age': [25, 30, 35],
23
+ 'City': ['NYC', 'LA', 'Chicago']
24
+ })
25
+
26
+ # Convert to image
27
+ render(df, 'output.png')
28
+
29
+ # With custom options
30
+ render(df, 'output.png', font_size=16, header_bg_color=(255, 0, 0))
31
+ ```
32
+
33
+ ### Using the Class
34
+
35
+ ```python
36
+ import pandas as pd
37
+ from table_to_image import DataFrameToImage
38
+
39
+ df = pd.DataFrame({'A': [1, 2], 'B': ['x', 'y']})
40
+
41
+ converter = DataFrameToImage(font_size=18, header_bg_color=(0, 128, 0))
42
+ converter.convert(df, 'table.png')
43
+ ```
44
+
45
+ ### CLI
46
+
47
+ ```bash
48
+ # Convert CSV to PNG
49
+ table-to-image input.csv output.png
50
+
51
+ # Convert with custom font size
52
+ table-to-image input.csv output.png --size 20
53
+
54
+ # Convert Excel file
55
+ table-to-image data.xlsx output.png
56
+ ```
57
+
58
+ ## Options
59
+
60
+ | Option | Description | Default |
61
+ |--------|-------------|---------|
62
+ | `font_size` | Font size for text | 14 |
63
+ | `header_bg_color` | Header background color (RGB tuple) | (70, 130, 180) |
64
+ | `header_text_color` | Header text color (RGB tuple) | (255, 255, 255) |
65
+ | `cell_bg_color` | Cell background color (RGB tuple) | (255, 255, 255) |
66
+ | `cell_text_color` | Cell text color (RGB tuple) | (0, 0, 0) |
67
+ | `margin` | Margin around the table | 10 |
68
+
69
+ ## License
70
+
71
+ MIT
@@ -0,0 +1,14 @@
1
+ [project]
2
+ name = "table-to-image"
3
+ version = "0.1.0"
4
+ description = "Convert markdown tables to PNG images"
5
+ readme = "README.md"
6
+ requires-python = ">=3.8"
7
+ dependencies = [
8
+ "pandas>=2.0.0",
9
+ "pillow>=10.0.0",
10
+ "typer>=0.9.0",
11
+ ]
12
+
13
+ [project.scripts]
14
+ table-to-image = "table_to_image.cli:app"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,3 @@
1
+ from .core import DataFrameToImage, render
2
+
3
+ __all__ = ["DataFrameToImage", "render"]
@@ -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()
@@ -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
+ )
@@ -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,14 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/table_to_image/__init__.py
5
+ src/table_to_image/cli.py
6
+ src/table_to_image/core.py
7
+ src/table_to_image/render.py
8
+ src/table_to_image.egg-info/PKG-INFO
9
+ src/table_to_image.egg-info/SOURCES.txt
10
+ src/table_to_image.egg-info/dependency_links.txt
11
+ src/table_to_image.egg-info/entry_points.txt
12
+ src/table_to_image.egg-info/requires.txt
13
+ src/table_to_image.egg-info/top_level.txt
14
+ tests/test_core.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ table-to-image = table_to_image.cli:app
@@ -0,0 +1,3 @@
1
+ pandas>=2.0.0
2
+ pillow>=10.0.0
3
+ typer>=0.9.0
@@ -0,0 +1 @@
1
+ table_to_image
@@ -0,0 +1,87 @@
1
+ """Tests for table_to_image core functionality."""
2
+ import tempfile
3
+ from pathlib import Path
4
+ import pandas as pd
5
+ from PIL import Image
6
+
7
+ from table_to_image import DataFrameToImage, render
8
+
9
+
10
+ def test_dataframe_to_image_init():
11
+ """Test DataFrameToImage initialization with default values."""
12
+ converter = DataFrameToImage()
13
+ assert converter.font_size == 14
14
+ assert converter.header_bg_color == (70, 130, 180)
15
+ assert converter.header_text_color == (255, 255, 255)
16
+ assert converter.cell_bg_color == (255, 255, 255)
17
+ assert converter.cell_text_color == (0, 0, 0)
18
+ assert converter.margin == 10
19
+
20
+
21
+ def test_dataframe_to_image_custom_init():
22
+ """Test DataFrameToImage initialization with custom values."""
23
+ converter = DataFrameToImage(
24
+ font_size=20,
25
+ header_bg_color=(255, 0, 0),
26
+ cell_bg_color=(200, 200, 200),
27
+ margin=20
28
+ )
29
+ assert converter.font_size == 20
30
+ assert converter.header_bg_color == (255, 0, 0)
31
+ assert converter.margin == 20
32
+
33
+
34
+ def test_render_dataframe():
35
+ """Test render function creates a valid image."""
36
+ df = pd.DataFrame({
37
+ 'Name': ['Alice', 'Bob'],
38
+ 'Age': [25, 30],
39
+ 'City': ['NYC', 'LA']
40
+ })
41
+
42
+ with tempfile.TemporaryDirectory() as tmpdir:
43
+ output_path = Path(tmpdir) / "test.png"
44
+ render(df, output_path)
45
+
46
+ # Verify file was created
47
+ assert output_path.exists()
48
+
49
+ # Verify it's a valid image
50
+ img = Image.open(output_path)
51
+ assert img.format == 'PNG'
52
+ assert img.size[0] > 0
53
+ assert img.size[1] > 0
54
+
55
+
56
+ def test_dataframe_to_image_convert():
57
+ """Test DataFrameToImage.convert method."""
58
+ df = pd.DataFrame({
59
+ 'Column A': ['Value 1', 'Value 2'],
60
+ 'Column B': [100, 200]
61
+ })
62
+
63
+ with tempfile.TemporaryDirectory() as tmpdir:
64
+ output_path = Path(tmpdir) / "convert_test.png"
65
+ converter = DataFrameToImage()
66
+ converter.convert(df, output_path)
67
+
68
+ assert output_path.exists()
69
+ img = Image.open(output_path)
70
+ assert img.format == 'PNG'
71
+
72
+
73
+ def test_render_with_custom_colors():
74
+ """Test render function with custom colors."""
75
+ df = pd.DataFrame({'X': [1, 2], 'Y': [3, 4]})
76
+
77
+ with tempfile.TemporaryDirectory() as tmpdir:
78
+ output_path = Path(tmpdir) / "custom_colors.png"
79
+ render(
80
+ df,
81
+ output_path,
82
+ font_size=16,
83
+ header_bg_color=(0, 128, 0),
84
+ header_text_color=(255, 255, 0)
85
+ )
86
+
87
+ assert output_path.exists()