Search code examples
pythonexcelopenpyxlworksheet

How to copy a range from one sheet to another as values using openpyxl in python


I have to copy a range from A1:Z100 from sheet onw of workbook 1 to sheet 1 of workbook 2?

My code :

wb = openpyxl.load_workbook('file1.xlsx')
wb1 = openpyxl.load_workbook('file2.xlsx')
sheet = wb["R"]
sheet1 = wb1["Rt"]
sheet1.cell(row=1,column=1).value = sheet.cell(row=1,column=1).value

This is not working properly. How to copy this range to that sheet?


Solution

  • You can try with something like:

    for i in range(1, 100):
        for j in range(1, 26):
            sheet1.cell(row=i,column=j).value = sheet.cell(row=i,column=j).value
    wb1.save('file2.xlsx')