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 10 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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.5.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Bug in :class:`CSSToExcelConverter` leading to error when unrecognized ``border-style`` (including ``"hair"``) provided to Excel writers (:issue:`48649`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would slightly learn toward moving this to v1.6.0.rst as it appears this was "unsupported" rather than "use to work"

Copy link
Contributor Author

@tehunter tehunter Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could support either, but to play devil's advocate: pandas never specified that these particular border-styles were supported or unsupported. The closest thing to documentation was this comment block in _border_style which implies there is a path to getting the "hair" style (as well as "mediumDashDot", "slantDashDot", "dashDot", and "dashDotDot") when one didn't actually exist:

        # convert styles and widths to openxml, one of:
        #       'dashDot'
        #       'dashDotDot'
        #       'dashed'
        #       'dotted'
        #       'double'
        #       'hair'
        #       'medium'
        #       'mediumDashDot'
        #       'mediumDashDotDot'
        #       'mediumDashed'
        #       'slantDashDot'
        #       'thick'
        #       'thin'

I think the error when using xlsxwriter with an unsupported border-style should be fixed in v1.5.1 (i.e. return "none" at end of function), as the stack location and error message make it very unclear where the user has introduced the error. And if we're fixing that, then it's pretty small step from there to add the support for the other styles.

I definitely think I'm onboard with removing the color defaulting portion from a v1.5.1 and moving it to a separate PR for v1.6.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error when using xlsxwriter with an unsupported border-style should be fixed in v1.5.1 (i.e. return "none" at end of function), as the stack location and error message make it very unclear where the user has introduced the error.

The criteria for whether something should go in a patch or minor version is "did it work before". That is, patch releases should only fix regressions. As this isn't a regression, I'm also +1 on putting this in 1.6.0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that if border-style attribute was supported for some styles, in v1.5.0, one should, by extension, assume that all the different border-style values were supported without their explicit documentation. And a failing to not support certain values is either a bug in the code or a bug in the documentation. I support 1.5.1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay given the reduced scope (removing the color defaulting). I would be okay with 1.5.1

- Bug in :class:`CSSToExcelConverter` where border would not fall back on black when border style or width provided without a color (:issue:`48649`)
-

.. ---------------------------------------------------------------------------
Expand Down
41 changes: 30 additions & 11 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,18 @@ def _get_is_wrap_text(self, props: Mapping[str, str]) -> bool | None:
def build_border(
self, props: Mapping[str, str]
) -> dict[str, dict[str, str | None]]:
return {
side: {
"style": self._border_style(
props.get(f"border-{side}-style"),
props.get(f"border-{side}-width"),
self.color_to_excel(props.get(f"border-{side}-color")),
),
"color": self.color_to_excel(props.get(f"border-{side}-color")),
}
for side in ["top", "right", "bottom", "left"]
}
border_dict = {}
for side in ["top", "right", "bottom", "left"]:
style = self._border_style(
props.get(f"border-{side}-style"),
props.get(f"border-{side}-width"),
self.color_to_excel(props.get(f"border-{side}-color")),
)
color = self.color_to_excel(props.get(f"border-{side}-color"))
if style and style != "none" and color is None:
color = self.color_to_excel("black")
border_dict[side] = {"style": style, "color": color}
return border_dict

def _border_style(self, style: str | None, width: str | None, color: str | None):
# convert styles and widths to openxml, one of:
Expand Down Expand Up @@ -299,6 +300,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
13 changes: 13 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,19 @@ 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",
),
("border-left-style: hair;", ["border", "left", "style"], "hair"),
# CSS should default to black if style provided w/o color
(
"border-left-style: hair;",
["border", "left", "color", "rgb"],
{"xlsxwriter": "FF000000", "openpyxl": "00000000"},
),
]


Expand Down
57 changes: 33 additions & 24 deletions pandas/tests/io/formats/test_to_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,63 +113,72 @@
"border-style: solid",
{
"border": {
"top": {"style": "medium"},
"bottom": {"style": "medium"},
"left": {"style": "medium"},
"right": {"style": "medium"},
"top": {"style": "medium", "color": "000000"},
"bottom": {"style": "medium", "color": "000000"},
"left": {"style": "medium", "color": "000000"},
"right": {"style": "medium", "color": "000000"},
}
},
),
(
"border-style: solid; border-width: thin",
{
"border": {
"top": {"style": "thin"},
"bottom": {"style": "thin"},
"left": {"style": "thin"},
"right": {"style": "thin"},
"top": {"style": "thin", "color": "000000"},
"bottom": {"style": "thin", "color": "000000"},
"left": {"style": "thin", "color": "000000"},
"right": {"style": "thin", "color": "000000"},
}
},
),
(
"border-top-style: solid; border-top-width: thin",
{"border": {"top": {"style": "thin"}}},
{"border": {"top": {"style": "thin", "color": "000000"}}},
),
(
"border-top-style: solid; border-top-width: 1pt",
{"border": {"top": {"style": "thin"}}},
{"border": {"top": {"style": "thin", "color": "000000"}}},
),
(
"border-top-style: solid",
{"border": {"top": {"style": "medium", "color": "000000"}}},
),
("border-top-style: solid", {"border": {"top": {"style": "medium"}}}),
(
"border-top-style: solid; border-top-width: medium",
{"border": {"top": {"style": "medium"}}},
{"border": {"top": {"style": "medium", "color": "000000"}}},
),
(
"border-top-style: solid; border-top-width: 2pt",
{"border": {"top": {"style": "medium"}}},
{"border": {"top": {"style": "medium", "color": "000000"}}},
),
(
"border-top-style: solid; border-top-width: thick",
{"border": {"top": {"style": "thick"}}},
{"border": {"top": {"style": "thick", "color": "000000"}}},
),
(
"border-top-style: solid; border-top-width: 4pt",
{"border": {"top": {"style": "thick"}}},
{"border": {"top": {"style": "thick", "color": "000000"}}},
),
(
"border-top-style: dotted",
{"border": {"top": {"style": "mediumDashDotDot"}}},
{"border": {"top": {"style": "mediumDashDotDot", "color": "000000"}}},
),
(
"border-top-style: dotted; border-top-width: thin",
{"border": {"top": {"style": "dotted"}}},
{"border": {"top": {"style": "dotted", "color": "000000"}}},
),
(
"border-top-style: dashed",
{"border": {"top": {"style": "mediumDashed", "color": "000000"}}},
),
("border-top-style: dashed", {"border": {"top": {"style": "mediumDashed"}}}),
(
"border-top-style: dashed; border-top-width: thin",
{"border": {"top": {"style": "dashed"}}},
{"border": {"top": {"style": "dashed", "color": "000000"}}},
),
(
"border-top-style: double",
{"border": {"top": {"style": "double", "color": "000000"}}},
),
("border-top-style: double", {"border": {"top": {"style": "double"}}}),
# - color
(
"border-style: solid; border-color: #0000ff",
Expand Down Expand Up @@ -240,10 +249,10 @@ def test_css_to_excel_multiple():
assert {
"font": {"bold": True, "underline": "single", "color": "FF0000"},
"border": {
"top": {"style": "thin"},
"right": {"style": "thin"},
"bottom": {"style": "thin"},
"left": {"style": "thin"},
"top": {"style": "thin", "color": "000000"},
"right": {"style": "thin", "color": "000000"},
"bottom": {"style": "thin", "color": "000000"},
"left": {"style": "thin", "color": "000000"},
},
"alignment": {"horizontal": "center", "vertical": "top"},
} == actual
Expand Down