美文网首页
用python搜索文件名并在其内容中查找指定字符串,txt,wo

用python搜索文件名并在其内容中查找指定字符串,txt,wo

作者: 旭日_太阳的后裔 | 来源:发表于2023-04-19 13:08 被阅读0次

电脑上的搜索功能无法对文件内容进行搜索,写了一个可以同时搜索文件名和文件内容的python 脚本。支持正则表达式匹配。

先针对文本文件、word、ppt、pdf、Excel 分别够建了一个函数,最后整合函数中调用,进行搜索匹配。

我的测试目录下有这么几个文件:


微信图片_20230420130404.png

搜索文本文件

构建一个文本文件内容查找函数

import os
import re
#构建一个文本文件内容查找函数
def search_text(file_path, search_str):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            lines = f.readlines()
            total_count = 0
            first_line = None
            for i, line in enumerate(lines):
                if re.search(search_str, line):
                    total_count += 1
                    if first_line is None:
                        first_line = i + 1
            if total_count > 0:
                print(f'{file_path}: 首次行: {first_line},  共匹配:{total_count}')          
    except UnicodeDecodeError:
        with open(file_path, 'r', encoding='ISO-8859-1') as f:
            lines = f.readlines()
            total_count = 0
            first_line = None
            for i, line in enumerate(lines):
                if re.search(search_str, line):
                    total_count += 1
                    if first_line is None:
                        first_line = i + 1
            if total_count > 0:
                print(f'{file_path}: 首次行: {first_line},  共匹配:{total_count}') 

测试一下search_text

file_path = "E:\shell\python\search_file_and_content\example\Tsub_defined_marker_heatmap.r"
search_str = "mark.*"
search_str = re.compile(search_str,re.IGNORECASE)
search_text(file_path, search_str)

#E:\shell\python\search_file_and_content\example\Tsub_defined_marker_heatmap.r: 首次行: 1,  共匹配:17

搜索word文件

构建一个word文件内容查找函数

import re
import docx

def search_docx(file_path, search_str):
    doc = docx.Document(file_path)
    pattern = re.compile(search_str)
    total_count = 0
    first_page = None
    for i, para in enumerate(doc.paragraphs):
        para_text = para.text
        matches = pattern.findall(para_text)
        if matches:
            total_count += len(matches)
            if first_page is None:
                    first_page = i + 1
    if total_count > 0:
        print(f"{file_path}  首次行: {first_page}  共匹配:{total_count} 次。")

测试一下search_docx

file_path = "E:\shell\python\search_file_and_content\example\cellphoneDB可视化进阶-详细画图.docx"
search_str = "li.*nd"
search_str = re.compile(search_str,re.IGNORECASE)
search_docx(file_path, search_str)

#E:\shell\python\search_file_and_content\example\cellphoneDB可视化进阶-详细画图.docx  首次行: 13  共匹配:10 次。

搜索ppt文件

构建PPT文件内容查找函数

import os
import re
from pptx import Presentation

def search_pptx_file(file_path, search_str):
    """在指定ppt文件中查找指定字符串,找到则输出该文件名、第一次匹配到的页码,以及总共匹配到多少次。要求输出结果在同一行显示。
    注意指定字符串可能为正则表达式。
    """
    prs = Presentation(file_path)
    count = 0
    first_page = -1
    for index, slide in enumerate(prs.slides, start=1):
        for shape in slide.shapes:
            if shape.has_text_frame:
                text_frame = shape.text_frame
                if search_str:
                    matches = re.findall(search_str, text_frame.text)
                    if matches:
                        count += len(matches)
                        if first_page == -1:
                            first_page = index
    if count > 0:
        file_path = os.path.basename(file_path)
        print(f"{file_path}  首次页码: {first_page}  共匹配: {count} 次")

测试一下search_pptx_file

file_path = "E:\shell\python\search_file_and_content\example\paper_PPT文献分享.pptx"
search_str = "single.*cell"
search_str = re.compile(search_str,re.IGNORECASE)
search_pptx_file(file_path, search_str)

## paper_PPT文献分享.pptx  首次页码: 1  共匹配: 1 次

搜索pdf文件

构建pdf文件内容查找函数

import re
import PyPDF2
#要求PyPDF2 3.0.0或以上,因为好几个函数名与低版本都不同了
def search_pdf_file(file_path, search_str):
    with open(file_path, 'rb') as f:
        pdf_reader = PyPDF2.PdfReader(f)
        matches = 0
        first_page = None
        for page_num in range(len(pdf_reader.pages)):
            page_obj = pdf_reader.pages[page_num]
            text = page_obj.extract_text()
            if re.search(search_str, text):
                matches += 1
                if first_page is None:
                    first_page = page_num + 1
        if matches > 0:
            print(f"{file_path}  首次页码: {first_page}  共匹配: {matches} 次")

测试一下search_pdf_file

file_path = r"E:\shell\python\search_file_and_content\example\2.Dotplot_TSubtype_marker.pdf"
#这里遇到了一个以数字开头的文件,Python 会把以数字开头的字符串当成数字来解析
#r 字符串前缀可以很好地解决这个问题

search_str = ".*FOXP3"
search_str = re.compile(search_str,re.IGNORECASE)
search_pdf_file(file_path, search_str)


## E:\shell\python\search_file_and_content\example\2.Dotplot_TSubtype_marker.pdf  首次页码: 1  共匹配: 1 次

搜索Excel文件

构建Excel文件内容查找函数

import openpyxl 
def search_xls_file(file_path, search_str):
    wb = openpyxl.load_workbook(file_path)
    for sheet_name in wb.sheetnames:
        sheet = wb[sheet_name]
        total_count = 0
        fist_cell = None
        for row in sheet.rows:
            for cell in row:
                if isinstance(cell.value, str) and re.search(search_str, cell.value):
                    match = re.search(search_str, cell.value)
                    total_count += 1
                    if fist_cell is None:
                        fist_cell = cell.coordinate
        if total_count > 0:
            print(f'{file_path}: 首次单元格: {fist_cell}  共匹配:{total_count} 次')

测试一下search_xls_file

file_path = r"E:\shell\python\search_file_and_content\example\Tsub_Marker_for_heatmap.xlsx"
search_str = ".*FOXP3"
search_str = re.compile(search_str,re.IGNORECASE)
search_xls_file(file_path, search_str)

## E:\shell\python\search_file_and_content\example\Tsub_Marker_for_heatmap.xlsx: 首次单元格: B18  共匹配:1 次

整合为一个函数

将上面的函数整合,实现文件查找,并可以对内容进行搜索

def search_files(directory, keyword, search_str):
    keyword = re.compile(keyword, re.IGNORECASE)
    search_str = re.compile(search_str, re.IGNORECASE)
    
    for root, dirs, files in os.walk(directory):
        for file in files:
            if re.search(keyword, file):
                file_path = os.path.join(root, file)
                if file.endswith('.docx'):
                    search_docx(file_path, search_str)
                elif file.endswith('.pptx'):
                    search_pptx_file(file_path, search_str)
                elif file.endswith('.pdf'):
                    search_pdf_file(file_path, search_str)
                elif file.endswith('.xlsx'):
                    search_xls_file(file_path, search_str)
                else:
                    search_text(file_path, search_str)

测试整合函数search_files

directory = r"E:\shell\python\search_file_and_content\example"
keyword = ".*marker.*heatmap"
search_str = "Seu.*t"
search_files(directory, keyword, search_str)

## E:\shell\python\search_file_and_content\example\Tsub_defined_marker_heatmap.r: 首次行: 2,  共匹配:1
## E:\shell\python\search_file_and_content\example\subdir1\3.4.4.celltype_marker_heatmap_dotplot.r: 首次行: 2,  共匹配:2
## E:\shell\python\search_file_and_content\example\subdir1\subdir2\2_marker heatmap_in_single_cell.r: 首次行: 23,  共匹配:1

相关文章

  • Xcode 搜索技巧

    全局搜索文件名字 搜索当前文件的字符串 全局搜索字符串 Find: 是查找内容 Replace: 是可以将查找的内...

  • 删除 N 天前文件

    -type f:指定查找的为文件,而非文件夹或文件链接等 -name '*.txt': 指定查找的文件名的模式 -...

  • 3-2(linux-grep、cp、mv、rmdir、mkdir

    grep 功能:在文件中查找字符串grep aa test.txt在test.txt中查找aa字符串 cp 复制文...

  • python循环查找包含指定字符串的文件

    周末学了点 python, 今天刚好需要查找目录下包含指定字符串的文件,mac 下用 grep 查找如下 不过非常...

  • MAC 文件查找,日志查找

    根据文件内容查找 如果目标范围较大 通过指定文件名的方式 查找关键字,并查看附近的内容 只显示包含内容的文件名 ...

  • PHP字符串处理函数:strpos() -- 内置函数

    函数简介 在字符串中查找指定的字符串,查找方式为 顺序 查找。即从字符串“头”找到字符串“尾”。查找成功:返回指定...

  • 2019-04-22awk使用fangfa

    环境 用grep查找ildboy.txt文件'oldb.y'未知内容 用sed查找ildboy.txt文件'old...

  • Linux--find和grep

    1.根据文件属性查看-find find 搜索路径 参数 搜索内容 文件名find 查找的目录 -name "查找...

  • Linux三剑客之grep命令

    先说说grep命令能做什么? 我们可以使用grep命令在文本中查找指定的字符串,就像你在windows中打开txt...

  • linux系统查找路径

    查找指定文件路径,请查看此文章在ubuntu下如何搜索文件?1.whereis 文件名特点:快速,但是是模糊查找,...

网友评论

      本文标题:用python搜索文件名并在其内容中查找指定字符串,txt,wo

      本文链接:https://www.haomeiwen.com/subject/fshljdtx.html