Insert row into Excel spreadsheet using openpyxl in Python

前端 未结 11 2298
既然无缘
既然无缘 2020-11-28 15:09

I\'m looking for the best approach for inserting a row into a spreadsheet using openpyxl.

Effectively, I have a spreadsheet (Excel 2007) which has a header row, foll

11条回答
  •  孤独总比滥情好
    2020-11-28 15:27

    I took Dallas solution and added support for merged cells:

        def insert_rows(self, row_idx, cnt, above=False, copy_style=True, fill_formulae=True):
            skip_list = []
            try:
                idx = row_idx - 1 if above else row_idx
                for (new, old) in zip(range(self.max_row+cnt,idx+cnt,-1),range(self.max_row,idx,-1)):
                    for c_idx in range(1,self.max_column):
                      col = self.cell(row=1, column=c_idx).column #get_column_letter(c_idx)
                      print("Copying %s%d to %s%d."%(col,old,col,new))
                      source = self["%s%d"%(col,old)]
                      target = self["%s%d"%(col,new)]
                      if source.coordinate in skip_list:
                          continue
    
                      if source.coordinate in self.merged_cells:
                          # This is a merged cell
                          for _range in self.merged_cell_ranges:
                              merged_cells_list = [x for x in cells_from_range(_range)][0]
                              if source.coordinate in merged_cells_list:
                                  skip_list = merged_cells_list
                                  self.unmerge_cells(_range)
                                  new_range = re.sub(str(old),str(new),_range)
                                  self.merge_cells(new_range)
                                  break
    
                      if source.data_type == Cell.TYPE_FORMULA:
                        target.value = re.sub(
                          "(\$?[A-Z]{1,3})%d"%(old),
                          lambda m: m.group(1) + str(new),
                          source.value
                        )
                      else:
                        target.value = source.value
                      target.number_format = source.number_format
                      target.font   = source.font.copy()
                      target.alignment = source.alignment.copy()
                      target.border = source.border.copy()
                      target.fill   = source.fill.copy()
                idx = idx + 1
                for row in range(idx,idx+cnt):
                    for c_idx in range(1,self.max_column):
                      col = self.cell(row=1, column=c_idx).column #get_column_letter(c_idx)
                      #print("Clearing value in cell %s%d"%(col,row))
                      cell = self["%s%d"%(col,row)]
                      cell.value = None
                      source = self["%s%d"%(col,row-1)]
                      if copy_style:
                        cell.number_format = source.number_format
                        cell.font      = source.font.copy()
                        cell.alignment = source.alignment.copy()
                        cell.border    = source.border.copy()
                        cell.fill      = source.fill.copy()
                      if fill_formulae and source.data_type == Cell.TYPE_FORMULA:
                        #print("Copying formula from cell %s%d to %s%d"%(col,row-1,col,row))
                        cell.value = re.sub(
                          "(\$?[A-Z]{1,3})%d"%(row - 1),
                          lambda m: m.group(1) + str(row),
                          source.value
                        )
    

提交回复
热议问题