在openpyx中迭代和移动单元格的动态范围

2024-04-20 11:40:35 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是我想要完成的:

1)插入x个新列
2) 列(u)中
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)

尝试

^{pr2}$

这显然行不通。 我需要范围的选择是动态的;比如在迭代中

这就是我试图实现的可视化

enter image description here

enter image description here

enter image description here

enter image description here

对于该列中的所有值,以此类推。


Tags: columnsinforws情况loadxlsxactive
1条回答
网友
1楼 · 发布于 2024-04-20 11:40:35

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

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)

相关问题 更多 >