Home > Back-end >  Hi All, I'm trying to copy paste data from Macro excel file to another Excel file but the thing
Hi All, I'm trying to copy paste data from Macro excel file to another Excel file but the thing

Time:12-12

I am using openpyxl library here.

Q1. How should I tweak it so that it can take macro file data for File 1?

Q2. how to copy paste this data in different cells (Than what is specified in the "i" & "j" variables)

In the below code, I am able to copy paste cells of file 1 in the same cell locations for file 2 Which is = 4 rows (row numbers- 1 to 4) having 4 columns (Column numbers- B to E)

However, I want to paste my data in file 2 which is in the 4 rows (Row number 8 to 11) & columns (Column numbers- G to J)

Pls see below the code I'm using currently-

import openpyxl as xl

#Copy from file1
file1="D:\\Python Excel copy-paste\\New Excel\\D1.xlsx"
wb1=xl.load_workbook(file1)
ws1=wb1["data"]

#paste to file 2
file2="D:\\Python Excel copy-paste\\New Excel\\D2.xlsx"
wb2=xl.load_workbook(file2)
ws2=wb2["here"]

for i in range(1,5):
    for j in range(2,6):
        
        ws2.cell(row=i,column=j).value=ws1.cell(row=i,column=j).value
                
wb2.save(file2)

It is pasting data in the same cells of file 2, I want to paste this data in different cells of file 2. Thank you in advance :)

CodePudding user response:

You just need to make a shift from the origin coordinates.

try this :

for i in range(1,5):
    for j in range(2,6):
        ws2.cell(row=i 4,column=j 4).value=ws1.cell(row=i,column=j).value
  • Related