OpenPyXL-如何查询单元格边框?

时间:2018-08-17 18:08:40

标签: python openpyxl

python和openpyxl的新手。

编写py脚本来遍历大量Excel工作簿/工作表,并且需要查找通过边框格式标识的某些单元格。

我在网上看到了一些有关如何设置单元格边界的示例,但是我需要阅读它们。

具体来说,当表中的数据不一致但始终存在表边界时,我希望标识表边界。所以,我需要找到标识单元格:

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

(细边框)。每个工作表只有一个这样的表。

某种行家能否将我指向代码示例?到目前为止,我将提供我的代码,但老实说,我不知道如何开始。我遍历每个工作表的代码是:

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

欢迎并感谢收到精简此新手代码的意见/建议。

更新:

通过查询像这样的单元格:

tst = sheet['Q17'].border

我看到我得到了这种类型的结果-但是如何使用它呢?还是将其转换为所需的布尔值?

Python openpyxl cell borders

3 个答案:

答案 0 :(得分:0)

这是一种方法。

我使用is not none是因为边界可能是thindouble等。

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

答案 1 :(得分:0)

确定“ Q17”是否有边框:

from openpyxl.styles.borders import Border, Side

if sheet['Q17'].border.left.style == "thin":
    print("Left side of Cell Q17 have left thin border")

答案 2 :(得分:0)

我找到了一种通过转换为 JSON 并获取边框样式值来使用边框对象的方法

t = sheet.cell(1,1).border
        f = json.dumps(t,default=lambda x: x.__dict__)
        r = json.loads(f)
        s = r['left']['style']
        print(s) # which prints the value for left border style 
        if s == 'thin':
            #do specific action