table-renderer 0.3.0__tar.gz → 0.4.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: table-renderer
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: A Python library for rendering structured tables as high-quality images or HTML.
5
5
  Keywords: table,renderer,rendering,image,html,weasyprint,pdfium,report
6
6
  Author: pTaunium
@@ -44,7 +44,7 @@ Description-Content-Type: text/markdown
44
44
  - **Auto-Crop**: Automatically crops images to the actual table area using visual detection, eliminating unnecessary white space.
45
45
  - **Continuous Long Tables**: Automatically stitches multiple PDF pages into a single continuous long image, preventing content from being cut off.
46
46
  - **Object-Oriented API**: Clean `Table` -> `Row/Column` -> `Cell` hierarchy with fluent interface support.
47
- - **Cascading Styles**: Manage visual styles effortlessly with "Global Defaults + Local Overrides."
47
+ - **Cascading Styles**: Manage visual styles effortlessly with logical precedence: `Cell` > `Row` > `Column` > `Table`.
48
48
  - **Multi-language Font Support**: Support for font stacks (e.g., different fonts for English and Chinese).
49
49
  - **Customizable Resolution**: Adjustable DPI for high-resolution output (e.g., 300 DPI for print).
50
50
  - **Lightweight Deployment**: No heavy browser dependencies (like Chromium), making it ideal for Docker/K8s.
@@ -82,7 +82,6 @@ from table_renderer import Table
82
82
 
83
83
  # 1. Initialize a 3x3 table
84
84
  table = Table(3, 3)
85
- table.set_width(600)
86
85
  table.set_border(width=1, color="black", style="solid")
87
86
 
88
87
  # 2. Set header style (Row 0)
@@ -105,8 +104,8 @@ table.cell(2, 2).set_text("$79")
105
104
  # 4. Global column styling (align prices to the right)
106
105
  table.get_column(2).set_align(horizontal="right").set_width(100)
107
106
 
108
- # 5. Export to image with custom DPI and padding
109
- table.to_image("report.png", dpi=300, padding=20)
107
+ # 5. Export to image with custom DPI, padding, and background color
108
+ table.to_image("report.png", dpi=300, padding=20, background_color="white")
110
109
  ```
111
110
 
112
111
  ---
@@ -21,7 +21,7 @@
21
21
  - **Auto-Crop**: Automatically crops images to the actual table area using visual detection, eliminating unnecessary white space.
22
22
  - **Continuous Long Tables**: Automatically stitches multiple PDF pages into a single continuous long image, preventing content from being cut off.
23
23
  - **Object-Oriented API**: Clean `Table` -> `Row/Column` -> `Cell` hierarchy with fluent interface support.
24
- - **Cascading Styles**: Manage visual styles effortlessly with "Global Defaults + Local Overrides."
24
+ - **Cascading Styles**: Manage visual styles effortlessly with logical precedence: `Cell` > `Row` > `Column` > `Table`.
25
25
  - **Multi-language Font Support**: Support for font stacks (e.g., different fonts for English and Chinese).
26
26
  - **Customizable Resolution**: Adjustable DPI for high-resolution output (e.g., 300 DPI for print).
27
27
  - **Lightweight Deployment**: No heavy browser dependencies (like Chromium), making it ideal for Docker/K8s.
@@ -59,7 +59,6 @@ from table_renderer import Table
59
59
 
60
60
  # 1. Initialize a 3x3 table
61
61
  table = Table(3, 3)
62
- table.set_width(600)
63
62
  table.set_border(width=1, color="black", style="solid")
64
63
 
65
64
  # 2. Set header style (Row 0)
@@ -82,8 +81,8 @@ table.cell(2, 2).set_text("$79")
82
81
  # 4. Global column styling (align prices to the right)
83
82
  table.get_column(2).set_align(horizontal="right").set_width(100)
84
83
 
85
- # 5. Export to image with custom DPI and padding
86
- table.to_image("report.png", dpi=300, padding=20)
84
+ # 5. Export to image with custom DPI, padding, and background color
85
+ table.to_image("report.png", dpi=300, padding=20, background_color="white")
87
86
  ```
88
87
 
89
88
  ---
@@ -4,7 +4,7 @@ build-backend = "uv_build"
4
4
 
5
5
  [project]
6
6
  name = "table-renderer"
7
- version = "0.3.0"
7
+ version = "0.4.0"
8
8
  description = "A Python library for rendering structured tables as high-quality images or HTML."
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -6,6 +6,6 @@ try:
6
6
  __version__ = importlib.metadata.version("table-renderer")
7
7
  except importlib.metadata.PackageNotFoundError:
8
8
  # Package is not installed (e.g. during local development without pip install -e .)
9
- __version__ = "0.3.0"
9
+ __version__ = "0.4.0"
10
10
 
11
11
  __all__ = ["Cell", "Column", "Row", "Style", "StyledObject", "Table", "__version__"]
@@ -1,8 +1,7 @@
1
- from .base import StyledObject
2
1
  from .cell import Cell
3
2
  from .column import Column
4
3
  from .row import Row
5
- from .style import Style
4
+ from .style import Style, StyledObject
6
5
  from .table import Table
7
6
 
8
7
  __all__ = ["Cell", "Column", "Row", "Style", "StyledObject", "Table"]
@@ -1,6 +1,6 @@
1
1
  from typing import Self
2
2
 
3
- from .base import StyledObject
3
+ from .style import StyledObject
4
4
 
5
5
 
6
6
  class Cell(StyledObject):
@@ -1,6 +1,6 @@
1
1
  from typing import Self
2
2
 
3
- from .base import StyledObject
3
+ from .style import StyledObject
4
4
 
5
5
 
6
6
  class Column(StyledObject):
@@ -1,4 +1,4 @@
1
- from .base import StyledObject
1
+ from .style import StyledObject
2
2
 
3
3
 
4
4
  class Row(StyledObject):
@@ -1,5 +1,5 @@
1
1
  from dataclasses import dataclass
2
- from typing import Literal
2
+ from typing import Literal, Self
3
3
 
4
4
 
5
5
  @dataclass
@@ -105,3 +105,93 @@ class Style:
105
105
  if style:
106
106
  css.append(f"border-{side}-style: {style};")
107
107
  return css
108
+
109
+
110
+ class StyledObject:
111
+ """Base class for objects that can be styled."""
112
+
113
+ def __init__(self) -> None:
114
+ """Initialize with a default Style."""
115
+ self.style = Style()
116
+
117
+ def set_font(
118
+ self,
119
+ *,
120
+ size: int | None = None,
121
+ color: str | None = None,
122
+ family: str | None = None,
123
+ weight: Literal["normal", "bold"] | None = None,
124
+ ) -> Self:
125
+ """
126
+ Set font related styles.
127
+
128
+ Args:
129
+ size: Font size in pixels.
130
+ color: Text color (e.g., 'red', '#ff0000').
131
+ family: Font family or font stack.
132
+ weight: 'normal' or 'bold'.
133
+
134
+ Returns:
135
+ The object itself for chaining.
136
+ """
137
+ self.style.font_size = size or self.style.font_size
138
+ self.style.font_color = color or self.style.font_color
139
+ self.style.font_family = family or self.style.font_family
140
+ self.style.font_weight = weight or self.style.font_weight
141
+ return self
142
+
143
+ def set_background(self, color: str) -> Self:
144
+ """
145
+ Set the background color.
146
+
147
+ Args:
148
+ color: Background color.
149
+
150
+ Returns:
151
+ The object itself for chaining.
152
+ """
153
+ self.style.bg_color = color
154
+ return self
155
+
156
+ def set_align(
157
+ self,
158
+ *,
159
+ horizontal: Literal["left", "center", "right"] | None = None,
160
+ vertical: Literal["top", "middle", "bottom"] | None = None,
161
+ ) -> Self:
162
+ """
163
+ Set text alignment.
164
+
165
+ Args:
166
+ horizontal: Horizontal alignment.
167
+ vertical: Vertical alignment.
168
+
169
+ Returns:
170
+ The object itself for chaining.
171
+ """
172
+ self.style.text_align = horizontal or self.style.text_align
173
+ self.style.vertical_align = vertical or self.style.vertical_align
174
+ return self
175
+
176
+ def set_border(
177
+ self,
178
+ *,
179
+ width: int | None = None,
180
+ color: str | None = None,
181
+ style: Literal["solid", "dashed", "dotted"] | None = None,
182
+ ) -> Self:
183
+ """
184
+ Set border styles.
185
+
186
+ Args:
187
+ width: Border width in pixels.
188
+ color: Border color.
189
+ style: Border style.
190
+
191
+ Returns:
192
+ The object itself for chaining.
193
+ """
194
+ self.style.border_width = width or self.style.border_width
195
+ self.style.border_color = color or self.style.border_color
196
+ self.style.border_style = style or self.style.border_style
197
+ return self
@@ -2,10 +2,10 @@ from typing import Self
2
2
 
3
3
  from table_renderer.renderer import render_to_image, save_html
4
4
 
5
- from .base import StyledObject
6
5
  from .cell import Cell
7
6
  from .column import Column
8
7
  from .row import Row
8
+ from .style import StyledObject
9
9
 
10
10
 
11
11
  class Table(StyledObject):
@@ -1,93 +0,0 @@
1
- from typing import Literal, Self
2
-
3
- from .style import Style
4
-
5
-
6
- class StyledObject:
7
- """Base class for objects that can be styled."""
8
-
9
- def __init__(self) -> None:
10
- """Initialize with a default Style."""
11
- self.style = Style()
12
-
13
- def set_font(
14
- self,
15
- *,
16
- size: int | None = None,
17
- color: str | None = None,
18
- family: str | None = None,
19
- weight: Literal["normal", "bold"] | None = None,
20
- ) -> Self:
21
- """
22
- Set font related styles.
23
-
24
- Args:
25
- size: Font size in pixels.
26
- color: Text color (e.g., 'red', '#ff0000').
27
- family: Font family or font stack.
28
- weight: 'normal' or 'bold'.
29
-
30
- Returns:
31
- The object itself for chaining.
32
- """
33
- self.style.font_size = size or self.style.font_size
34
- self.style.font_color = color or self.style.font_color
35
- self.style.font_family = family or self.style.font_family
36
- self.style.font_weight = weight or self.style.font_weight
37
- return self
38
-
39
- def set_background(self, color: str) -> Self:
40
- """
41
- Set the background color.
42
-
43
- Args:
44
- color: Background color.
45
-
46
- Returns:
47
- The object itself for chaining.
48
- """
49
- self.style.bg_color = color
50
- return self
51
-
52
- def set_align(
53
- self,
54
- *,
55
- horizontal: Literal["left", "center", "right"] | None = None,
56
- vertical: Literal["top", "middle", "bottom"] | None = None,
57
- ) -> Self:
58
- """
59
- Set text alignment.
60
-
61
- Args:
62
- horizontal: Horizontal alignment.
63
- vertical: Vertical alignment.
64
-
65
- Returns:
66
- The object itself for chaining.
67
- """
68
- self.style.text_align = horizontal or self.style.text_align
69
- self.style.vertical_align = vertical or self.style.vertical_align
70
- return self
71
-
72
- def set_border(
73
- self,
74
- *,
75
- width: int | None = None,
76
- color: str | None = None,
77
- style: Literal["solid", "dashed", "dotted"] | None = None,
78
- ) -> Self:
79
- """
80
- Set border styles.
81
-
82
- Args:
83
- width: Border width in pixels.
84
- color: Border color.
85
- style: Border style.
86
-
87
- Returns:
88
- The object itself for chaining.
89
- """
90
- self.style.border_width = width or self.style.border_width
91
- self.style.border_color = color or self.style.border_color
92
- self.style.border_style = style or self.style.border_style
93
- return self
@@ -40,18 +40,18 @@ td {
40
40
  {{ table_style }}
41
41
  }
42
42
 
43
- {% for row in rows %}
44
- .row-{{ row.index }} {
45
- {{ row.style }}
46
- }
47
- {% endfor %}
48
-
49
43
  {% for col in columns %}
50
44
  .col-{{ col.index }} {
51
45
  width: {{ col.width }};
52
46
  {{ col.style }}
53
47
  }
54
48
  {% endfor %}
49
+
50
+ {% for row in rows %}
51
+ .row-{{ row.index }} {
52
+ {{ row.style }}
53
+ }
54
+ {% endfor %}
55
55
  </style>
56
56
  </head>
57
57
  <body>