Skip to content

Commit 842826a

Browse files
committed
feat: bump version to 0.6.5, refine style handling in xlsxwriter engine and enhance template creation
Update the version to 0.6.5, improve style handling in the xlsxwriter engine by ensuring fill patterns are set for background colors. Enhance template creation in tests to include solid fill styles for better visual consistency. Adjust assertions in tests to verify style properties accurately.
1 parent 92f64e2 commit 842826a

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "xpyxl"
3-
version = "0.6.4"
3+
version = "0.6.5"
44
description = "Create styled excel reports with declarative python."
55
readme = "README.md"
66
authors = [{ name = "dakixr", email = "[email protected]" }]

src/xpyxl/engines/openpyxl_engine.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,6 @@ def _clone_sheet_contents(self, source_ws: Worksheet, target_ws: Worksheet) -> N
358358
target_cell.alignment = copy.copy(cell.alignment) # pyright: ignore[reportAttributeAccessIssue]
359359
target_cell.number_format = cell.number_format
360360
target_cell.protection = copy.copy(cell.protection) # pyright: ignore[reportAttributeAccessIssue]
361-
try:
362-
target_cell.style = cell.style
363-
except Exception:
364-
pass
365361
if getattr(cell, "hyperlink", None):
366362
target_cell._hyperlink = copy.copy(cell._hyperlink) # type: ignore[attr-defined]
367363
if cell.comment:

src/xpyxl/engines/xlsxwriter_engine.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def _get_format(
101101
# Fill color
102102
if style.fill_color:
103103
fill_color = normalize_hex(style.fill_color)
104+
# XlsxWriter requires a fill pattern for background colors to render.
105+
# Use a solid pattern and set both foreground/background for compatibility.
106+
fmt.set_pattern(1)
107+
fmt.set_fg_color(fill_color)
104108
fmt.set_bg_color(fill_color)
105109

106110
# Alignment

tests/import_sheet_demo.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44

55
import openpyxl
6+
from openpyxl.styles import PatternFill
67

78
import xpyxl as x
89

@@ -19,8 +20,16 @@ def _build_template(path: Path) -> None:
1920

2021
ws["A1"] = "Template Cover"
2122
ws["A1"].style = "Title"
23+
ws["A1"].fill = PatternFill(
24+
start_color="FFFFCC", end_color="FFFFCC", fill_type="solid"
25+
)
2226
ws.merge_cells("A1:C1")
2327

28+
ws["F1"].fill = PatternFill(
29+
start_color="FFFFCC", end_color="FFFFCC", fill_type="solid"
30+
)
31+
ws.merge_cells("F1:F12")
32+
2433
ws["A3"] = "Notes"
2534
ws["A4"] = "Static content from Excel"
2635

tests/test_xlsxwriter_import_sheet.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def _create_template(path: Path) -> None:
3535
# Add content with a merge
3636
ws["A1"] = "Template Title"
3737
ws["A1"].font = Font(bold=True, color="FF123456")
38-
ws["A1"].fill = PatternFill(fill_type="solid", start_color="FFFFE699", end_color="FFFFE699")
38+
ws["A1"].fill = PatternFill(
39+
fill_type="solid", start_color="FFFFE699", end_color="FFFFE699"
40+
)
3941
ws["A1"].alignment = Alignment(horizontal="center", vertical="center")
4042
ws.merge_cells("A1:C1")
4143

@@ -110,7 +112,7 @@ def test_xlsxwriter_with_import_sheet() -> None:
110112
# Build workbook with imported sheet + generated sheets
111113
workbook = x.workbook()[
112114
x.sheet("Before")[
113-
x.row(style=[x.bold])["Generated Before"],
115+
x.row(style=[x.bold, x.bg_warning, x.text_white])[None],
114116
x.row()["A", 1],
115117
],
116118
x.import_sheet(template_path, "Template", name="Imported"),
@@ -146,7 +148,11 @@ def test_xlsxwriter_with_import_sheet() -> None:
146148

147149
# Check generated sheets have content
148150
before_ws = result_wb["Before"]
149-
assert before_ws["A1"].value == "Generated Before"
151+
assert before_ws["A1"].value is None
152+
assert (
153+
before_ws["A1"].fill is not None
154+
and before_ws["A1"].fill.fgColor.rgb == "FFB45309"
155+
)
150156
assert before_ws["A2"].value == "A"
151157
assert before_ws["B2"].value == 1
152158

@@ -162,9 +168,18 @@ def test_xlsxwriter_with_import_sheet() -> None:
162168
assert imported_ws["A4"].value == "Static content from template"
163169
# Verify styles were preserved
164170
assert imported_ws["A1"].font.bold is True
165-
assert imported_ws["A1"].font.color is not None and imported_ws["A1"].font.color.rgb == "FF123456"
166-
assert imported_ws["A1"].fill is not None and imported_ws["A1"].fill.start_color.rgb == "FFFFE699"
167-
assert imported_ws["A1"].alignment is not None and imported_ws["A1"].alignment.horizontal == "center"
171+
assert (
172+
imported_ws["A1"].font.color is not None
173+
and imported_ws["A1"].font.color.rgb == "FF123456"
174+
)
175+
assert (
176+
imported_ws["A1"].fill is not None
177+
and imported_ws["A1"].fill.start_color.rgb == "FFFFE699"
178+
)
179+
assert (
180+
imported_ws["A1"].alignment is not None
181+
and imported_ws["A1"].alignment.horizontal == "center"
182+
)
168183

169184
# Check merge was preserved
170185
merged_ranges = list(imported_ws.merged_cells.ranges)

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)