excel 批量删除大量关键字

excel 自带的替换功能每次只能替换一个关键字,还不支持 regex,vba 我不会,也不想学,所以还是用 python

关键字按行写到 keyword.txt 中:

hell\s*o
av
讨厌
hate

然后是 py 脚本 excel_clear_word.py:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import re
import openpyxl


with open('keyword.txt', 'r') as f:
    lines = [line.strip() for line in f]


def filter_value(s):
    for reg in lines:
        s = re.sub(reg, '', s, flags=re.IGNORECASE)
    return s



def main():
    file_path = sys.argv[1]
    wrkbk = openpyxl.load_workbook(file_path)
    sheet = wrkbk.active

    # colC = sheet['C'] # 第 C 列
    # col_range = ws['C:D']
    # row3 = sheet[3] # 第 3 行
    # row_range = ws[5:10]
    # sheet.max_column
    # sheet.max_row
    # cell = sheet.cell(row=1, column=1)
    # cell = sheet['A1']
    # sheet.iter_cols()
    for row in sheet.iter_rows(min_row=1, min_col=1):
        for cell in row:
            if cell.value and cell.data_type == 's':
                cell.value = filter_value(str(cell.value))
    wrkbk.save(file_path)

if __name__ == '__main__':
    main()

posted on 2023-03-09 17:12  明天有风吹  阅读(349)  评论(0编辑  收藏  举报

导航

+V atob('d2h5X251bGw=')

请备注:from博客园