使用Python中的openpyxl向Excel电子表格中插入行

19

我正在寻找使用openpyxl插入电子表格行的最佳方法。

实际上,我有一个包含标题行的电子表格(Excel 2007),然后是最多几千行数据。我希望将该行插入为实际数据的第一行,即在标题行之后。我的理解是,append函数适合将内容添加到文件的末尾。

阅读openpyxl和xlrd(以及xlwt)的文档后,除了手动循环遍历内容并插入所需行后,我找不到任何明确的操作方式。

鉴于我对Python的经验有限,我试图了解这是否确实是最佳选项(最符合Python风格!),如果是,是否可以提供一个明确的示例。具体而言,我能否使用openpyxl读取和写入行,还是必须访问单元格?此外,我能否(覆盖)写入同一文件(名称)?


您是否有导出为CSV的选项?这是一次性过程吗?还是需要重复进行? - Srinivas Reddy Thatiparthy
@Srini 不,我们必须坚持这种格式,而且这是一个可重复的过程:实际上,该文件会通过FTP传输到另一台服务器,并由SSIS包进行处理。 - Nick
13个回答

18

== 根据这里的反馈,更新为完全功能版本:groups.google.com/forum/#!topic/openpyxl-users/wHGecdQg3Iw。 ==

正如其他人指出的那样,openpyxl不提供此功能,但我已经扩展了Worksheet类来实现插入行。希望这对他人有用。

def insert_rows(self, row_idx, cnt, above=False, copy_style=True, fill_formulae=True):
    """Inserts new (empty) rows into worksheet at specified row index.

    :param row_idx: Row index specifying where to insert new rows.
    :param cnt: Number of rows to insert.
    :param above: Set True to insert rows above specified row index.
    :param copy_style: Set True if new rows should copy style of immediately above row.
    :param fill_formulae: Set True if new rows should take on formula from immediately above row, filled with references new to rows.

    Usage:

    * insert_rows(2, 10, above=True, copy_style=False)

    """
    CELL_RE  = re.compile("(?P<col>\$?[A-Z]+)(?P<row>\$?\d+)")

    row_idx = row_idx - 1 if above else row_idx

    def replace(m):
        row = m.group('row')
        prefix = "$" if row.find("$") != -1 else ""
        row = int(row.replace("$",""))
        row += cnt if row > row_idx else 0
        return m.group('col') + prefix + str(row)

    # First, we shift all cells down cnt rows...
    old_cells = set()
    old_fas   = set()
    new_cells = dict()
    new_fas   = dict()
    for c in self._cells.values():

        old_coor = c.coordinate

        # Shift all references to anything below row_idx
        if c.data_type == Cell.TYPE_FORMULA:
            c.value = CELL_RE.sub(
                replace,
                c.value
            )
            # Here, we need to properly update the formula references to reflect new row indices
            if old_coor in self.formula_attributes and 'ref' in self.formula_attributes[old_coor]:
                self.formula_attributes[old_coor]['ref'] = CELL_RE.sub(
                    replace,
                    self.formula_attributes[old_coor]['ref']
                )

        # Do the magic to set up our actual shift    
        if c.row > row_idx:
            old_coor = c.coordinate
            old_cells.add((c.row,c.col_idx))
            c.row += cnt
            new_cells[(c.row,c.col_idx)] = c
            if old_coor in self.formula_attributes:
                old_fas.add(old_coor)
                fa = self.formula_attributes[old_coor].copy()
                new_fas[c.coordinate] = fa

    for coor in old_cells:
        del self._cells[coor]
    self._cells.update(new_cells)

    for fa in old_fas:
        del self.formula_attributes[fa]
    self.formula_attributes.update(new_fas)

    # Next, we need to shift all the Row Dimensions below our new rows down by cnt...
    for row in range(len(self.row_dimensions)-1+cnt,row_idx+cnt,-1):
        new_rd = copy.copy(self.row_dimensions[row-cnt])
        new_rd.index = row
        self.row_dimensions[row] = new_rd
        del self.row_dimensions[row-cnt]

    # Now, create our new rows, with all the pretty cells
    row_idx += 1
    for row in range(row_idx,row_idx+cnt):
        # Create a Row Dimension for our new row
        new_rd = copy.copy(self.row_dimensions[row-1])
        new_rd.index = row
        self.row_dimensions[row] = new_rd
        for col in range(1,self.max_column):
            col = get_column_letter(col)
            cell = self.cell('%s%d'%(col,row))
            cell.value = None
            source = self.cell('%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:
                s_coor = source.coordinate
                if s_coor in self.formula_attributes and 'ref' not in self.formula_attributes[s_coor]:
                    fa = self.formula_attributes[s_coor].copy()
                    self.formula_attributes[cell.coordinate] = fa
                # 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
                )   
                cell.data_type = Cell.TYPE_FORMULA

    # Check for Merged Cell Ranges that need to be expanded to contain new cells
    for cr_idx, cr in enumerate(self.merged_cell_ranges):
        self.merged_cell_ranges[cr_idx] = CELL_RE.sub(
            replace,
            cr
        )

Worksheet.insert_rows = insert_rows

复制听起来有点过度。你可以尝试一下我在邮件列表中提出的插入列的建议 https://groups.google.com/forum/#!topic/openpyxl-users/wHGecdQg3Iw 它没有经过测试,只适用于2.2版本,因为它依赖于内部机制,但它给出了一般的方向。 - Charlie Clark
我同意修改坐标数据是理想的,但这是在不了解内部情况的情况下完成的。不确定为什么团队不会只是添加此功能,因为它在Excel中经常使用。 - Dallas
没有人提交相关代码。 openpyxl 是开源的,没有团队。 - Charlie Clark
达拉斯:某物使用频率对其实现的难易程度没有影响。正如@CharlieClark所说,这不是一个“团队”(他应该知道!)。这是一个基于phpexcel的开源项目。随着该项目的成熟,单元格/坐标系统发生了重大变化。这些变化是为了满足所需功能而必要的,否则解决方案(如上面的我的)最多也是不可靠的,并且不适合进行主要发布。请给它时间(或更好的,贡献力量!)来实现该功能。 - Rejected
1
请查看我在代码库中的片段。我已经进一步实现了这个...我会说非常接近完全了。;^) - Dallas
大家好,我们在开发中遇到了同样的问题,但是无法通过@Dallas的代码解决这个问题,请建议我们一种克服这个问题的方法。 - Amar

15

针对更近期的 openpyxl 版本(v2.5+),现在提供了新的方法 insert_rows()insert_cols()

insert_rows(idx, amount=1)

在行号为 idx 的行之前插入一行或多行。


3
很遗憾,它不支持合并单元格(它们会固定在它们所在的行位置)。 - virtualxtc

11

以下是我现在使用的代码来实现所需结果。请注意,我手动在位置1处插入了行,但这应该很容易调整以满足特定需求。您也可以轻松地调整此代码以插入多个行,并从相关位置开始填充其余数据。

此外,请注意,由于下游依赖项,我们手动指定来自'Sheet1'的数据,并将数据复制到一个新工作表中,该工作表插入到工作簿的开头,同时将原始工作表重命名为'Sheet1.5'。

编辑:我后来还添加了一些更改到format_code,以解决默认复制操作会删除所有格式的问题:new_cell.style.number_format.format_code ='mm/dd/yyyy'。我找不到任何文档说明它是可设置的,这更多是试错的情况!

最后,请不要忘记此示例正在保存覆盖原始文件。您可以根据需要更改保存路径以避免这种情况。

    import openpyxl

    wb = openpyxl.load_workbook(file)
    old_sheet = wb.get_sheet_by_name('Sheet1')
    old_sheet.title = 'Sheet1.5'
    max_row = old_sheet.get_highest_row()
    max_col = old_sheet.get_highest_column()
    wb.create_sheet(0, 'Sheet1')

    new_sheet = wb.get_sheet_by_name('Sheet1')

    # Do the header.
    for col_num in range(0, max_col):
        new_sheet.cell(row=0, column=col_num).value = old_sheet.cell(row=0, column=col_num).value

    # The row to be inserted. We're manually populating each cell.
    new_sheet.cell(row=1, column=0).value = 'DUMMY'
    new_sheet.cell(row=1, column=1).value = 'DUMMY'

    # Now do the rest of it. Note the row offset.
    for row_num in range(1, max_row):
        for col_num in range (0, max_col):
            new_sheet.cell(row = (row_num + 1), column = col_num).value = old_sheet.cell(row = row_num, column = col_num).value

    wb.save(file)

这个操作按预期处理文件,尽管我感觉应该补充说明我们现在决定实现CSV导出。这是因为openpyxl库存在编码问题,这超出了本问题的范围,但我觉得我应该提一下! - Nick

5

从openpyxl 1.5开始,您现在可以使用.insert_rows(idx, row_qty)来插入行。

from openpyxl import load_workbook
wb = load_workbook('excel_template.xlsx')
ws = wb.active
ws.insert_rows(14, 10)

如果您在Excel中手动操作,它不会捕捉idx行的格式。因此,您需要在之后应用正确的格式,例如单元格颜色。


5

在进行行或列级别操作时,Openpyxl工作表的功能受到限制。与行/列相关的Worksheet属性仅为row_dimensionscolumn_dimensions,它们分别为每行和每列存储“RowDimensions”和“ColumnDimensions”对象。这些字典也用于类似get_highest_row()get_highest_column()的函数中。

其他所有操作都是在单元格级别上进行的,单元格对象在字典_cells中被跟踪(其样式在字典_styles中被跟踪)。看起来像在行或列级别上执行任何操作的大多数函数实际上是在一系列单元格上操作的(例如前面提到的append())。

最简单的方法是创建一个新工作表,附加标题行,附加新数据行,附加旧数据行,删除旧工作表,然后将新工作表重命名为旧工作表。这种方法可能会导致行/列维度属性和单元格样式丢失,除非您也明确复制它们。

或者,您可以创建自己的插入行或列的函数。

我有许多非常简单的工作表需要从中删除列。由于您要求明确的示例,我将提供我快速组合的函数:

from openpyxl.cell import get_column_letter

def ws_delete_column(sheet, del_column):

    for row_num in range(1, sheet.get_highest_row()+1):
        for col_num in range(del_column, sheet.get_highest_column()+1):

            coordinate = '%s%s' % (get_column_letter(col_num),
                                   row_num)
            adj_coordinate = '%s%s' % (get_column_letter(col_num + 1),
                                       row_num)

            # Handle Styles.
            # This is important to do if you have any differing
            # 'types' of data being stored, as you may otherwise get
            # an output Worksheet that's got improperly formatted cells.
            # Or worse, an error gets thrown because you tried to copy
            # a string value into a cell that's styled as a date.

            if adj_coordinate in sheet._styles:
                sheet._styles[coordinate] = sheet._styles[adj_coordinate]
                sheet._styles.pop(adj_coordinate, None)
            else:
                sheet._styles.pop(coordinate, None)

            if adj_coordinate in sheet._cells:
                sheet._cells[coordinate] = sheet._cells[adj_coordinate]
                sheet._cells[coordinate].column = get_column_letter(col_num)
                sheet._cells[coordinate].row = row_num
                sheet._cells[coordinate].coordinate = coordinate

                sheet._cells.pop(adj_coordinate, None)
            else:
                sheet._cells.pop(coordinate, None)

        # sheet.garbage_collect()

我传递给它我正在使用的工作表和要删除的列号,然后它开始工作。虽然这不完全是您想要的,但我希望这些信息能够帮助您!
编辑:注意到有人对此进行了另一次投票,并认为我应该更新它。Openpyxl中的坐标系统在过去几年中经历了一些变化,引入了_cell中项目的coordinate属性。这也需要进行编辑,否则行将被留空(而不是删除),Excel会在文件出现问题时抛出一个错误。这适用于Openpyxl 2.2.3(未经过后续版本测试)。

4
使用Python中的openpyxl插入行到Excel电子表格,以下代码可以帮助你:
import openpyxl

file = "xyz.xlsx"
#loading XL sheet bassed on file name provided by user
book = openpyxl.load_workbook(file)
#opening sheet whose index no is 0
sheet = book.worksheets[0]

#insert_rows(idx, amount=1) Insert row or rows before row==idx, amount will be no of 
#rows you want to add and it's optional
sheet.insert_rows(13)

对于插入列,openpyxl也有类似的函数,即insert_cols(idx,amount = 1)


2
我编写了一个函数,可以使用openpyxl在电子表格中任意位置插入整个行或整个二维表。

该函数的每一行都有注释,但如果您只想插入一行,请将您的行设置为[row]。例如,如果row = [1,2,3,4,5],则将输入设置为[[1,2,3,4,5]]。如果您希望将此行插入到电子表格的顶行(A1),则Start = [1,1]。

您确实可以像我在底部的示例中看到的那样覆盖文件名。

def InputList(Start, List): #This function is to input an array/list from a input start point; len(Start) must equal 2, where Start = [1,1] is cell 1A. List must be a two dimensional array; if you wish to input a single row then this can be done where len(List) == 1, e.g. List = [[1,2,3,4]]
    x = 0 #Sets up a veriable to go through List columns
    y = 0 #Sets up a veriable to go through List rows
    l = 0 #Sets up a veriable to count addional columns against Start[1] to allow for column reset on each new row
    for row in List: #For every row in List
        l = 0 #Set additonal columns to zero
        for cell in row: #For every cell in row
            ws.cell(row=Start[0], column=Start[1]).value = List[y][x] #Set value for current cell
            x = x + 1 #Move to next data input (List) column
            Start[1] = Start[1] + 1 #Move to next Excel column
            l = l + 1 #Count addional row length
        y = y + 1 #Move to next Excel row
        Start[0] = Start[0] + 1 #Move to next Excel row
        x = 0 #Move back to first column of input data (ready for next row)
        Start[1] = Start[1] - l #Reset Excel column back to orignal start column, ready to write next row

在第7行的开头插入单个行的示例:

from openpyxl import load_workbook
wb = load_workbook('New3.xlsx')
ws = wb.active

def InputList(Start, List): #This function is to input an array/list from a input start point; len(Start) must equal 2, where Start = [1,1] is cell 1A. List must be a two dimensional array; if you wish to input a single row then this can be done where len(List) == 1, e.g. List = [[1,2,3,4]]
    x = 0 #Sets up a veriable to go through List columns
    y = 0 #Sets up a veriable to go through List rows
    l = 0 #Sets up a veriable to count addional columns against Start[1] to allow for column reset on each new row
    for row in List: #For every row in List
        l = 0 #Set additonal columns to zero
        for cell in row: #For every cell in row
            ws.cell(row=Start[0], column=Start[1]).value = List[y][x] #Set value for current cell
            x = x + 1 #Move to next data input (List) column
            Start[1] = Start[1] + 1 #Move to next Excel column
            l = l + 1 #Count addional row length
        y = y + 1 #Move to next Excel row
        Start[0] = Start[0] + 1 #Move to next Excel row
        x = 0 #Move back to first column of input data (ready for next row)
        Start[1] = Start[1] - l #Reset Excel column back to orignal start column, ready to write next row

test = [[1,2,3,4]]
InputList([7,1], test)

wb.save('New3.xlsx')

1
我采用了Dallas的方案,并添加了对合并单元格的支持:
    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
                    )

请查看此代码片段以获取更完整的功能:https://bitbucket.org/snippets/openpyxl/qyzKn - Dallas

0

0

我成功地使用了Dallas's的答案,尽管对于openpyxl 3.0.9,我进行了一些修改。我在这里发布代码,以供其他人在2022年想知道如何做到这一点。

区别在于:

  • 添加导入
  • Cell.TYPE_FORMULA更改为TYPE_FORMULA
  • 在需要的地方添加类型转换,使用str()int()
  • 更新definedNames

我是Python的新手,所以请随时提出任何更改建议,但这是我让它工作的方法。

import copy
import re
from openpyxl.utils import get_column_letter
from openpyxl.cell.cell import TYPE_FORMULA

#https://dev59.com/0mQm5IYBdhLWcg3w2R1J
def insert_rows(self, row_idx, cnt, above=True, copy_style=True, fill_formulae=True):
    """Inserts new (empty) rows into worksheet at specified row index.

    :param self: Worksheet
    :param row_idx: Row index specifying where to insert new rows.
    :param cnt: Number of rows to insert.
    :param above: Set True to insert rows above specified row index.
    :param copy_style: Set True if new rows should copy style of immediately above row.
    :param fill_formulae: Set True if new rows should take on formula from immediately above row, filled with references new to rows.

    Usage:

    * insert_rows(2, 10, above=True, copy_style=False)

    """
    CELL_RE  = re.compile("(?P<col>\$?[A-Z]+)(?P<row>\$?\d+)")

    row_idx = row_idx - 1 if above else row_idx

    def replace(m):
        row = m.group('row')
        prefix = "$" if row.find("$") != -1 else ""
        row = int(row.replace("$",""))
        row += cnt if row > row_idx else 0
        return m.group('col') + prefix + str(row)

    # First, we shift all cells down cnt rows...
    old_cells = set()
    old_fas   = set()
    new_cells = dict()
    new_fas   = dict()
    for c in self._cells.values():

        old_coor = c.coordinate

        # Shift all references to anything below row_idx
        if c.data_type == TYPE_FORMULA:
            c.value = CELL_RE.sub(
                replace,
                c.value
            )
            # Here, we need to properly update the formula references to reflect new row indices
            if old_coor in self.formula_attributes and 'ref' in self.formula_attributes[old_coor]:
                self.formula_attributes[old_coor]['ref'] = CELL_RE.sub(
                    replace,
                    self.formula_attributes[old_coor]['ref']
                )

        # Do the magic to set up our actual shift    
        if c.row > row_idx:
            old_coor = c.coordinate
            old_cells.add((c.row,c.column))
            c.row += cnt
            new_cells[(c.row,c.column)] = c
            if old_coor in self.formula_attributes:
                old_fas.add(old_coor)
                fa = self.formula_attributes[old_coor].copy()
                new_fas[c.coordinate] = fa

    for coor in old_cells:
        del self._cells[coor]
    self._cells.update(new_cells)

    for fa in old_fas:
        del self.formula_attributes[fa]
    self.formula_attributes.update(new_fas)

    # Next, we need to shift all the Row Dimensions below our new rows down by cnt...
    for row in range(len(self.row_dimensions)-1+cnt,row_idx+cnt,-1):
        new_rd = copy.copy(self.row_dimensions[row-cnt])
        new_rd.index = row
        self.row_dimensions[row] = new_rd
        del self.row_dimensions[row-cnt]

    # Now, create our new rows, with all the pretty cells
    row_idx += 1
    for row in range(row_idx,row_idx+cnt):
        # Create a Row Dimension for our new row
        new_rd = copy.copy(self.row_dimensions[row-1])
        new_rd.index = row
        self.row_dimensions[row] = new_rd
        for col in range(1,self.max_column):
            col = get_column_letter(col)
            cell = self[str(col)+str(row)]
            cell.value = None
            source = self[str(col)+str(row-1)]
            if copy_style:
                cell.number_format = source.number_format
                cell.font      = copy.copy(source.font)
                cell.alignment = copy.copy(source.alignment)
                cell.border    = copy.copy(source.border)
                cell.fill      = copy.copy(source.fill)
            if fill_formulae and source.data_type == TYPE_FORMULA:
                s_coor = source.coordinate
                if s_coor in self.formula_attributes and 'ref' not in self.formula_attributes[s_coor]:
                    fa = self.formula_attributes[s_coor].copy()
                    self.formula_attributes[cell.coordinate] = fa
                # 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
                )   
                cell.data_type = TYPE_FORMULA

    # Check for Merged Cell Ranges that need to be expanded to contain new cells
    for cr_idx, cr in enumerate(self.merged_cells.ranges):
        self.merged_cells.ranges[cr_idx] = CELL_RE.sub(
            replace,
            str(cr)
        )

    # Update all defined names
    wb :Workbook = self.parent
    for definedName in wb.defined_names.definedName:
        ref :str = definedName.attr_text
        parts = ref.split("!")
        if parts[0].strip("'") == self.title:
            definedName.attr_text = CELL_RE.sub(replace, ref)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接