Home > Blockchain >  How can I copy a range of data in excel (B2:B15) down with openpyxl?
How can I copy a range of data in excel (B2:B15) down with openpyxl?

Time:05-13

I'm a Python beginner and I made a script to extract data into an xlsx file with openpyxl but I'm stuck with a problem which seems pretty easy. I'd like to copy(not move) the yellow data to the green cells in the following Excel file: 1

Or said in another way, I want to copy B2:B15 to B16:B29 within my python script. I don't need help with the import of openpyxl or creation of my ws it´s just the specific code that allows to copy the B2:B15 to B16:B29 which I don't get.

I appreciate any help! Ty so much.

I tried the following which didn´t work at all:

for row in range(16,29):
    for col in range(1,2):
        char = get_column_letter(col)
        ws[char   str(row)] = ws(['B2:B15'].value)

CodePudding user response:

If ws is your worksheet, then the code to do that is...

for row in range(16,30):
    ws.cell(row=row, column=2).value = ws.cell(row=row-14, column=2).value

Updated below for doing this multiple times

Repeat = 5  #Indicate how many times you want to paste the 15 rows
for cycle in range(1, Repeat 1):
    for row in range(14):
        ws.cell(row=row 2 (cycle)*14, column=2).value = ws.cell(row=row 2, column=2).value
  • Related