Openpyxl:检查单元格是否包含特定值后如何复制行

时间:2017-07-05 10:44:07

标签: python openpyxl

我有一个工作表,每周更新数千行,并且需要在过滤后从此工作表中传输行。我正在使用当前代码来查找具有我需要的值的单元格,然后将整行传输到另一个工作表,但在保存文件后,我得到“IndexError:list index out of range”异常。

我使用的代码如下:

import openpyxl

wb1 = openpyxl.load_workbook('file1.xlsx')
wb2 = openpyxl.load_workbook('file2.xlsx')

ws1 = wb1.active
ws2 = wb2.active

for row in ws1.iter_rows():
    for cell in row:
        if cell.value == 'TrueValue':
            n = 'A' + str(cell.row) + ':' + ('GH' + str(cell.row))
            for row2 in ws1.iter_rows(n):
                ws2.append(row2)

wb2.save("file2.xlsx")

我以前使用的原始代码在下面并且必须进行修改,因为大文件会导致MS Excel无法打开它们(超过40mb)。

n = 'A3' + ':' + ('GH'+ str(ws1.max_row))
for row in ws1.iter_rows(n):
    ws2.append(row)

感谢。

4 个答案:

答案 0 :(得分:2)

我不完全确定你要做什么,但我怀疑问题是你已经嵌套了你的复制循环。

尝试以下方法:

row_nr = 1
for row in ws1:
    for cell in row:
        if cell.value == "TrueValue":
            row_nr = cell.row
            break
    if row_nr > 1:
        break

for row in ws1.iter_rows(min_row=row_nr, max_col=190):
    ws2.append((cell.value for cell in row))

答案 1 :(得分:1)

使用列表来保存特定行的每列中的项目。 然后将列表附加到 ws2

...

def iter_rows(ws,n):  #produce the list of items in the particular row
        for row in ws.iter_rows(n):
            yield [cell.value for cell in row]

for row in ws1.iter_rows():
    for cell in row:
        if cell.value == 'TrueValue':
            n = 'A' + str(cell.row) + ':' + ('GH' + str(cell.row))
            list_to_append = list(iter_rows(ws1,n))
            for items in list_to_append:
                ws2.append(items)

答案 2 :(得分:1)

  

问题:我得到" IndexError:列表索引超出范围"例外。

  

我从ws1.iter_rows(n)

获得
UserWarning: Using a range string is deprecated. Use ws[range_string]
     

来自ws2.append(row2)

ValueError: Cells cannot be copied from other worksheets
     

原因row2确实包含 list of Cell objects 而不是list of Values

  

问题:...过滤后需要从此工作表中传输行

以下是您想要的,例如:

# If you want to Start at Row 2 to append Row Data
# Set Private self._current_row to 1
ws2.cell(row=1, column=1).value = ws2.cell(row=1, column=1).value

# Define min/max Column Range to copy
from openpyxl.utils import range_boundaries
min_col, min_row, max_col, max_row = range_boundaries('A:GH')

# Define Cell Index (0 Based) used to Check Value
check = 0 # == A

for row in ws1.iter_rows():
    if row[check].value == 'TrueValue':
        # Copy Row Values
        # We deal with Tuple Index 0 Based, so min_col must have to be -1
        ws2.append((cell.value for cell in row[min_col-1:max_col]))

使用Python测试:3.4.2 - openpyxl:2.4.1 - LibreOffice:4.3.3.2

答案 3 :(得分:0)

我能够使用我的项目列表来解决这个问题。

import openpyxl
#load data file
wb1 = openpyxl.load_workbook('original.xlsx')
sheet1 = wb1.active
print("loaded 1st file")    
#new template file
wb2 = openpyxl.load_workbook('blank.xlsx') 
sheet2 = wb2.active
print("loaded 2nd file")
header = sheet1[1:1] #grab header row
listH =[]
for h in header:
    listH.append(h.value)
sheet2.append(listH)
colOfInterest= 11 # this is my col that contains the value I'm checking against
for rowNum in range(2, sheet1.max_row +1):  #iterate over each row, starting with 2 to skipping header from original file
    if sheet1.cell(row=rowNum, column=colOfInterest).value is not None: #interested in non blank values in column 11
        listA = [] # list which will hold my data
        row = sheet1[rowNum:rowNum] #creates a tuple of row's data
        #print (str(rowNum))  # for debugging to show what rows are copied
        for cell in row:  # for each cell in the row
            listA.append(cell.value) # add each cell's data as an element in the list
        if listA[10]  == 1:  # condition1 I'm checking for by looking up the index in the list
            sheet2.append(listA)  # appending the sheet2's next available row
        elif listA[10] > 1:  # condition2 I'm checking for by looking up the index in the list
            # do something else and store it in bar
            sheet2.append(bar) # appending the sheet2's next available row

print("saving file...")
wb2.save('result.xlsx')  # save file
print("Done!")

经过以下测试:Python 3.7 openpyxl 2.5.4