-2
我是新手,我想知道您是否可以幫助我我不明白如何使用openpyxl選擇多個單元格並應用邊框,如果可以請給我一個示例。謝謝在許多單元格中應用邊框openpyxl
我是新手,我想知道您是否可以幫助我我不明白如何使用openpyxl選擇多個單元格並應用邊框,如果可以請給我一個示例。謝謝在許多單元格中應用邊框openpyxl
屬性被稱爲邊界(不是邊框)
試試這個:
def set_border(ws, cell_range):
rows = list(ws.iter_rows(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
代碼編譯沒有錯誤,但我認爲沒有邊界 –