Copying styles from a range to another range?

后端 未结 1 1649
礼貌的吻别
礼貌的吻别 2021-01-29 05:17

I have an excel file that I\'m using as a template where I add information in as it\'s needed.

I have special styles and merges that need to be done on a couple of range

相关标签:
1条回答
  • 2021-01-29 06:04

    Your workload are to copy the six styles, e.g.
    new_third_col_cell.font = copy(third_col_cell.font)

    Try to copy only the style reference, instead of assigning new styles, for instance

    target_cell._style = copy(source_cell._style)
    

    You can save ~10 %, per Style, if not all Styles are are used in all cells.
    Copy only used styles, for instance:

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

    Besides this, consider to slim your code, for instance:

    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)
    
    0 讨论(0)
提交回复
热议问题