OpenPyXL - How to query cell borders?

2019-08-23 03:37发布

问题:

New to both python and openpyxl.

Writing a py script to glom through a ton of Excel workbooks/sheets, and need to find certain cells identified by their border formatting.

I see several examples online of how to set cell borders, but I need to read them.

Specifically, I wish to identify table boundaries, when the data within the table is inconsistent, but the table borders are always present. So, I need to find identify the cells with:

* top / left borders
* top / right borders
* bottom / left borders
* bottom / right borders

(thin borders). There is only one such table per worksheet.

Could some kind maven point me to a code sample? I would provide my code thus far, but honestly I have no idea how to begin. My code for looping through each worksheet is:

for row in range(1, ws.max_row, 1):
    for col in range(1, sheet.max_column+1):
        tmp = NumToAlpha(col)
        ref = str(tmp) + str(row)
        hasTopBorder = ws[ref].?????? <=== how do I get a boolean here?
        hasLeftBorder = ws[ref].?????? <=== how do I get a boolean here?
        hasRightBorder = ws[ref].?????? <=== how do I get a boolean here?
        hasBottomBorder = ws[ref].?????? <=== how do I get a boolean here?
        if hasTopBorder==True and hasLeftBorder==True and hasRightBorder==False and hasBottomBorder==False: 
            tableTopLeftCell = tmp + str(row)
        elif hasTopBorder==True and hasLeftBorder==False and hasRightBorder==True and hasBottomBorder==False: 
            tableTopRightCell = tmp + str(row)
        elif hasTopBorder==False and hasLeftBorder==True and hasRightBorder==False and hasBottomBorder==True: 
            tableBottomLeftCell = tmp + str(row)
        elif hasTopBorder==False and hasLeftBorder==False and hasRightBorder==True and hasBottomBorder==True: 
            tableBottomRightCell = tmp + str(row)
        if tableTopLeftCell != "" and tableTopRightCell != "" and tableBottomLeftCell != "" and tableBottomRightCell != "": break
    if tableTopLeftCell != "" and tableTopRightCell != "" and tableBottomLeftCell != "" and tableBottomRightCell != "": break

Comments/suggestions for streamlining this novice code welcome and gratefully received.

Update:

By querying a cell like this:

tst = sheet['Q17'].border

I see that I get this type of result - but how do I use it? Or convert it into the desired boolean?

回答1:

Here's one way.

I used is not none because the borders could be thin, double, etc.

for row in range(1, ws.max_row, 1):
    for col in range(1, ws.max_column+1):
        tmp = NumToAlpha(col)
        cellRef = str(tmp) + str(row)
        cellBorders = getCellBorders(ws, cellRef)
        if ('T' in cellBorders) or  ('L' in cellBorders) or  ('R' in cellBorders) or  ('B' in cellBorders):
            if 'myTableTopLeftCell' not in refs:
                if ('T' in cellBorders) and ('L' in cellBorders):
                    refs['myTableTopLeftCell'] = (cell.row, cell.col_idx)
                    nowInmyTable = True
            if (nowInmyTable == True) and ('L' not in cellBorders):
                if 'myTableBottomLeftCell' not in refs:
                    refs['myTableBottomLeftCell'] = (cell.row-1, cell.col_idx)

def getCellBorders(ws, cellRef):
    tmp = ws[cellRef].border
    brdrs = ''

    if tmp.top.style is not None: brdrs += 'T'
    if tmp.left.style is not None: brdrs += 'L'
    if tmp.right.style is not None: brdrs += 'R'
    if tmp.bottom.style is not None: brdrs += 'B'
    return brdrs