openpyxl 中的边框

Borders in openpyxl

我正在尝试在 excel 中为最大行应用边框。下面是我的代码,我收到错误 'Int' object is not iterable 有人帮我根据最大行在 excel 中应用边框吗?

sheet = 是作品sheet

from openpyxl.styles import Border, Side, 

row = sheet.max_row
thin = Side(border_style="thin", color="000000")
for cell in row:
    cell.border = Border(top=thin, left=thin, right=thin, bottom=thin)

sheet.max_row 是一个数字,不是可迭代的对象。

您几乎成功了。如果想使用 openpyxl 在工作表的最后一行设置边框,请尝试如下操作:

row = sheet.max_row
thin = Side(border_style="thin", color="000000")

for col in range(1, sheet.max_column+1):
    cell = ws.cell(row=row, column=col)
    cell.border = Border(top=thin, left=thin, right=thin, bottom=thin)

wb.save('newfile.xlsx')