zoukankan      html  css  js  c++  java
  • python 利用插入空行的方式制作工资条表格

    版本信息:

     需求:

     脚本:

    # import
    import re
    import openpyxl
    from copy import copy
    
    # 封装函数
    def cell_style(cell):
        '''获取给定单元格的四大样式属性'''
        alignment = copy(cell.alignment)    # 对齐方式
        border = copy(cell.border)    # 单元格边框样式
        fill = copy(cell.fill)   # 单元格填充样式
        font = copy(cell.font)    # 单元格值的字体属性
        return alignment, border, fill, font
    
    wb = openpyxl.load_workbook('工资信息.xlsx')  # 加载工作簿
    wb.copy_worksheet(wb['工资条'])
    ws = wb.worksheets[-1]
    ws.title = '工资条_2'    # 重命名 sheet
    
    
    cells_rows = [[cell for cell in row] for row in ws.rows]
    header = [cell.value for cell in cells_rows[0]]
    alignment, border, fill, font = cell_style(cell=cells_rows[0][0])
    
    for i, _ in enumerate(rows[:-1]):
        if i > 0:
            index = i*3
            ws.insert_rows(idx=index, amount=2)
            # 循环写入表头
            for j, v in enumerate(header):
                r, c = index+1, j+1
                cell = ws.cell(row=r, column=c)
                cell.value = v
                cell.alignment = alignment
                cell.font = font
                cell.border = border
                cell.fill = fill
                # 更新公式
                if cell.column_letter in ('H', 'J'):
                    cell = ws.cell(row=r+1, column=c)
                    cell.value = re.sub('d+', str(r+1), cell.value)
    
    wb.save('test.xlsx')
    

    excel 结果:

    按:

    随着 openpyxl 的更新,其功能越发强大。

  • 相关阅读:
    python day 09 文件操作
    day python calss08 深浅copy
    python.day05
    pytonn04day
    python开发day03
    python开发day02
    python 变量名的规范
    设备描述表
    GDI基础
    windows编程-窗口
  • 原文地址:https://www.cnblogs.com/shanger/p/13184082.html
Copyright © 2011-2022 走看看