Insert row into Excel spreadsheet using openpyxl in Python

前端 未结 11 2289
既然无缘
既然无缘 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:31

    I've written a function which will both insert an entire row anywhere you want in a spreadsheet, or an entire 2D table, with openpyxl.

    Every row of the function is explained with a comment but if you want to just insert a single row, just make your row equal to [row]. i.e. if row = [1,2,3,4,5] then set your input to [[1,2,3,4,5]]. If you want this row to be inserted into the top row of your spreadsheet (A1) then Start = [1,1].

    You can indeed overwrite the file name as see you can with my example at the bottom.

    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
    

    Example with single row being inserted at start of 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')
    

提交回复
热议问题