Python XlsxWriter – Borders
Python XlsxWriter – Borders
This section describes how to apply and format the appearance of cell borders and text box borders.
Using Cell Borders
The following table lists the properties in the add_format() method that control the appearance of cell borders.
Description | Properties | Methods |
---|---|---|
Cell Border | ‘border | set_border() |
Bottom Border | ‘bottom’ | set_bottom() om() |
Top border | ‘top’ | set_top() |
Left border | ‘left’ | set_left() |
Right border | ‘right’ | set_right() |
Border Color | ‘border_color’ | set_border_color() |
Bottom Color | ‘bottom_color’ | set_bottom_color() |
Top Color | ‘top_color’ | set _top_color() |
Left color | ‘left_color | set_left_color() |
Right color | ‘right_color’ | set_right_color() |
Note that for each property in the add_format() method, there is a corresponding format class method, starting with the set_propertyname() method.
For example, to set a border around a cell, we can use the border attribute in the add_format() method, as shown below.
f1= wb.add_format({ 'border':2})
The same action can also be accomplished by calling the set_border( ) method:
f1 = workbook.add_format()
f1.set_border(2)
Individual border elements can be configured via attributes or format methods as shown below
- set_bottom()
- set_top()
- set_left()
- set_right()
These border methods/attributes have an integer value that corresponds to a predefined style as shown in the table below
index | name | Style | |
---|---|---|---|
0 | None | 0 | |
1 | Continuous | 1 | -———- |
2 | Continuous | 2 | -———- |
3 | Sprint | 1 | – – – – – – |
4 | Point | 1 | . . . . . . |
5 | Continuous | 3 | -————- |
6 | Double | 3 | =========== |
7 | Continuous | 0 | -————- |
8 | Sprint | 2 | – – – – – – |
9 | Sprint Point | 1 | – . – . – . |
10 | Dash Dot | 2 | – . – . – . |
11 | Sprint Point | 1 | – . .- . . |
12 | Dash Dot | 2 | – . .- . . |
13 | Slash dot | 2 | / – ./ – . |
Example
The following code shows how to use border properties. Here, each row has a border style of 2, corresponding to continuous bold.
import xlsxwriter
wb = xlsxwriter.Workbook('hello.xlsx')
ws = wb.add_worksheet()
f1=wb.add_format({'bold':True, 'border':2, 'border_color':'red'})
f2=wb.add_format({'border':2, 'border_color':'red'})
headings = ['Month', 'Product A', 'Product B']
data = [
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June'],
[10, 40, 50, 20, 10, 50],
[30, 60, 70, 50, 40, 30],
]
ws.write_row('A1', headings, f1)
ws.write_column('A2', data[0], f2)
ws.write_column('B2', data[1],f2)
ws.write_column('C2', data[2],f2)
wb.close()
Output
The worksheet displays a bold border around the cell.
Using Text Box Borders
The border property can also be used on text box objects. Text boxes also have a line property that is similar to border, so they can be used interchangeably. The border itself can be further formatted using parameters such as none, color, width, and dash_type.
Setting line or border to none means the text box will have no border. The dash_type parameter can be any of the following values: −
- solid
- round_dot
- square_dot
- dash
- dash_dot
- long_dash
- long_dash_dot
- long_dash_dot_dot
Example
Below is a program that displays two text boxes, one with a solid red border and the second with a blue dash_dot border.
import xlsxwriter
wb = xlsxwriter.Workbook('hello.xlsx')
ws = wb.add_worksheet()
ws.insert_textbox('B2', 'Welcome to Tutorialspoint',
{'border': {'color': '#FF9900'}})
ws.insert_textbox('B10', 'Welcome to Tutorialspoint', {
'line':
{'color': 'blue', 'dash_type': 'dash_dot'}
})
wb.close()
Output
The output worksheet shows the borders of the text box.