简体   繁体   English

openpyxl - “复制/粘贴”单元格范围

[英]openpyxl - “copy/paste” range of cells

I'm new to Python and I'm trying to adapt some of my VBA code to it using the openpyxl library.我是 Python 的新手,我正在尝试使用 openpyxl 库使我的一些 VBA 代码适应它。 On this particular case, I'm trying to copy 468 rows in a single column from a workbook according to the string in the header and to paste them in another workbook in a particular column that has another specific string as a header.在这种特殊情况下,我试图根据标题中的字符串从工作簿的单个列中复制 468 行,并将它们粘贴到另一个工作簿中的特定列中,该列具有另一个特定字符串作为标题。 I can't simply select the range of cells I want to copy because this is part of a report automation and the headers change positions from file to file.我不能简单地选择要复制的单元格范围,因为这是报表自动化的一部分,并且标题会在文件之间更改位置。

What's the function I need to use to copy each of the 468 cells from one workbook into the 468 cells of the second workbook?我需要使用什么函数将一个工作簿中的 468 个单元格中的每一个复制到第二个工作簿的 468 个单元格中? Or alternatively how can I copy a range of cells and then paste them in another workbook?或者,我如何复制一系列单元格,然后将它们粘贴到另一个工作簿中? Here is my code and I know exactly what's wrong: I'm copying one cell (the last from the first workbook) repeatedly into the 468 cells of the second workbook.这是我的代码,我确切地知道出了什么问题:我将一个单元格(第一个工作簿中的最后一个)重复复制到第二个工作簿的 468 个单元格中。

#!/usr/bin/python3

import pdb
import openpyxl
from openpyxl.utils import column_index_from_string

wb1 = openpyxl.load_workbook('.../Extraction.xlsx')
wb2 = openpyxl.load_workbook('.../Template.xlsx')

ws1 = wb1.active
first_row1 = list(ws1.rows)[0]             #to select the first row (header)
for cell in first_row1:
    if cell.value == "email":
        x = cell.column                    #to get the column
        y = column_index_from_string(x)    #to get the column's index

for i in range(2, 469):
    cell_range1 = ws1.cell(i, y)           #the wrong part

ws2 = wb2.active
first_row2 = list(ws2.rows)[0]
for cell in first_row2:
    if cell.value == "emailAddress":
        w = cell.column
        z = column_index_from_string(w)

for o in range(2, 469):
    cell_range2 = ws2.cell(o, z)
    cell_range2.value = cell_range1.value

path = '.../Test.xlsx'
wb2.save(path)

It is actually quite easy to create such a function:创建这样的函数实际上很容易:

from openpyxl.utils import rows_from_range

def copy_range(range_str, src, dst):

    for row in rows_from_range(range_str):
        for cell in row:
            dst[cell].value = src[cell].value

    return

Note that range_str is a regular string such as "A1:B2" and src and dest both have to be valid sheet objects.请注意, range_str 是一个常规字符串,例如“A1:B2”,并且 src 和 dest 都必须是有效的工作表对象。 However, if you are copying large ranges, this might take a while, as the read/writes seem to be rather time-consuming.但是,如果您要复制大范围,这可能需要一段时间,因为读/写似乎相当耗时。

You may have to flip the input to .cell() , I guess it is .cell(column, row) .您可能需要将输入翻转为.cell() ,我猜它是.cell(column, row) Or just use the keywords .cell(column=z, row=o)或者只使用关键字.cell(column=z, row=o)

You need a dynamic index for both of the row iterators, while keeping the column indices where you found them:您需要为两个行迭代器设置动态索引,同时将列索引保留在您找到它们的位置:

for o in range(2, 469):
    #note the common o for both, could also be o+1 for one if there is an offset
    ws2.cell(o, z).value = ws1.cell(o, y).value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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