Python 将样式从一个范围复制到另一个范围?
我有一个excel文件,我正在使用它作为模板,根据需要在其中添加信息 我有一些特殊的样式和合并需要在几个单元格范围内完成,但是当有大量数据时,我现在做的方式(强行执行)非常慢 有没有办法让我做得更好Python 将样式从一个范围复制到另一个范围?,python,excel,performance,openpyxl,Python,Excel,Performance,Openpyxl,我有一个excel文件,我正在使用它作为模板,根据需要在其中添加信息 我有一些特殊的样式和合并需要在几个单元格范围内完成,但是当有大量数据时,我现在做的方式(强行执行)非常慢 有没有办法让我做得更好 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,
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)
您的工作量将复制六种样式,例如
new\u third\u col\u cell.font=复制(third\u col\u 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)
除此之外,考虑细化代码,例如:
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)
def复制单元样式(s单元、t单元):
...
def merge_单元格(ws、行、偏移、列_456):
...
def干管(推荐,平面图):
第456列=[4,5,6]
对于X范围内的a(1,len(平面)):
偏移量=3*a
#为计划创建空白模板
对于x范围内的x(5549):
如果x!=42:
对于第456列中的列:
单元格=推荐值。单元格(行=x,列=列)
如果cell.has_样式:
复制样式(单元格,推荐。单元格(行=x,列=列+偏移))
如果(x>=6和x您的工作负载要复制六种样式,例如
new\u third\u col\u cell.font=复制(third\u col\u 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)
除此之外,考虑细化代码,例如:
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)
def复制单元样式(s单元、t单元):
...
def merge_单元格(ws、行、偏移、列_456):
...
def干管(推荐,平面图):
第456列=[4,5,6]
对于X范围内的a(1,len(平面)):
偏移量=3*a
#为计划创建空白模板
对于x范围内的x(5549):
如果x!=42:
对于第456列中的列:
单元格=推荐值。单元格(行=x,列=列)
如果cell.has_样式:
复制样式(单元格,推荐。单元格(行=x,列=列+偏移))
如果(x>=6和x 100计划在我的环境中需要~5分钟。你的速度有多慢?@stovfl,非常接近于此。令人尴尬的慢。我需要找到一个更好的方法来实现这一点。100计划在我的环境中需要~5分钟。你的速度有多慢?@stovfl,非常接近于此。令人尴尬的慢。我需要找到一个更好的方法来实现这一点。target\u cell.\u style=copy(source\u cell.\u style)
成功了。这大大缩短了时间。谢谢!target\u cell.\u style=copy(source\u cell.\u style)
成功了。这大大缩短了时间。谢谢!