Styling Borders Using the "horizontal" and "vertical" Attributes

62 views
Skip to first unread message

Curtis Mayberry

unread,
Aug 28, 2018, 3:30:13 PM8/28/18
to openpyxl-users
How do the "vertical" and "horizontal" attributes of a border style work?  

The documentation shows that you need to set each cell's format.  I don't see a way to set the format for a range of cells. (except the example code for formatting a range of merged cells)  It seems like the "vertical" and "horizontal" attributes would only make sense in the context of setting the borders for a range of cells similar to the way you set the borders in Excel.  

Another explanation would be that "horizontal" sets both the top and bottom borders to the value provided and "vertical" sets the left and right borders to the value provided.  If this is the case it would seem like the first definition I provided along with a mechanism to set the border style on a range of cells would be more useful.  

When I try to set these border attributes for a cell they don't seem to do anything.  The following code doesn't change the borders of cell 'C3' after saving and then opening the worksheet in Excel.

thin_side = Side(style='thin', color=BLACK)
ws['C3'].border = Border(vertical=thin_side,
                                        horizontal=thin_side)

-Curtis

Charlie Clark

unread,
Aug 31, 2018, 3:52:02 AM8/31/18
to openpyx...@googlegroups.com
Am .08.2018, 21:30 Uhr, schrieb Curtis Mayberry <curt...@gmail.com>:

> How do the "vertical" and "horizontal" attributes of a border style work?

The specification has this to say:

18.8.25 horizontal (Horizontal Inner Borders)
This element specifies the color and line style for the horizontal inner
border(s) of a range of cells. Used in the context of dxf elements only.

18.8.44 vertical (Vertical Inner Border)
This element specifies the color and line style for the vertical inner
border(s) of a range of cells. Used in the context of dxf elements only.

> The documentation shows that you need to set each cell's format. I don't
> see a way to set the format for a range of cells. (except the example
> code for formatting a range of merged cells) It seems like the
> "vertical" and
> "horizontal" attributes would only make sense in the context of setting
> the borders for a range of cells similar to the way you set the borders
> in
> Excel.

That's because ranges exist only conceptually. In fact all cells must be
formatted individually.

> Another explanation would be that "horizontal" sets both the top and
> bottom borders to the value provided and "vertical" sets the left and
> right
> borders to the value provided. If this is the case it would seem like
> the first definition I provided along with a mechanism to set the border
> style on a range of cells would be more useful.

But it doesn't work as you suggest.

> When I try to set these border attributes for a cell they don't seem to
> do anything. The following code doesn't change the borders of cell
> 'C3' after saving and then opening the worksheet in Excel.
>
> thin_side = Side(style='thin', color=BLACK)
> ws['C3'].border = Border(vertical=thin_side,
> horizontal=thin_side)

See above. They are apparently only used in table styles.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226
Reply all
Reply to author
Forward
0 new messages