Apply borders to all cells in a range with openpyxl

后端 未结 8 793
面向向阳花
面向向阳花 2021-01-03 01:28

I have a script that takes a pandas dataframe and chops it up into several hundred chunks and saves each chunk as a separate excel file. Each chunk will have the same number

8条回答
  •  鱼传尺愫
    2021-01-03 02:07

    Decision that works on openpyxl 2.3.5

    from openpyxl.styles import Border, Side
    
    def set_border(ws, cell_range):
        border = Border(left=Side(border_style='thin', color='000000'),
                    right=Side(border_style='thin', color='000000'),
                    top=Side(border_style='thin', color='000000'),
                    bottom=Side(border_style='thin', color='000000'))
    
        rows = ws.iter_rows(cell_range)
        for row in rows:
            for cell in row:
                cell.border = border
    
    set_border(worksheet, 'A5:C10')
    

提交回复
热议问题