• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python教程:巧用openpyxl为指定区域设置边框为粗匣框线

python 搞java代码 3年前 (2022-05-21) 79次浏览 已收录 0个评论

有些小伙伴做表格追求版面美观,有的是喜欢表格层次分明,那就一定不能错过今天的巧用openpyxl设置边框为粗匣框线教学,集美观与层次分明一体的实用教学。

举个简单的例子,就是这样:

思路:openpyxl有一个border方法可以给单元格设置边框,同时需要设置上下左右四个方向。我们先得到这片区域的最外层的单元格们,分四个方向,我们给最左边一排的单元格设置左边框为粗线,其他三边为细线,其他三个方向的单元格方法一样。

代码示例:

<p style="line-height: 1.75em"><span>import openpyxl
from openpyxl.styles import Side, Border, colors

#定义边框样式
def my_border(t_border, b_border, l_border, r_border):
    border = Border(top=Side(border_style=t_border, color=colors.BLACK),
                    bottom=Side(border_style=b_border, color=colors.BLACK),
                    left=Side(border_style=l_border, color=colors.BLACK),
                    right=Side(border_style=r_border, color=colors.BLACK))
    return border

#初始化制定区域边框为所有框线
def format_border(s_column, s_index, e_column , e_index):
    for row in tuple(sheet[s_column + str(s_index):e_column + str(e_index)]):
        for cell in row:
            cell.border = my_border('thin', 'thin', 'thin', 'thin')

#给指定区域设置粗匣框线
def set_solid_border(area_list):
    for area in area_list:
        s_column = area[0]
        s_index = area[1]
        e_column = area[2]
        e_index = area[3]
        #设置左粗框线
        for cell in sheet[s_column][s_index - 1:e_index]:
            cell.border = my_border(cell.border.top.style, cell.border.bottom.style,
                                    'medium', cell.border.right.style)
        # 设置右粗框线
        for cell in sheet[e_column][s_index - 1:e_index]:
            cell.border = my_border(cell.border.top.style, cell.border.bottom.style,
                                    cell.border.left.style, 'medium')
        # 设置上粗框线
        for row in tuple(sheet[s_column + str(s_index):e_column + str(s_index)]):
            for cell in row:
                cell.border = my_border('medium', cell.border.bottom.style,
                                        cell.border.left.style, cell.border.right.style)
        # 设置下粗框线
        for row in tuple(sheet[s_column + str(e_index):e_column + str(e_index)]):
            for cell in row:
                cell.border = my_border(cell.border.top.style, 'medium',
                                        cell.border.left.style, cell.border.right.style)


if __name__ == '__main__':
    wb = openpyxl.load_workbook('test.xlsx')
    sheet = wb['Sheet1']
    format_border('A', 3, 'D', 10)
    set_solid_border([['A', 3, 'D', 5], ['A', 6, 'D', 7], ['A', 8, 'D', 10],
                      ['A', 3, 'A', 10], ['B', 3, 'C', 10], ['D', 3, 'D', 10]])
    wb.save('test.xlsx')<br></span></p>

www#gaodaima.com来源[email protected]搞@^&代*@码)网搞代码

运行结果如下:

设置完粗框线的表格是不是层次分明,更加美观了呢?更多Python实用知识,点击进入云海天Python教程网

来源:搞代码网:原文地址:https://www.gaodaima.com


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python教程:巧用openpyxl为指定区域设置边框为粗匣框线
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址