Skip to content

Fix Excel-specific border styles #48660

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

Merged
merged 32 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e3f2ea8
Add tests
tehunter Sep 20, 2022
1657735
Add support for "hair" style
tehunter Sep 20, 2022
16258a9
Support excel-specific border styles
tehunter Sep 20, 2022
6626a97
Use black border if styled but color unspecified
tehunter Sep 20, 2022
69d75fa
Added documentation and whatsnew
tehunter Sep 20, 2022
25ad970
Black linter
tehunter Sep 20, 2022
e7d7eb0
Merge branch 'main' into excel-border-hair
tehunter Sep 20, 2022
d2680ef
Fixed tests for Excel border color fallback
tehunter Sep 20, 2022
ec3e137
Revert style.ipynb metadata
tehunter Sep 22, 2022
fb0f81c
Fix typo
tehunter Sep 22, 2022
cb8068a
Revert border-color default value
tehunter Sep 26, 2022
d169d3a
Revert "Fixed tests for Excel border color fallback"
tehunter Sep 26, 2022
e632719
Revert border color default tests
tehunter Sep 27, 2022
222cf3a
Updated whatsnew entry
tehunter Sep 27, 2022
f7b698c
Merge branch 'main' into excel-border-hair
tehunter Sep 27, 2022
3e65ce7
Add method link to whatsnew
tehunter Sep 28, 2022
db09655
Merge branch 'main' into excel-border-hair
tehunter Sep 28, 2022
e352a1c
Add tests and fix case sensitivity
tehunter Sep 28, 2022
c6e4c5c
Merge branch 'main' into excel-border-hair
tehunter Sep 28, 2022
d9fad6d
Apply black linter
tehunter Sep 28, 2022
7c9f7d0
Merge remote-tracking branch 'upstream/main' into excel-border-hair
tehunter Nov 2, 2022
a6abe18
Update v1.5.2.rst
tehunter Nov 2, 2022
eae63aa
Merge remote-tracking branch 'upstream/main' into excel-border-hair
tehunter Nov 10, 2022
e050967
Merge remote-tracking branch 'upstream/main' into excel-border-hair
tehunter Nov 10, 2022
0ae7634
Fix documentation typo
tehunter Nov 10, 2022
69366ae
Test that border shorthand works
tehunter Nov 10, 2022
ba774f7
Append Excel styles to shorthand parsing
tehunter Nov 10, 2022
97fd1c1
Update to find_stack_level call
tehunter Nov 10, 2022
84273e3
Merge remote-tracking branch 'upstream/main' into excel-border-hair
tehunter Nov 11, 2022
b5773f5
Merge branch 'main' into excel-border-hair
tehunter Nov 14, 2022
8a0f656
Merge remote-tracking branch 'upstream/main' into excel-border-hair
tehunter Nov 23, 2022
b6f6212
Update v1.5.3.rst
tehunter Nov 23, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/source/user_guide/style.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1594,8 +1594,9 @@
"\n",
"\n",
"- Only CSS2 named colors and hex colors of the form `#rgb` or `#rrggbb` are currently supported.\n",
"- The following pseudo CSS properties are also available to set excel specific style properties:\n",
"- The following pseudo CSS properties are also available to set Excel specific style properties:\n",
" - `number-format`\n",
" - `border-style` (for Excel-specific styles: \"hair\", \"mediumDashDot\", \"dashDotDot\", \"mediumDashDotDot\", \"dashDot\", \"slandDashDot\", or \"mediumDashed\")\n",
"\n",
"Table level styles, and data cell CSS-classes are not included in the export to Excel: individual cells must have their properties mapped by the `Styler.apply` and/or `Styler.applymap` methods."
]
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Bug fixes
- Bug in :func:`assert_index_equal` for extension arrays with non matching ``NA`` raising ``ValueError`` (:issue:`48608`)
- Bug in :meth:`DataFrame.pivot_table` raising unexpected ``FutureWarning`` when setting datetime column as index (:issue:`48683`)
- Bug in :meth:`DataFrame.sort_values` emitting unnecessary ``FutureWarning`` when called on :class:`DataFrame` with boolean sparse columns (:issue:`48784`)
- Bug in :meth:`.Styler.to_excel` leading to error when unrecognized ``border-style`` (including ``"hair"``) provided to Excel writers (:issue:`48649`)
-

.. ---------------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ def _border_style(self, style: str | None, width: str | None, color: str | None)
if width_name in ("hair", "thin"):
return "dashed"
return "mediumDashed"
elif style in (
"hair",
"mediumDashDot",
"dashDotDot",
"mediumDashDotDot",
"dashDot",
"slantDashDot",
"mediumDashed",
):
# Excel-specific styles
return style
else:
warnings.warn(
f"Unhandled border style format: {repr(style)}",
CSSWarning,
stacklevel=find_stack_level(inspect.currentframe()),
)
return "none"

def _get_width_name(self, width_input: str | None) -> str | None:
width = self._width_to_float(width_input)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/io/excel/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ def test_styler_to_excel_unstyled(engine):
["border", "left", "color", "rgb"],
{"xlsxwriter": "FF111222", "openpyxl": "00111222"},
),
# Border styles
(
"border-left-style: hair; border-left-color: black",
["border", "left", "style"],
"hair",
),
]


Expand Down