从一个范围复制样式到另一个范围?

我有一个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 

你的工作量是复制六种风格,例如
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) ... 

除此之外,请考虑减less代码,例如:

 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)