Insert column using openpyxl

Shawn picture Shawn · Apr 5, 2013 · Viewed 20.1k times · Source

I'm working on a script that modifies an existing excel document and I need to have the ability to insert a column between two other columns like the VBA macro command .EntireColumn.Insert.

Is there any method with openpyxl to insert a column like this?
If not, any advice on writing one?

Answer

Naghmeh picture Naghmeh · Mar 22, 2018

Here is an example of a much much faster way:

    import openpyxl
    wb = openpyxl.load_workbook(filename)
    sheet = wb.worksheets[0]
    //this statement inserts a column before column 2
    sheet.insert_cols(2)
    wb.save("filename.xlsx")