openpyxl如何实现复制粘贴到指定行和列?

我想在下图excel中将G和H分别复制粘贴到B和C列

img

我目前先写了将G列复制粘贴B列的代码:

for row in bs_ws.iter_rows(min_row=1,min_col=7,max_col=8,values_only=True):
    for cell in bs_ws['B']:
        cell.value = row[0]

但是结果并不是我想要的:

img


for i in range(1,sheet.max_row+1):#这里你看要复制多少行,我习惯用sheet,你可以用ws
    sheet[f'B{i}'].value=sheet[f'G{i}'].value

https://blog.csdn.net/heianduck/article/details/122430426?spm=1001.2014.3001.5502
我写的这个, 你参考下?

你可以参考下:
假如复制excel第2列的2-8行 到 第3列1-7行

import xlrd
book = xlrd.open_workbook("myfile.xls")
print("The number of worksheets is {0}".format(book.nsheets))
print("Worksheet name(s): {0}".format(book.sheet_names()))
sh = book.sheet_by_index(0)
print("{0} {1} {2}".format(sh.name, sh.nrows, sh.ncols))
print("Cell D30 is {0}".format(sh.cell_value(rowx=29, colx=3)))
for rx in range(sh.nrows):
print(sh.row(rx))

将你代码改下试试,使用iter_cols列遍历,每次返回一列的内容,再复制到你指定列里面:

for row in bs_ws.iter_cols(min_row=1,min_col=7,max_col=8,values_only=True):
for index,cell in enumerate(bs_ws['B']):
cell.value = row[index]