根据一系列值搜索和删除Excel行

时间:2015-10-13 07:59:17

标签: python excel range delete-row

我有两个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

这会引发索引超出范围错误。我需要这行代码来将工作者名称复制到New Roster文件中的单元格中。

1 个答案:

答案 0 :(得分:0)

以下方法应该有效。它首先将所有删除加载到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'作为文件名,以避免使用反斜杠时出现问题。