python XlsxWriter 设置多个单元格周围的边框

问题描述 投票:0回答:7

我需要一种简单的方法来设置多个单元格周围的边框,如下所示: Border around cells

我发现的只是1个单元格的边框,并合并单元格,这不是我需要的。

我期待类似的事情:

worksheet.range_border(first_row, first_col, last_row, last_col)

有没有办法可以做到这一点(即不涉及设置 top_border、bottom_border、 每个单元格的左边框、右边框)?

python excel xlsxwriter
7个回答
17
投票

XlsxWriter 是一个很棒的模块,它使我的旧工作变得简单 1,000 倍(感谢 John!),但用它格式化单元格可能非常耗时。我有几个辅助函数用来做这样的事情。

首先,您需要能够通过向现有格式添加属性来创建新格式:

def add_to_format(existing_format, dict_of_properties, workbook):
    """Give a format you want to extend and a dict of the properties you want to
    extend it with, and you get them returned in a single format"""
    new_dict={}
    for key, value in existing_format.__dict__.iteritems():
        if (value != 0) and (value != {}) and (value != None):
            new_dict[key]=value
    del new_dict['escapes']

    return(workbook.add_format(dict(new_dict.items() + dict_of_properties.items())))

现在使用以下函数构建该函数:

def box(workbook, sheet_name, row_start, col_start, row_stop, col_stop):
    """Makes an RxC box. Use integers, not the 'A1' format"""

    rows = row_stop - row_start + 1
    cols = col_stop - col_start + 1

    for x in xrange((rows) * (cols)): # Total number of cells in the rectangle

        box_form = workbook.add_format()   # The format resets each loop
        row = row_start + (x // cols)
        column = col_start + (x % cols)

        if x < (cols):                     # If it's on the top row
            box_form = add_to_format(box_form, {'top':1}, workbook)
        if x >= ((rows * cols) - cols):    # If it's on the bottom row
            box_form = add_to_format(box_form, {'bottom':1}, workbook)
        if x % cols == 0:                  # If it's on the left column
            box_form = add_to_format(box_form, {'left':1}, workbook)
        if x % cols == (cols - 1):         # If it's on the right column
            box_form = add_to_format(box_form, {'right':1}, workbook)

        sheet_name.write(row, column, "", box_form)

6
投票

Gilad 的答案很棒,因为它与 Python 3 兼容。我进一步修改它以处理单列或行的场景。

# Format cell borders via a configurable RxC box
def draw_frame_border(workbook, worksheet, first_row, first_col, rows_count, cols_count,thickness=1):

    if cols_count == 1 and rows_count == 1:
        # whole cell
        worksheet.conditional_format(first_row, first_col,
                                     first_row, first_col,
                                     {'type': 'formula', 'criteria': 'True',
                                     'format': workbook.add_format({'top': thickness, 'bottom':thickness,
                                                                    'left': thickness,'right':thickness})})    
    elif rows_count == 1:
        # left cap
        worksheet.conditional_format(first_row, first_col,
                                 first_row, first_col,
                                 {'type': 'formula', 'criteria': 'True',
                                  'format': workbook.add_format({'top': thickness, 'left': thickness,'bottom':thickness})})
        # top and bottom sides
        worksheet.conditional_format(first_row, first_col + 1,
                                 first_row, first_col + cols_count - 2,
                                 {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'top': thickness,'bottom':thickness})})

        # right cap
        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': thickness, 'right': thickness,'bottom':thickness})})

    elif cols_count == 1:
        # top cap
        worksheet.conditional_format(first_row, first_col,
                                 first_row, first_col,
                                 {'type': 'formula', 'criteria': 'True',
                                  'format': workbook.add_format({'top': thickness, 'left': thickness,'right':thickness})})

        # left and right sides
        worksheet.conditional_format(first_row + 1,              first_col,
                                 first_row + rows_count - 2, first_col,
                                 {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'left': thickness,'right':thickness})})

        # bottom cap
        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': thickness, 'left': thickness,'right':thickness})})

    else:
        # top left corner
        worksheet.conditional_format(first_row, first_col,
                                 first_row, first_col,
                                 {'type': 'formula', 'criteria': 'True',
                                  'format': workbook.add_format({'top': thickness, 'left': thickness})})

        # 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': thickness, 'right': thickness})})

        # 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': thickness, 'left': thickness})})

        # 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': thickness, 'right': thickness})})

        # 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': thickness})})

        # 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': thickness})})

        # 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': thickness})})

        # 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': thickness})})

4
投票

目前没有简单的方法可以做到这一点。


3
投票

@aubaub 当前的解决方案绘制了一个空框。我需要围绕现有值绘制一个框架而不覆盖它们。这是我的职能,以防它对任何人有帮助:

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})})

2
投票

我在@Gilad 的基础上写了这个,它设置了外部的边框。这适用于外部盒子,如果您在内部进行格式化书写,这会很有帮助。我意识到我不必用这种方法做角落;但是,在编写代码后我意识到了这一点。希望这对某人有帮助。

def draw_frame_border_outside(workbook, worksheet, first_row, first_col, rows_count, cols_count):
# verify type of data passed in
if(first_row <= 0):
    first_row = 1
if(first_col <= 0):
    first_col = 1
cols_count = abs(cols_count)
rows_count = abs(rows_count)
# top left corner
worksheet.conditional_format(first_row - 1, first_col,
                             first_row - 1, first_col,
                             {'type': 'formula', 'criteria': 'True',
                              'format': workbook.add_format({'bottom': 5,'border_color': '#0000FF'})})
worksheet.conditional_format(first_row, first_col - 1,
                             first_row, first_col - 1,
                             {'type': 'formula', 'criteria': 'True',
                              'format': workbook.add_format({'right': 5,'border_color': '#0000FF'})})
# top right corner
worksheet.conditional_format(first_row - 1, first_col + cols_count - 1,
                             first_row - 1, first_col + cols_count - 1,
                             {'type': 'formula', 'criteria': 'True',
                              'format': workbook.add_format({'bottom': 5,'border_color': '#0000FF'})})
worksheet.conditional_format(first_row, first_col + cols_count,
                             first_row, first_col + cols_count,
                             {'type': 'formula', 'criteria': 'True',
                              'format': workbook.add_format({'left': 5,'border_color': '#0000FF'})})
# bottom left corner
worksheet.conditional_format(first_row + rows_count - 1, first_col - 1,
                             first_row + rows_count - 1, first_col - 1,
                             {'type': 'formula', 'criteria': 'True',
                              'format': workbook.add_format({'right': 5,'border_color': '#0000FF'})})
worksheet.conditional_format(first_row + rows_count, first_col,
                             first_row + rows_count, first_col,
                             {'type': 'formula', 'criteria': 'True',
                              'format': workbook.add_format({'top': 5,'border_color': '#0000FF'})})
# bottom right corner
worksheet.conditional_format(first_row + rows_count - 1, first_col + cols_count,
                             first_row + rows_count - 1, first_col + cols_count,
                             {'type': 'formula', 'criteria': 'True',
                              'format': workbook.add_format({'left': 5,'border_color': '#0000FF'})})
worksheet.conditional_format(first_row + rows_count, first_col + cols_count - 1,
                             first_row + rows_count, first_col + cols_count - 1,
                             {'type': 'formula', 'criteria': 'True',
                              'format': workbook.add_format({'top': 5,'border_color': '#0000FF'})})
# top
worksheet.conditional_format(first_row -1, first_col + 1,
                             first_row - 1, first_col + cols_count - 2,
                             {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'bottom': 5,'border_color': '#0000FF'})})
# left
worksheet.conditional_format(first_row + 1, first_col - 1,
                             first_row + rows_count - 2, first_col - 1,
                             {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'right': 5,'border_color': '#0000FF'})})
# bottom
worksheet.conditional_format(first_row + rows_count, first_col + 1,
                             first_row + rows_count, first_col + cols_count - 2,
                             {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'top': 5,'border_color': '#0000FF'})})
# right
worksheet.conditional_format(first_row + 1, first_col + cols_count,
                             first_row + rows_count - 2, first_col + cols_count,
                             {'type': 'formula', 'criteria': 'True', 'format': workbook.add_format({'left': 5,'border_color': '#0000FF'})})

1
投票

有一个简单的方法可以使用

conditional_format
方法来做到这一点。

我们首先需要定义边框的样式。然后我们将提到前 4 个参数作为起始行、起始列、结束行和结束列,最后一个参数作为格式。这将有助于在不使用任何循环的情况下用多个单元格的边框填充 Excel。

format = workbook.add_format({'border': 1})
worksheet.conditional_format(7, 3, 16, 7, {'type': 'no_blanks','format': format})

conditional_format
还接受 Excel 单元格范围(例如,
A1:B2
)。在这种情况下,我们也可以像下面这样实现。

worksheet.conditional_format('D7:H16', {'type': 'no_blanks','format': format})

请注意,上述数字是指数范围。也就是说,如果您在 Excel 中的起始行是

2
,那么在代码中您必须提及为
1


0
投票

我知道我已经迟到了,但如果有人还有疑问,我可以按如下方式进行操作

for col in range(ord('B'), ord('I') + 1):
            column_letter = chr(col)
            worksheet.write(f"{column_letter}{row}", "", body_format)
© www.soinside.com 2019 - 2024. All rights reserved.