Подтвердить что ты не робот

Python XlsxWriter устанавливает границу вокруг нескольких ячеек

Мне нужен простой способ установить границу вокруг нескольких ячеек, например: Border around cells

Все, что я нашел, это граница 1 ячейки и слияние ячеек, что не то, что мне нужно.

Я ожидал чего-то вроде:

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

Есть ли способ, которым это можно сделать (это не связано с установкой top_border, bottom_border, left_border, right_border для каждой ячейки отдельно)?

4b9b3361

Ответ 1

XlsxWriter - это замечательный модуль, который упростил мою старую работу на 1000 раз (спасибо Джону!), но форматирование ячеек с ней может занять много времени. У меня есть пара вспомогательных функций, которые я использую для создания подобных вещей.

Во-первых, вам нужно создать новый формат, добавив свойства в существующий формат:

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

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

Ответ 2

В настоящее время нет простого способа сделать это.