Skip to content

Commit 403b50f

Browse files
committed
cond format: add support for bool type values
1 parent ccb0d72 commit 403b50f

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

dev/docs/source/format.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ properties that can be applied and the equivalent object method:
278278
+------------+------------------+----------------------+------------------------------+
279279
| | Right color | ``'right_color'`` | :func:`set_right_color()` |
280280
+------------+------------------+----------------------+------------------------------+
281-
| Misc. | Cell border | ``'quote_prefix'`` | :func:`set_quote_prefix()` |
281+
| Other | Cell border | ``'quote_prefix'`` | :func:`set_quote_prefix()` |
282282
+------------+------------------+----------------------+------------------------------+
283283
| | Checkbox format | ``'checkbox'`` | :func:`set_checkbox()` |
284284
+------------+------------------+----------------------+------------------------------+

xlsxwriter/test/comparison/test_checkbox05.py

+31
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,34 @@ def test_create_file_with_boolean_and_format(self):
110110
workbook.close()
111111

112112
self.assertExcelEqual()
113+
114+
def test_conditional_format_with_boolean(self):
115+
"""Subtest for conditional format value as a Python boolean."""
116+
117+
workbook = Workbook(self.got_filename)
118+
worksheet = workbook.add_worksheet()
119+
120+
cell_format1 = workbook.add_format({"checkbox": True})
121+
122+
worksheet.write("E9", False, cell_format1)
123+
124+
cell_format2 = workbook.add_format(
125+
{
126+
"font_color": "#9C0006",
127+
"bg_color": "#FFC7CE",
128+
}
129+
)
130+
131+
worksheet.conditional_format(
132+
"E9",
133+
{
134+
"type": "cell",
135+
"format": cell_format2,
136+
"criteria": "equal to",
137+
"value": False,
138+
},
139+
)
140+
141+
workbook.close()
142+
143+
self.assertExcelEqual()

xlsxwriter/worksheet.py

+5
Original file line numberDiff line numberDiff line change
@@ -2936,6 +2936,11 @@ def conditional_format(
29362936
if "criteria" in options and options["criteria"] in criteria_type:
29372937
options["criteria"] = criteria_type[options["criteria"]]
29382938

2939+
# Convert boolean values if required.
2940+
if "value" in options:
2941+
if isinstance(options["value"], bool):
2942+
options["value"] = str(options["value"]).upper()
2943+
29392944
# Convert date/times value if required.
29402945
if options["type"] in ("date", "time"):
29412946
options["type"] = "cellIs"

0 commit comments

Comments
 (0)