openpyxl python 框线,OpenPyXL-如何查询单元格边框?

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?

0AgF0.jpg

解决方案

To identify whether the "Q17" has a border:

from openpyxl.styles.borders import Border, Side

if sheet['Q17'].border.left.style == "thin":

print("Left side of Cell Q17 have left thin border")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值