简体   繁体   中英

copy multiple columns from file1 of an excel sheet to other column/row of file2 other excel sheet using python , use pandas or anything else

I need to copy from file1 (I), from Ith column entire row after 9th cell and paste the data into D column from row 4 into file 2 using python. Also again I need to copy from file1 from b4 to b8 into file2 as J7 to j11.

i tried this code:

import openpyxl as xl
from openpyxl import load_workbook
import pandas as pd

# opening the source excel file 
filename1 ="elementsheetsample.xlsx"
wb1 = xl.load_workbook(filename1) 
ws1 = wb1['Sheet1']

# opening the destination excel file  
filename2 ="filledelementsheetsample.xlsx"   
wb2 = xl.load_workbook(filename2) 
ws2 = wb2['INACTIVE Sheet']

i1=9 #I row in file1
i2=4  #D row in file2



# calculate total number of rows and 
# columns in source excel file 
mr = ws1.max_row 
mc = ws1.max_column 


# copying the cell values from source 

if i1==2:
    if i2==2:
        for j1 in range (2, mc +1):
            for j2 in range (2, mc + 1): 
                    c = ws1.cell(row = i1, column = j1) 
                    ws2.cell(row = i2, column = j2).value = c.value

updatedtx = "updatedtx2.xlsx"
# saving the destination excel file 
wb2.save(str(updatedtx)) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM