Skip to content

Commit 9c0cc9c

Browse files
authored
Update test_autofilter_xlsxwriter.py
1 parent 6ef4c8e commit 9c0cc9c

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed
Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
11
import io
2+
import zipfile
3+
24
import pytest
5+
36
import pandas as pd
47

58
pytest.importorskip("xlsxwriter")
69
openpyxl = pytest.importorskip("openpyxl")
710

811

9-
def test_to_excel_xlsxwriter_autofilter_and_bold():
12+
def test_to_excel_xlsxwriter_autofilter():
13+
df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
14+
buf = io.BytesIO()
15+
with pd.ExcelWriter(buf, engine="xlsxwriter") as writer:
16+
# Test autofilter
17+
df.to_excel(writer, index=False, autofilter=True)
18+
buf.seek(0)
19+
with zipfile.ZipFile(buf) as zf:
20+
with zf.open("xl/worksheets/sheet1.xml") as f:
21+
sheet = f.read().decode("utf-8")
22+
# Check for autofilter
23+
assert '<autoFilter ref="A1:B3"' in sheet
24+
25+
26+
def test_to_excel_xlsxwriter_styler_bold_header():
1027
df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
1128
buf = io.BytesIO()
1229
with pd.ExcelWriter(buf, engine="xlsxwriter") as writer:
13-
df.to_excel(
14-
writer,
15-
index=False,
16-
engine_kwargs={"autofilter_header": True, "header_bold": True},
17-
)
30+
# Test header bold using Styler
31+
df.style.set_properties(
32+
**{"font-weight": "bold"}, subset=pd.IndexSlice[0, :]
33+
).to_excel(writer, index=False)
1834
buf.seek(0)
19-
wb = openpyxl.load_workbook(buf)
20-
ws = wb.active
21-
# Autofilter should be set spanning header+data
22-
assert ws.auto_filter is not None
23-
assert ws.auto_filter.ref is not None and ws.auto_filter.ref != ""
24-
# Header row (row 1) should be bold
25-
assert all(ws.cell(row=1, column=c).font.bold for c in range(1, df.shape[1] + 1))
35+
with zipfile.ZipFile(buf) as zf:
36+
# Check styles.xml for the bold font definition
37+
with zf.open("xl/styles.xml") as f:
38+
styles = f.read().decode("utf-8")
39+
print("\n===== STYLES XML =====")
40+
print(styles)
41+
print("===========================\n")
42+
# Check sheet1.xml for style references
43+
with zf.open("xl/worksheets/sheet1.xml") as f:
44+
sheet = f.read().decode("utf-8")
45+
print("\n===== SHEET1 XML =====")
46+
print(sheet)
47+
print("===========================\n")
48+
# Check for bold style in the styles.xml and its reference in sheet1.xml
49+
assert "<b/>" in styles, "Bold style not found in styles.xml"
50+
# Check that the header row (first row) uses a style with bold
51+
assert 'r="1"' in sheet, "Header row not found in sheet1.xml"

0 commit comments

Comments
 (0)