|
1 | 1 | import io |
| 2 | +import zipfile |
| 3 | + |
2 | 4 | import pytest |
| 5 | + |
3 | 6 | import pandas as pd |
4 | 7 |
|
5 | 8 | pytest.importorskip("xlsxwriter") |
6 | 9 | openpyxl = pytest.importorskip("openpyxl") |
7 | 10 |
|
8 | 11 |
|
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(): |
10 | 27 | df = pd.DataFrame({"A": [1, 2], "B": [3, 4]}) |
11 | 28 | buf = io.BytesIO() |
12 | 29 | 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) |
18 | 34 | 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