thick_border = Side(border_style='thick', color='000000')
thin_border = Side(border_style='thin',color='000000')
no_border = Side(border_style=None)
for row in sheet.iter_rows(min_row=1, min_col=1, max_row=24, max_col=5):
for cell in row:
cell.fill = PatternFill(start_color="FFFFFF", end_color="FFFFFF", fill_type="solid")
if cell.row < 4:
cell.border = Border(bottom=no_border, right=no_border)
elif cell.row == 4:
cell.border = Border(bottom=thin_border)
elif 4 < cell.row <= 6 and cell.column < 5:
cell.border = Border(right=thin_border)
elif cell.row == 7:
cell.border = Border(top=thin_border)
elif cell.row < 22:
cell.border = Border(bottom=no_border,right=no_border)
elif cell.row == 23:
cell.border = Border(top=thin_border,right=no_border)
代码中出现边框间隙
elif cell.row == 7:
cell.border = Border(top=thin_border)
我认为问题与条件“cell.row == 7”有关。意外的边界间隙可能是由于相邻单元及其边界配置之间的相互作用造成的。设置不同样式的边框时,要考虑相邻单元格的配置。
首先验证 cell.row == 7 所在行上方和下方单元格的边框配置。
确保相邻单元格的边框样式一致。
您还可以使用打印来识别意外的间隙。类似...
如果 cell.row == 7: print(f"前:行 {cell.row},列 {cell.column},边框:{cell.border}") cell.border = 边框(顶部=thin_border) print(f"之后:行{cell.row},列{cell.column},边框:{cell.border}")