2017-03-10 11 views
0

필자는 필자가 필요로 할 때 정보를 추가하는 템플릿으로 사용하고있는 Excel 파일을 가지고 있습니다.범위에서 다른 범위로 스타일을 복사 하시겠습니까?

몇 가지 셀 범위에서 수행해야하는 특수한 스타일과 병합이 있습니다. 그러나 데이터가 많을 때 제가 지금하고있는 방식 (bruteforcing)은 매우 느립니다.

내가 더 잘할 수있는 방법이 있습니까? 여기

for a in xrange(1, len(plans)): 
offset = 3 * a 

# Create blank templates for plans 
for x in xrange(5, 549): 
    first_col_cell = recommended.cell(row=x, column=4) 
    second_col_cell = recommended.cell(row=x, column=5) 
    third_col_cell = recommended.cell(row=x, column=6) 

    new_first_col_cell = recommended.cell(row=x, column=4 + offset) 
    new_second_col_cell = recommended.cell(row=x, column=5 + offset) 
    new_third_col_cell = recommended.cell(row=x, column=6 + offset) 

    if third_col_cell.has_style and x != 42: 
    new_third_col_cell.font = copy(third_col_cell.font) 
    new_third_col_cell.border = copy(third_col_cell.border) 
    new_third_col_cell.fill = copy(third_col_cell.fill) 
    new_third_col_cell.number_format = copy(third_col_cell.number_format) 
    new_third_col_cell.protection = copy(third_col_cell.protection) 
    new_third_col_cell.alignment = copy(third_col_cell.alignment) 
    new_third_col_cell.value = copy(third_col_cell.value) 

    if second_col_cell.has_style and x != 42: 
    new_second_col_cell.font = copy(second_col_cell.font) 
    new_second_col_cell.border = copy(second_col_cell.border) 
    new_second_col_cell.fill = copy(second_col_cell.fill) 
    new_second_col_cell.number_format = copy(second_col_cell.number_format) 
    new_second_col_cell.protection = copy(second_col_cell.protection) 
    new_second_col_cell.alignment = copy(second_col_cell.alignment) 
    new_second_col_cell.value = copy(second_col_cell.value) 

    if first_col_cell.has_style and x != 42: 
    new_first_col_cell.font = copy(first_col_cell.font) 
    new_first_col_cell.border = copy(first_col_cell.border) 
    new_first_col_cell.fill = copy(first_col_cell.fill) 
    new_first_col_cell.number_format = copy(first_col_cell.number_format) 
    new_first_col_cell.protection = copy(first_col_cell.protection) 
    new_first_col_cell.alignment = copy(first_col_cell.alignment) 
    new_first_col_cell.value = copy(first_col_cell.value) 

    if (x >= 6 and x <= 33) or x == 36 or x == 41 or x == 44: 
    recommended.merge_cells(start_row=x, start_column=4 + offset, end_row=x, end_column=6 + offset) 

    if first_col_cell.has_style: 
     recommended_merge = colnum_string(4 + offset) + str(x) + ':' + colnum_string(6 + offset) + str(x) 
     style_border(recommended, recommended_merge, border) 
     second_col_cell.border = copy(first_col_cell.border) 
     third_col_cell.border = copy(first_col_cell.border) 

이 경우, 너무 calnum_string 기능의 필요한 것을 :

def colnum_string(n): 
    div=n 
    string="" 
    while div>0: 
    module=(div-1)%26 
    string=chr(65+module)+string 
    div=int((div-module)/26) 
    return string 
+1

100 계획 내 환경에 ~ 5 분을 필요로한다. 네 천천히? – stovfl

+0

@stovfl, 아주 가깝습니다. 당황스럽게 천천히. 이 작업을 수행하는 더 좋은 방법을 찾아야합니다. – m0ngr31

답변

1

귀하의 작업는 여섯 개 스타일, 예를 들면, 복사하는 모든 스타일이에 사용되는하지 않을 경우
new_third_col_cell.font = copy(third_col_cell.font)

봅니다, 스타일 당 ~ 당신은 절약 할 수 있습니다 예를

target_cell._style = copy(source_cell._style) 

를 들어, 대신 새로운 스타일을 할당하는, 를 10 % 만 스타일 참조를 복사 모든 세포. 단지 예를 들어, 스타일을 사용
복사 :이 외에도

if source_cell._style.fontId: target_cell.font = copy(s_cell.font) 
... 

는, 예를 들어, 슬림 코드에 고려 :

def copy_style(s_cell, t_cell): 
    ... 

def merge_cells(ws, row, offset, columns_456): 
    ... 

def main(recommended, plans): 
    column_456 = [4,5,6] 

    for a in xrange(1, len(plans)): 
     offset = 3 * a 

     # Create blank templates for plans 
     for x in xrange(5, 549): 
      if x != 42: 
       for column in column_456: 
        cell = recommended.cell(row=x, column=column) 
        if cell.has_style: 
         copy_style(cell, recommended.cell(row=x, column=column+offset)) 

      if (x >= 6 and x <= 33) or x == 36 or x == 41 or x == 44: 
       merge_cells(recommended, x, offset, column_456) 
+1

'target_cell._style = copy (source_cell._style)'이 트릭을했습니다. 그로 인해 시간이 크게 단축되었습니다. 고맙습니다! – m0ngr31