Skip to content Skip to sidebar Skip to footer

Applying Borders To A Cell In Openpyxl

I am trying to use Openpyxl to apply a border to a cell, but I have failed on the most basic 'apply any kind of border to any cell anywhere' task. I tried copying from the Openpyx

Solution 1:

With openpyxl version 2.2.5, this snippet works for me:

from openpyxl.styles.borders import Border, Side
from openpyxl import Workbook

thin_border = Border(left=Side(style='thin'), 
                     right=Side(style='thin'), 
                     top=Side(style='thin'), 
                     bottom=Side(style='thin'))

wb = Workbook()
ws = wb.get_active_sheet()
# property cell.border should be used instead of cell.style.border
ws.cell(row=3, column=2).border = thin_border
wb.save('border_test.xlsx')

The documentation mentions other values for the style attribute :

Value must be one of {‘double’, ‘dashed’, ‘thin’, ‘medium’, ‘mediumDashDot’, ‘dashDot’, ‘thick’, ‘mediumDashed’, ‘hair’, ‘dotted’, ‘slantDashDot’, ‘mediumDashDotDot’, ‘dashDotDot’}

Solution 2:

With openpyxl version 2.0.4, this snippet works for me:

from openpyxl.styles.borders import Border, Side
from openpyxl.styles import Style
from openpyxl import Workbook

thin_border = Border(left=Side(style='thin'), 
                     right=Side(style='thin'), 
                     top=Side(style='thin'), 
                     bottom=Side(style='thin'))
my_style = Style(border=thin_border)

wb = Workbook()
ws = wb.get_active_sheet()
ws.cell(row=3, column=2).style = my_style
wb.save('border_test.xlsx')

Solution 3:

This answer works with version 2.4.8 The difference with the previous two answers is that the property for Side is border_style, not style

from openpyxl.styles.borders import Border, Side, BORDER_THIN
thin_border = Border(
    left=Side(border_style=BORDER_THIN, color='00000000'),
    right=Side(border_style=BORDER_THIN, color='00000000'),
    top=Side(border_style=BORDER_THIN, color='00000000'),
    bottom=Side(border_style=BORDER_THIN, color='00000000')
)
ws.cell(row=3, column=2).border = thin_border

Working with styles: https://openpyxl.readthedocs.io/en/2.5/styles.html

Solution 4:

If I wanna put only the bottom line in a cell, Then mention only for the bottom side of a cell like the code below,

from openpyxl.styles.borders import Border, Sidewb= Workbook()
ws = wb.activethin_border= Border(bottom=Side(style='thin'))
ws.cell(row=3, column=2).border = thin_border

wb.save("test.xlsx")

Solution 5:

Define Style and apply all your style and then you can apply that style to any cell.

  • You can define Border, Font, Alignment, Fill etc.

I have taken the example of Border and Alignment:

bd = Side(border_style='thin')
border = Border(left=bd, top=bd, right=bd, bottom=bd)
center_alignment = Alignment(horizontal="center", vertical="center")


common_style_center = NamedStyle(name="commonStyleCenter")
common_style_center.border = border
common_style_center.alignment = center_alignment

# apply style to the cell. 
summary_sheet.cell(row=scenario_start_row, column=1, value=serial_number).style = 'commonStyleCenter'

Post a Comment for "Applying Borders To A Cell In Openpyxl"