我想从源Excel中选择一系列单元格粘贴到目标Excel中。通过下面的代码,我可以做到这一点(我需要从E6:E15中提取数据)。然而,这些数据被粘贴到目标工作簿中的单元格E6:E15中。
我如何将数据粘贴到不同的单元格中?例如F6:F15?
代码。
import openpyxl
wbo = openpyxl.load_workbook(r'file_origin.xlsx', read_only=True)
wbd = openpyxl.load_workbook(r'file_destination.xlsx')
wso= wb['Sheet1']
wsd= wb1['Sheet1']
for row in wso['E6':'E15']:
for cell in row:
wsd[cell.coordinate].value = cell.value
IIUC,
import openpyxl
wbo = openpyxl.load_workbook(r'file_origin.xlsx', read_only=True)
wbd = openpyxl.load_workbook(r'file_destination.xlsx')
#attach the ranges to the workbooks
wso= wbo['Sheet1']['E6':'E15']
wsd= wbd['Sheet1']['F6':'F15']
#step1 : pair the rows
#since it is the same number of rows
#row 6 will pair with 6, 7 with 7 ,....
for row1,row2 in zip(wso,wsd):
#within the row pair, pair the cells
for cell1, cell2 in zip(row1,row2):
#assign the value of cell 1 to the destination cell 2 for each row
cell2.value = cell1.value
#save document
wbd.save('destination.xlsx')