2014-02-06 15 views
13

ich eine einfache Möglichkeit, brauchen Grenze um mehrere Zellen zu setzen, etwa so: Border around cellsPython XlsxWriter gesetzt Grenze um mehrere Zellen

Alle ich fand, war Grenze von 1 Zelle und Zellen verschmelzen, was nicht das, was ich brauche .

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

Gibt es eine Möglichkeit, dass dies getan werden kann (das ist nicht beteiligt Einstellung top_border, bottom_border, left_border, right_border für jede Zelle einzeln):

Ich war für so etwas wie erwartet?

+0

Bitte nehmen Sie sich einen Blick auf Diese Antwort http://stackoverflow.com/a/32964050/1731460 – pymen

Antwort

2

Derzeit gibt es keine einfache Möglichkeit, das zu tun.

+0

Ich scannte die Dokumente und die GitHub-Seite, konnte aber nicht sagen - ist das schon hinzugefügt worden? Thx –

+0

Nein. Es wird immer noch nicht unterstützt. – jmcnamara

8

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) 
+0

Das war hilfreich für mich und ich habe im Grunde Ihren Code kopiert, um einen Rahmen und ein anderes Styling um eine Kiste von Zellen hinzuzufügen. Ein Addendum ist, dass Ihr aktuelles Beispiel die Formate verwendet, aber sobald die Box (..) -Funktion ausgeführt wird, verlieren Sie Verweise auf die Format-Objekte. Ich musste ein Mapping erstellen und es von der Box-Funktion zurückgeben, damit ich später die Formatobjekte beim Schreiben von textspezifischen Daten in die Zellen innerhalb des Boxaufrufs wiederverwenden konnte, so dass sie ihre Formatierung nicht verlieren würden, wenn ich das Blatt + wb schrieb . – Matteius

+0

Matteius hat Recht. Dieser Aspekt von xlsxwriter ist nervig. Ich habe aktualisiert, wie ich mit diesem Problem umgehe. Überprüfen Sie diese Frage für bessere Anweisungen: http://stackoverflow.com/questions/37907406/rewritting-some-functions-for-xlsxwriter-box-borders-from-python-2-to-python-3/39608582#39608582 – aubaub