python - Insert column using openpyxl -


i'm working on script modifies existing excel document , need have ability insert column between 2 other columns vba macro command .entirecolumn.insert.

is there method openpyxl insert column this?
if not, advice on writing one?

haven't found .entirecolumn.insert in openpyxl.

first thought coming mind insert column manually modifying _cells on worksheet. don't think it's best way insert column works:

from openpyxl.workbook import workbook openpyxl.cell import get_column_letter, cell, column_index_from_string, coordinate_from_string  wb = workbook() dest_filename = r'empty_book.xlsx' ws = wb.worksheets[0] ws.title = "range names"  # inserting sample data col_idx in xrange(1, 10):     col = get_column_letter(col_idx)     row in xrange(1, 10):         ws.cell('%s%s' % (col, row)).value = '%s%s' % (col, row)  # inserting column between 4 , 5 column_index = 5 new_cells = {} ws.column_dimensions = {} coordinate, cell in ws._cells.iteritems():     column_letter, row = coordinate_from_string(coordinate)     column = column_index_from_string(column_letter)      # shifting columns     if column >= column_index:         column += 1      column_letter = get_column_letter(column)     coordinate = '%s%s' % (column_letter, row)      # it's important create new cell object     new_cells[coordinate] = cell(ws, column_letter, row, cell.value)  ws._cells = new_cells wb.save(filename=dest_filename) 

i understand solution ugly hope it'll think in right direction.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -