2014-02-06 2 views
13

여러 셀 주위에 경계를 설정 .파이썬 XlsxWriter 내가 여러 세포 주위에 경계를 설정하는 쉬운 방법이 필요하므로 같은

내가 좋아하는 뭔가를 기다리고 있었다 :

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

이 (즉, 개별적으로 각 셀에 대해 top_border, bottom_border, left_border, right_border 설정을 포함하지 않음) 할 수있는 방법이 있습니까?

+0

가에서 봐 주시기 바랍니다 :

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

지금과 그 기능의 해제 구축 :

먼저, 기존 형식에 속성을 추가하여 새로운 형식을 만들 수 있어야합니다 이 대답 http://stackoverflow.com/a/32964050/1731460 – pymen

답변

2

현재로서는 쉬운 방법이 없습니다.

+0

문서와 github 페이지를 스캔했지만 말할 수 없습니다 - 아직 추가 되었습니까? Thx –

+0

아니요. 아직 지원되지 않습니다. – jmcnamara

8

XlsxWriter는 내 오래된 직업을 1,000 배 쉽게 만들 수있는 훌륭한 모듈입니다 (John에게 감사합니다!). 그러나 셀을 포맷하면 시간이 오래 걸릴 수 있습니다. 이런 일을 할 때 사용하는 몇 가지 헬퍼 함수가 있습니다.

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

이것은 나에게 도움이되었고 기본적으로 셀 상자 주위에 테두리와 다른 스타일을 추가하는 코드를 복사했습니다. 한 가지 추가 정보는 현재 예제가 형식을 사용하지만 일단 box (..) 함수가 실행되면 Format 객체에 대한 참조가 느슨해집니다. 매핑을 생성하고 상자 함수에서 반환해야하므로 나중에 시트 특정 데이터를 셀에 호출 할 때 형식 개체를 다시 사용할 수 있으므로 시트를 작성할 때 서식을 잃지 않을 것입니다. . – Matteius

+0

매트가 맞습니다. xlsxwriter의 그 측면은 성가신 일입니다. 나는이 문제를 어떻게 다루는 지 업데이트했다. 자세한 내용은 다음 질문을 확인하십시오. http://stackoverflow.com/questions/37907406/rewriting-some-functions-for-xlsxwriter-box-borders-from-python-2-to-python-3/39608582#39608582 – aubaub