删除excel中列为特定值的所有行
# 导入需要用到的相关包
import os
import openpyxl
#定义删除函数
def delete_row(filename, sheet_name, exc_string, column_num):
# 载入工作簿
wb = openpyxl.load_workbook(filename)
print('excel have been opened')
# open the sheet
ws = wb[sheet_name]
print('use the sheet')
for i in range(1, ws.max_row, 1): #从第一行开始到最后一行逐行进行
# print("go the for")
# print(ws.max_row)
# print(ws.cell(row=i, column=column_num).value)
if ws.cell(row=i, column=column_num).value == exc_string: #如果该行第column_num列值为'exc_string'
print(i)
ws.delete_rows(i,1) #则删除该行
print('删除第-',i,'-行成功!')
wb.save(filename='Update.xlsx')
path = os.getcwd()
filename = 'Result1.xlsx'
sheet_name = 'FinalData'
exc_string = 'Sheet3'
column_num = 2
delete_row(filename, sheet_name, exc_string, column_num)