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

我需要一个简单的方法来设置多个单元格的边框,如下所示: 细胞周围的边界

我发现的只有一个单元格的边界,并且合并单元格,这不是我所需要的。

我期待着像这样的东西:

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

有没有一种方法可以做到这一点(不涉及为每个单元格单独设置top_border,bottom_border,left_border,right_border)?

XlsxWriter是一个非常棒的模块,它让我的旧作业变得更加简单(谢谢John!),但是用它格式化单元格可能会很耗时。 我有一些帮助function,我用来做这样的事情。

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

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

现在通过以下方式构build该function:

 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) 

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