python XlsxWriter set border around multiple cells

前端 未结 4 1548
星月不相逢
星月不相逢 2021-02-19 00:20

I need an easy way to set border around multiple cells, like so: \"Border

All I found was border of

4条回答
  •  时光说笑
    2021-02-19 00:57

    The current solution by @aubaub draws an empty box. I needed to draw a frame around existing values without overriding them. This is my function in case it helps anyone:

    def draw_frame_border(workbook, worksheet, first_row, first_col, rows_count, cols_count):
    
        # top left corner
        worksheet.conditional_format(first_row, first_col,
                                     first_row, first_col,
                                     {'type': 'formula', 'criteria': 'True',
                                      'format': workbook.add_format({'top': 1, 'left': 1})})
        # top right corner
        worksheet.conditional_format(first_row, first_col + cols_count - 1,
                                     first_row, first_col + cols_count - 1,
                                     {'type': 'formula', 'criteria': 'True',
                                      'format': workbook.add_format({'top': 1, 'right': 1})})
        # bottom left corner
        worksheet.conditional_format(first_row + rows_count - 1, first_col,
                                     first_row + rows_count - 1, first_col,
                                     {'type': 'formula', 'criteria': 'True',
                                      'format': workbook.add_format({'bottom': 1, 'left': 1})})
        # bottom right corner
        worksheet.conditional_format(first_row + rows_count - 1, first_col + cols_count - 1,
                                     first_row + rows_count - 1, first_col + cols_count - 1,
                                     {'type': 'formula', 'criteria': 'True',
                                      'format': workbook.add_format({'bottom': 1, 'right': 1})})
    
        # top
        worksheet.conditional_format(first_row, first_col + 1,
                                     first_row, first_col + cols_count - 2,
                                     {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'top': 1})})
        # left
        worksheet.conditional_format(first_row + 1,              first_col,
                                     first_row + rows_count - 2, first_col,
                                     {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'left': 1})})
        # bottom
        worksheet.conditional_format(first_row + rows_count - 1, first_col + 1,
                                     first_row + rows_count - 1, first_col + cols_count - 2,
                                     {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'bottom': 1})})
        # right
        worksheet.conditional_format(first_row + 1,              first_col + cols_count - 1,
                                     first_row + rows_count - 2, first_col + cols_count - 1,
                                     {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'right': 1})})
    

提交回复
热议问题