2022-11-29

How to move specific cells in an excel file to a new column with openpyxl in python

I am trying to moving some specific cells to a designated location. As shown in the image, would like to move data in cells D3 to E2, D5 to E4,..... so on so for. Is it doable with openpyxl? Any suggestions would be greatly appreciate it!! Click to see the image

Here is what I got so far. It worked per say.

wb=xl.load_workbook(datafile)
ws=wb['Sheet1']

#insert a new column #5
ws.insert_cols(idx=5,amount=1)
wb.save(datafile)

mr=ws.max_row
   
#move cells

for i in range (1,mr+1):
    v=ws.cell(row = i+1,column=4) 
    ws.cell(row=i,column =5).value=v.value

wb.save(datafile)
wb.close

How do I skip a row? I only wanted to copy values in every other row over.



No comments:

Post a Comment