XlsxWriter ist ein tolles Modul, das meinen alten Job 1000x einfacher gemacht hat (danke John!), Aber das Formatieren von Zellen kann zeitaufwendig sein. Ich habe ein paar Hilfsfunktionen, die ich benutze, um solche Sachen zu machen.
Zuerst müssen Sie in der Lage sein, ein neues Format zu erstellen, indem Sie Eigenschaften zu einem bestehenden Format hinzugefügt:
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())))
baut nun mit dieser Funktion aus:
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)
Bitte nehmen Sie sich einen Blick auf Diese Antwort http://stackoverflow.com/a/32964050/1731460 – pymen