按行筛选Excel

假设你有如下的一个Excel表(可以直接复制粘贴到Excel表中使用),你只关心如何获取到张三与李四之间的交易往来,该如何做呢?
image.png
脚本提供了2种模式,行扫描模式与列扫描模式:

列扫描模式:

假如你只关心第4列中,张三与李四的交易,你可以使用此模式。
此模式将从上到下依次读取第4列中有无匹配的内容,一旦匹配,就将这一行数据标记起来,并单独保存

  1. 已知第4列有13个“李四”

image.png

  1. 开始跑脚本

3.gif

行扫描模式:

脚本会逐行检测数据,一旦匹配,就将这一行数据标记起来,并单独保存

  1. 搜索发现Excel表中,李四出现了17次

image.png

  1. 开始跑脚本

4.gif
最终结果如下
image.png
image.png

脚本如下:

import os
import openpyxl
from openpyxl.styles import PatternFill


# 列扫描模式
def choice1(excel_path1):
    print("请输入关键字(输入quit结束输入):")
    word = []  # 存储用户输入的内容
    while True:
        char = input()
        char = char.strip()
        # 下面的两个if主要是判断输入的内容中是否:某行是空白,某行用逗号分割了字符串(主要适用于多个tid)
        if char == '':
            continue
        if 'quit' in char:
            break
        word.append(char)
    workbook = openpyxl.load_workbook(excel_path1)  # 返回一个workbook数据类型的值
    sheet = workbook.active  # 获取活动表
    print('> 当前活动表是:' + str(sheet))
    workbook.create_sheet('提取结果')  # 创建一个新的sheet表,存储匹配上的信息
    sheet2 = workbook['提取结果']
    rows = sheet.max_row  # 获取行数,值是int型

    you_have_column = int(input("> 你想检测第几列的数据:"))  # 已有数据的列
    count = 0
    for i in range(1, rows + 1):
        if str(sheet.cell(i, you_have_column).value) in word:  # 如果单元格中的值在指定列中找到了(二者内容必须完全一致)
            # print(str(sheet.cell(i, you_have_column).value))
            count += 1
            choice_cell = sheet[i]  # 选中当前行
            for j in choice_cell:  # 为此行中的每一个单元格填充颜色
                pattern_fill = PatternFill(fill_type="solid", fgColor="FFFF00")  # 填充黄底高亮
                j.fill = pattern_fill

            # 把这些检测的数据整行复制到另一个sheet表“提取结果”
            # 获取sheet表“提取结果”中第1行的单元格值
            cell_value = sheet2.cell(row=1, column=1).value
            # 如果第1行有值,就往下找空行
            if cell_value:
                row = 2
                while sheet2.cell(row=row, column=1).value:
                    row += 1
            else:  # 如果第1行没有值,就覆盖
                row = 1

            # 复制sheet1中敏感行的数据到sheet表“提取结果”中对应的行
            for column in range(1, sheet.max_column + 1):
                sheet2.cell(row=row, column=column).value = sheet.cell(row=i, column=column).value
    if count == 0:
        print("> 未在第{}列中发现你要检测的内容,".format(you_have_column))
    else:
        print("> 在第{}列中发现你要检测的内容共计{}条,导出结果在桌面".format(you_have_column, count))
        desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')
        path_result = os.path.join(desktop_path, 'result.xlsx')
        workbook.save(path_result)  # 保存excel表
    workbook.close()  # 关闭Excel表格
    print('> 操作结束')


# 行扫描模式
def choice2(excel_path2):
    print("请输入关键字(输入quit结束输入):")
    word = []  # 存储用户输入的内容
    while True:
        char = input()
        char = char.strip()
        # 下面的两个if主要是判断输入的内容中是否:某行是空白,某行用逗号分割了字符串(主要适用于多个tid)
        if char == '':
            continue
        if 'quit' in char:
            break
        word.append(char)
    workbook = openpyxl.load_workbook(excel_path2)  # 返回一个workbook数据类型的值
    sheet = workbook.active  # 获取活动表
    print('> 当前活动表是:' + str(sheet))
    workbook.create_sheet('提取结果')  # 创建一个新的sheet表,存储匹配上的信息
    sheet2 = workbook['提取结果']
    rows = sheet.max_row  # 获取行数,值是int型
    count = 0

    for i in range(1, rows + 1):
        cell = sheet[i]  # 获取每一行的数据
        for ii in cell:  # 遍历这一行的数据
            if str(ii.value) in word:  # 如果某个单元格中的值匹配关键字了(二者内容必须完全一致)
                count += 1
                # print(str(sheet.cell(i, you_have_column).value))
                choice_cell = sheet[i]  # 选中当前行
                for j in choice_cell:  # 为此行中的每一个单元格填充颜色
                    pattern_fill = PatternFill(fill_type="solid", fgColor="FFFF00")  # 填充黄底高亮
                    j.fill = pattern_fill

                # 把这些检测的数据整行复制到另一个sheet表“提取结果”
                # 获取sheet表“提取结果”中第1行的单元格值
                cell_value = sheet2.cell(row=1, column=1).value
                # 如果第1行有值,就往下找空行
                if cell_value:
                    row = 2
                    while sheet2.cell(row=row, column=1).value:
                        row += 1
                else:  # 如果第1行没有值,就覆盖
                    row = 1

                # 复制sheet1中敏感行的数据到sheet表“提取结果”中对应的行
                for column in range(1, sheet.max_column + 1):
                    sheet2.cell(row=row, column=column).value = sheet.cell(row=i, column=column).value

                break
    if count == 0:
        print("> 未在Excel表中发现你要检测的内容,")
    else:
        print("> 发现你要检测的内容共计{}条,导出结果在桌面".format(count))
        desktop_path = os.path.join(os.path.expanduser("~"), 'Desktop')
        path_result = os.path.join(desktop_path, 'result.xlsx')
        workbook.save(path_result)  # 保存excel表
    workbook.close()  # 关闭Excel表格
    print('> 操作结束')


print('''
假设你手里有个Excel表,你只关心部分数据,并希望获取这些数据整行的信息,可以使用此脚本。
脚本有2中扫描模式:
1. 列扫描模式:
脚本会检测指定列中的数据,一旦匹配,就将这一行数据标记起来,并单独保存

2. 行扫描模式:
脚本会逐行检测数据,一旦匹配,就将这一行数据标记起来,并单独保存
''')
excel_path = input("请输入Excel表的绝对路径:")
# excel_path = r'C:\Users\asuka\Desktop\33.xlsx'
while True:
    user_input = input('我选择:列扫描模式【1】推荐,行扫描模式【2】:')
    if user_input in ["1", "2"]:
        break
    else:
        print("输入无效,请重新输入。")

# 用户输入的是1或2,可以进行下一步操作
if user_input == "1":
    choice1(excel_path)
else:
    choice2(excel_path)

猜你喜欢

转载自blog.csdn.net/weixin_44288604/article/details/130205340