Insert row into Excel spreadsheet using openpyxl in Python

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

    To insert row into Excel spreadsheet using openpyxl in Python

    Below code can help you :-

    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)
    

    For inserting column also openpyxl have similar function i.e.insert_cols(idx, amount=1)

提交回复
热议问题