Python设置Excel的表格边框\上色\填值

设置边框 

# ボーダー設定
def format_border(ws, start_row, end_row, start_col, end_col):
    # 内部ボーダー
    for row in tuple(ws[start_row:end_row]):
        for cell in row[start_col-1:end_col]:
            cell.border = set_border('dotted', 'dotted', 'dotted', 'dotted')
    # 左側ボーダー
    for cell in [row[start_col-1] for row in ws[start_row:end_row]]:
        cell.border = set_border(cell.border.top.style, cell.border.bottom.style, 'medium', cell.border.right.style)
    # 右側ボーダー
    for cell in [row[end_col-1] for row in ws[start_row:end_row]]:
        cell.border = set_border(cell.border.top.style, cell.border.bottom.style, cell.border.left.style, 'medium')
    # 上側ボーダー
    for cell in ws[start_row][start_col-1:end_col]:
        cell.border = set_border('medium', cell.border.bottom.style, cell.border.left.style, cell.border.right.style)
    # 下側ボーダー
    for cell in ws[end_row][start_col-1:end_col]:
        cell.border = set_border(cell.border.top.style, 'medium', cell.border.left.style, cell.border.right.style)
    return ws

# 定義ボーダー様式
def set_border(t_border, b_border, l_border, r_border, t_color='000000', b_color='000000', l_color='000000', r_color='000000'):
    border = Border(top=Side(border_style=t_border, color=t_color),
                    bottom=Side(border_style=b_border, color=b_color),
                    left=Side(border_style=l_border, color=l_color),
                    right=Side(border_style=r_border, color=r_color))
    return border

 直接调用set_border()方法就行

 单个单元格上色

sheet.cell(row=1, column=1).fill = Body_fill

循环达到批量上色

    for i in range(start, end):
        for j in range(start, end):
            sheet.cell(row=j , column=i).fill = Body_fill

填值
 

sheet.cell(row=1, column=1).value = '填充内容'

批量同上

引用及参考:【Python】openpyxl设置excel的表格边框内外边框不同处理_openpyxl 边框_梦想不断超越的博客-CSDN博客

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值