首先调用style
(非styles
)和border
(不borders
)属性.另外要更改边框,您应该cell.border
直接设置.
除了你有一些边界逻辑的问题,由于迭代器和角落,使它正常工作更复杂.这是一个粗略的版本(它很简单,我可以得到它,但不是内存效率):
def set_border(ws, cell_range): rows = ws[cell_range] side = Side(border_, 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