-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Change DataFrame.to_excel to output unformatted excel file #54302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
620dc40
a44242a
4c04104
ac25ee3
505b4a8
86662c4
024a162
c1731fd
ebdef70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,28 @@ def assert_equal_cell_styles(cell1, cell2): | |
assert cell1.protection.__dict__ == cell2.protection.__dict__ | ||
|
||
|
||
def test_styler_default_values(): | ||
# GH 54154 | ||
openpyxl = pytest.importorskip("openpyxl") | ||
df = DataFrame([{"A": 1, "B": 2, "C": 3}, {"A": 1, "B": 2, "C": 3}]) | ||
|
||
with tm.ensure_clean(".xlsx") as path: | ||
with ExcelWriter(path, engine="openpyxl") as writer: | ||
df.to_excel(writer, sheet_name="custom") | ||
|
||
with contextlib.closing(openpyxl.load_workbook(path)) as wb: | ||
# Check font, spacing, indentation | ||
assert wb["custom"].cell(1, 1).font.bold is False | ||
assert wb["custom"].cell(1, 1).alignment.horizontal is None | ||
assert wb["custom"].cell(1, 1).alignment.vertical is None | ||
|
||
# Check border | ||
assert wb["custom"].cell(1, 1).border.bottom.color is None | ||
assert wb["custom"].cell(1, 1).border.top.color is None | ||
assert wb["custom"].cell(1, 1).border.left.color is None | ||
assert wb["custom"].cell(1, 1).border.right.color is None | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"engine", | ||
["xlsxwriter", "openpyxl"], | ||
|
@@ -123,6 +145,39 @@ def test_styler_to_excel_unstyled(engine): | |
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"css", | ||
["background-color: #111222"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you inline this in the test since it's only 1 parameter |
||
) | ||
def test_styler_custom_style(css): | ||
# GH 54154 | ||
openpyxl = pytest.importorskip("openpyxl") | ||
df = DataFrame([{"A": 1, "B": 2}, {"A": 1, "B": 2}]) | ||
|
||
with tm.ensure_clean(".xlsx") as path: | ||
with ExcelWriter(path, engine="openpyxl") as writer: | ||
styler = df.style.map(lambda x: css) | ||
styler.to_excel(writer, sheet_name="custom", index=False) | ||
|
||
with contextlib.closing(openpyxl.load_workbook(path)) as wb: | ||
# Check font, spacing, indentation | ||
assert wb["custom"].cell(1, 1).font.bold is False | ||
assert wb["custom"].cell(1, 1).alignment.horizontal is None | ||
assert wb["custom"].cell(1, 1).alignment.vertical is None | ||
|
||
# Check border | ||
assert wb["custom"].cell(1, 1).border.bottom.color is None | ||
assert wb["custom"].cell(1, 1).border.top.color is None | ||
assert wb["custom"].cell(1, 1).border.left.color is None | ||
assert wb["custom"].cell(1, 1).border.right.color is None | ||
|
||
# Check background color | ||
assert wb["custom"].cell(2, 1).fill.fgColor.index == "00111222" | ||
assert wb["custom"].cell(3, 1).fill.fgColor.index == "00111222" | ||
assert wb["custom"].cell(2, 2).fill.fgColor.index == "00111222" | ||
assert wb["custom"].cell(3, 2).fill.fgColor.index == "00111222" | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can simplify code test by combining both functions
|
||
@pytest.mark.parametrize( | ||
"engine", | ||
["xlsxwriter", "openpyxl"], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put this under
Other API Changes
and also mention that styling can still be done withStyler.to_excel
?