Apply borders to all cells in a range with openpyxl

后端 未结 8 803
面向向阳花
面向向阳花 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 01:44

    Had the same issue but couldn't find anything that fixes this problem for 2019 because of depreciation. I have something that works below.. could be better but works for all intends and purposes.

    def set_border(ws, cell_range):
        rows = ws[cell_range]
        for row in rows:
            if row == rows[0][0] or row == rows[0][-1] or row == rows[-1][0] or row == rows[-1][-1]:
                pass
            else:
                row[0].border = Border(left=Side(style='thin'))
                row[-1].border = Border(right=Side(style='thin'))
            for c in rows[0]:
                c.border = Border(top=Side(style='thin'))
            for c in rows[-1]:
                c.border = Border(bottom=Side(style='thin'))
        rows[0][0].border = Border(left=Side(style='thin'), top=Side(style='thin'))
        rows[0][-1].border = Border(right=Side(style='thin'), top=Side(style='thin'))
        rows[-1][0].border = Border(left=Side(style='thin'), bottom=Side(style='thin'))
        rows[-1][-1].border = Border(right=Side(style='thin'), bottom=Side(style='thin'))
    

提交回复
热议问题