根据值范围搜索并删除 Excel 行

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

我有两个 Excel 文件,一个包含需要删除的人员的电子邮件列表(即删除文件),第二个 Excel 文件包含活动人员列表(即花名册文件)。我想在花名册文件的电子邮件列中搜索要删除的电子邮件列表,然后将新列表(减去已删除的人员)复制到新文件中。

我希望使用 Python 来实现这一点。到目前为止我的代码如下:

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

我遇到的问题是:

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

这将引发 索引超出范围 错误。我需要这行代码做的是将工人姓名复制到新名册文件中的单元格中。

以下方法应该有效。它首先将所有删除内容加载到 Python set 中。然后它遍历 All Candidates.xlsx 文件中的条目,如果在删除集中找到删除列,则不会将其写入新文件:

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

请注意,使用 r'c:\xxxxxxxxx\xxxxxxxxx.xlsx' 作为文件名以避免使用反斜杠时出现问题。