简体   繁体   中英

Search for and delete Excel rows based on a range of values

I have two Excel files, one containing a list of emails of people that need to be removed (ie removal file), and a second Excel file that contains the active list of people (ie roster file). I want to search the email column of the roster file for the list of emails to be removed, and then copy the new list, minus the removed people, to a new file.

I'm looking to achieve this using Python. My code so far is as follows:

from xlrd import open_workbook
import openpyxl

# Open Removal file
book1 = open_workbook('C:\Python27\Delete\Removals.xlsx')
sheet1 = book1.sheet_by_index(0)
search_remove_col = 0

# Open All Candidates file
book2 = open_workbook('C:\Python27\Delete\All Candidates.xlsx')
sheet2 = book2.sheet_by_index(0)
search_worker_col = 4

wb3 = openpyxl.load_workbook('c:\python27\delete\All Candidates.xlsx')
oldlivesheet = wb3.get_sheet_by_name('Live')

# Create a New Roster file
book3 = openpyxl.Workbook()
book3.save('c:\python27\delete\New Roster.xlsx')
book3 = open_workbook('C:\Python27\Delete\New Roster.xlsx')
sheet3 = book3.sheet_by_index(0)
new_worker_col = 4

# Interate through file, looking for worker to remove
for row_sheet1 in range(1, sheet1.nrows):

    workername = sheet1.cell(row_sheet1, search_remove_col).value

    for row_sheet2 in range(1, sheet2.nrows):                
            if sheet2.cell(row_sheet2,search_worker_col).value != workername:
                    sheet3.cell(row_sheet2,new_worker_col).value = sheet2.cell(row_sheet2,search_worker_col).value
                    print row_sheet2
            else:
                    print 'Worker to remove was found!'

book3.save('c:\python27\delete\New Roster.xlsx')

The issue I have is with the line:

sheet3.cell(row_sheet2,new_worker_col).value = sheet2.cell(row_sheet2,search_worker_col).value

This throws an index out of range error. What I need this line of code to do is to copy the worker name into a cell in the New Roster file.

The following approach should work. It first loads all of the removals into a Python set . It then iterates over entries in the All Candidates.xlsx file and if the removal column is found in the removals set it is not written to the new file:

from xlrd import open_workbook
import openpyxl

# Open Removal file and create a set of required removals
book_removals = open_workbook(r'removals.xlsx')
sheet_removals = book_removals.sheet_by_index(0)
search_remove_col = 0
removals = set(sheet_removals.col_values(search_remove_col))

# Open the All Candidates file
book_candidates = open_workbook(r'All Candidates.xlsx')
sheet_candidates = book_candidates.sheet_by_index(0)
search_worker_col = 4

# Create a New Roster file
book_new = openpyxl.Workbook()
sheet_new = book_new.create_sheet(0)

# Iterate through candidates file, looking for removals
for row in range(sheet_candidates.nrows):
    if sheet_candidates.cell(row, search_worker_col).value not in removals:
        sheet_new.append(sheet_candidates.row_values(row))

book_new.save(r'New Roster.xlsx')

Note, use r'c:\\xxxxxxxxx\\xxxxxxxxx.xlsx' for your filenames to avoid problems with using the backslash.

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