简体   繁体   中英

Openpyxl: How to copy a row after checking if a cell contains specific value

I have a worksheet that is updated every week with thousands of rows and would need to transfer rows from this worksheet after filtering. I am using the current code to find the cells which has the value I need and then transfer the entire row to another sheet but after saving the file, I get the "IndexError: list index out of range" exception.

The code I use is as follows:

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

The original code I used that used to work is below and has to be modified because of the large files which causes MS Excel not to open them (over 40mb).

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

Thanks.

I'm not entirely sure what you're trying to do but I suspect the problem is that you have nested your copy loop.

Try the following:

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

Use a list to hold the items in each column for the particular row. Then append the list to your 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)

Question : I get the "IndexError: list index out of range" exception.


I get, from ws1.iter_rows(n)

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

and from ws2.append(row2) .

 ValueError: Cells cannot be copied from other worksheets 

The Reason are row2 does hold a list of Cell objects instead of a list of Values


Question : ... need to transfer rows from this worksheet after filtering

The following do what you want, for instance:

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

Tested with Python: 3.4.2 - openpyxl: 2.4.1 - LibreOffice: 4.3.3.2

I was able to solve this with lists for my project.

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

Tested with: Python 3.7 openpyxl 2.5.4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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