温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用openpyxl创建excel并设置单元格样式

发布时间:2020-03-02 21:21:35 来源:网络 阅读:428 作者:LeslieLiang 栏目:编程语言
wb = Workbook()
        ws = wb.create_sheet('月度排名汇总', 0)

        # 合并单元格
        ws.merge_cells('b2:b3')
        ws.merge_cells('c2:c3')
        ws.merge_cells('d2:d3')
        ws.merge_cells('e2:g2')
        ws.merge_cells('h3:j2')

        # 设置单元格文本内容
        ws['b2'].value = '负责人'
        ws['c2'].value = '部门/小组'
        ws['d2'].value = '负责产品总量'
        ws['e2'].value = '与初始排名比较'
        ws['h3'].value = '与月初排名比较'

        ws['e3'].value = ws['h4'].value = '排名提升'
        ws['f3'].value = ws['i3'].value = '排名不变'
        ws['g3'].value = ws['j3'].value = '排名下滑'

        # 创建单元格样式对象
        headerCellStyle = NamedStyle(name = 'headerCellStyle')
        headerCellStyle.alignment = Alignment(horizontal = 'center', vertical = 'center') # 水平垂直居中
        border = Side(border_style = 'thin', color = '000000') # 线框样式
        headerCellStyle.border = Border(left = border, top = border, right = border, bottom = border) # 设置单元格边框样式

        # 设置表格样式
        for row in ws['b2:j30']:
            for cell in row:
                cell.style = headerCellStyle

        # 数据写入
        for index, item in enumerate(data, 4):
            ws.cell(row = index, column = 2, value = item.get('name'))
            ws.cell(row = index, column = 3, value = item.get('group_name'))
            ws.cell(row = index, column = 4, value = item.get('total'))
            ws.cell(row = index, column = 5, value = item.get('initial').get('up'))
            ws.cell(row = index, column = 6, value = item.get('initial').get('normal'))
            ws.cell(row = index, column = 7, value = item.get('initial').get('down'))
            ws.cell(row = index, column = 8, value = item.get('month').get('up'))
            ws.cell(row = index, column = 9, value = item.get('month').get('normal'))
            ws.cell(row = index, column = 10, value = item.get('month').get('down'))

        wb.save('rank.xlsx')

效果

使用openpyxl创建excel并设置单元格样式

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI