遍历并移动动态范围的单元格openpyxl

时间:2019-05-22 18:27:50

标签: python excel openpyxl

这是我要完成的事情:

1)插入x个新列
2)对于column_i中的单元格(n至k)
3)移动单元格值x列和y行

这是我到目前为止所拥有的:

#opening book 
wb=openpyxl.load_workbook('testdat.xlsx')
ws=wb.active

#inserting X columns
    for i in range(5,11):
        ws.insert_cols(i)

尝试

    #iterate through cells and move them
for k in range(3,8):
 ws.move_range(C{k}:D{k}, cols=2, rows=k-1

显然不起作用。 我需要选择动态范围;如在迭代中

这就是我要实现的可视化

enter image description here

enter image description here

enter image description here

enter image description here

该列中所有值的依此类推。

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,这非常简单:

from openpyxl.worksheet.cell_range import CellRange
cr = CellRange(min_col=2, max_col=3, min_row=3, max_row=3)
ws.move_range(cr, rows=-1, cols=2)
相关问题