使用python、pandas或其他方法将多个列从excel工作表的file1复制到file2其他excel工作表的其他列/行

2024-06-26 13:30:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从文件1(I)中复制第9个单元格后第I列的整行数据,并使用python将数据从第4行粘贴到文件2中的D列中。同样,我需要将文件1从b4复制到b8,作为J7复制到j11


Tags: 文件数据粘贴b4b8j7j11
1条回答
网友
1楼 · 发布于 2024-06-26 13:30:32

我尝试了以下代码:

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)) 

相关问题 更多 >