7

I am using python 2.7.10 and openpyxl 2.3.2 and I am a Python newbie.

I am attempting to apply a border to a specified range of cells in an Excel worksheet (e.g. C3:H10). My attempt below is failing with the the following message:

AttributeError: 'Cell' object has no attribute 'styles'.

How do I attach a border to a cell? Any insights would be gratefully received.

My current code:

import openpyxl
from openpyxl.styles import Border, Side

def set_border(ws, cell_range):
    rows = ws.iter_rows(cell_range)
    for row in rows:
        row[0].styles.borders = Border(left=Side(border_style='thin', color="FF000000"))
        row[-1].styles.borders = Border(right=Side(border_style='thin', color="FF000000"))
    for c in rows[0]:
        c.styles.borders = Border(top=Side(border_style='thin', color="FF000000"))
    for c in rows[-1]:
        c.styles.borders = Border(bottom=Side(border_style='thin', color="FF000000"))


# Example call to set_border
wb = openpyxl.load_workbook('example.xlsx')
ws = wb.get_sheet_by_name('Sheet1')

set_border(ws, "B3:H10")
1
  • Where did you get the idea to try and work with "styles"? Commented Dec 30, 2015 at 9:46

3 Answers 3

19

First of all properties are called style (not styles) and border (not borders). Also to change border you should set cell.border directly.

Besides that you have some problems with borders logic, it's more complex to get it working correctly, because of iterators and corners. Here is a rough version (it is as simple as I could get it, but not memory efficient):

def set_border(ws, cell_range):
    rows = ws[cell_range]
    side = Side(border_style='thin', color="FF000000")

    rows = list(rows)  # we convert iterator to list for simplicity, but it's not memory efficient solution
    max_y = len(rows) - 1  # index of the last row
    for pos_y, cells in enumerate(rows):
        max_x = len(cells) - 1  # index of the last cell
        for pos_x, cell in enumerate(cells):
            border = Border(
                left=cell.border.left,
                right=cell.border.right,
                top=cell.border.top,
                bottom=cell.border.bottom
            )
            if pos_x == 0:
                border.left = side
            if pos_x == max_x:
                border.right = side
            if pos_y == 0:
                border.top = side
            if pos_y == max_y:
                border.bottom = side

            # set new border only if it's one of the edge cells
            if pos_x == 0 or pos_x == max_x or pos_y == 0 or pos_y == max_y:
                cell.border = border
4
  • Firstly thank you for the response. Once my reputation exceeds 15 my feedback will show your response was useful. Second, ignoring the possible logical problems for the moment, changing the properties name to border (from borders) and setting the cell.border directly (by removing .styles) yields TypeError: 'generator' object has no attribute 'getitem' at line 9 (for c in rows[0]:). Could you offer any help on what this means?
    – Pete B
    Commented Dec 30, 2015 at 2:19
  • @PeteB ws.iter_rows(cell_range) returns generator. You can't use row[N] syntax to get Nth item from generator (that's the reason why I convert it to list in my example). Commented Dec 30, 2015 at 2:24
  • The answer overwrites any existing borders such that if you have inner thinner borders this creates an outer that deleted the inner. Is this intentional? Commented Feb 22, 2016 at 15:46
  • @toasteez I'm not sure what do you mean by inner and outer borders. I've added a fix to leave all borders except the one being added unmodified. Check if it solves your issue. Commented Feb 23, 2016 at 11:17
0
border = Border(
            left=cell.border.left,
            right=cell.border.right,
            top=cell.border.top,
            bottom=cell.border.bottom)

can be replaced by:

border = cell.border.copy()

PS: your answer helped me...

1
  • This results in thousands of lines of deprecation warnings for me in the latest version of openpyxl. Commented Mar 15, 2017 at 20:56
0

There is a cell_range module that you can use, to define (rectangular) ranges, using the CellRange object.

A CellRange instance contains a property 'cells' that you can loop through. It also contains properties 'top', 'bottom', 'left' and 'right', each of them being a list of tuples (row, column) representing the cells in the given range that belong to the top, bottom, left and right (respectively) of the given cell range. For instance, (2, 4) in cell_range.top means that cell C4 belongs to the 'top' part of the cell range. You can use this to define the border, for each cell while looping through all cells in the given cell range:

from openpyxl.styles import Alignment, Font, PatternFill, Side, Border 
from openpyxl.worksheet.cell_range import CellRange

def set_border(ws, cell_range):
    side = Side(border_style='thin', color="FF000000")
    for cell in cell_range.cells:
        right = side if cell in cell_range.right else None
        left = side if cell in cell_range.left else None
        top = side if cell in cell_range.top else None
        bottom = side if cell in cell_range.bottom else None
        current_cell = ws.cell(*cell)
        current_cell.border = Border(right=right, left=left, top=top, bottom=bottom)

set_border(ws, cell_range=CellRange("C3:H10"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.