| import os
|
| import re
|
| import subprocess
|
| import sys
|
| from flask import Flask, render_template, request, jsonify
|
| import PyPDF2
|
| import docx
|
| from pptx import Presentation
|
| from openpyxl import load_workbook
|
|
|
| app = Flask(__name__)
|
|
|
| def search_files(directory, search_text, recursive=False):
|
| """
|
| Search for files containing the specified text in the given directory.
|
|
|
| Args:
|
| directory (str): The directory path to search in
|
| search_text (str): The text to search for
|
| recursive (bool): Whether to search recursively in subdirectories
|
|
|
| Returns:
|
| list: A list of dictionaries containing file paths and matching lines
|
| """
|
| results = []
|
|
|
|
|
| if not os.path.isdir(directory):
|
| return {"error": f"Directory '{directory}' does not exist"}
|
|
|
|
|
|
|
| pattern = ''.join(f'.*?{re.escape(c)}' for c in search_text)
|
| regex = re.compile(pattern, re.IGNORECASE)
|
|
|
|
|
| if recursive:
|
| walk_func = os.walk
|
| else:
|
|
|
| def walk_func(path):
|
| yield (path, [d for d in os.listdir(path) if os.path.isdir(os.path.join(path, d))],
|
| [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))])
|
|
|
| for root, _, files in walk_func(directory):
|
| for file in files:
|
| file_path = os.path.join(root, file)
|
| try:
|
| content = ""
|
| file_ext = os.path.splitext(file_path.lower())[1]
|
|
|
|
|
| if file_ext == '.pdf':
|
| try:
|
| with open(file_path, 'rb') as pdf_file:
|
| pdf_reader = PyPDF2.PdfReader(pdf_file)
|
| for page_num in range(len(pdf_reader.pages)):
|
| page = pdf_reader.pages[page_num]
|
| content += page.extract_text() + "\n"
|
| except Exception as e:
|
|
|
| continue
|
|
|
|
|
| elif file_ext == '.docx':
|
| try:
|
| doc = docx.Document(file_path)
|
| for para in doc.paragraphs:
|
| content += para.text + "\n"
|
|
|
| for table in doc.tables:
|
| for row in table.rows:
|
| for cell in row.cells:
|
| content += cell.text + "\n"
|
| except Exception as e:
|
|
|
| continue
|
|
|
|
|
| elif file_ext == '.xlsx':
|
| try:
|
| wb = load_workbook(filename=file_path, read_only=True)
|
| for sheet_name in wb.sheetnames:
|
| sheet = wb[sheet_name]
|
| content += f"Sheet: {sheet_name}\n"
|
| for row in sheet.rows:
|
| row_text = ""
|
| for cell in row:
|
| if cell.value is not None:
|
| row_text += str(cell.value) + "\t"
|
| content += row_text + "\n"
|
| except Exception as e:
|
|
|
| continue
|
|
|
|
|
| elif file_ext == '.pptx':
|
| try:
|
| prs = Presentation(file_path)
|
| for i, slide in enumerate(prs.slides):
|
| content += f"Slide {i+1}:\n"
|
| for shape in slide.shapes:
|
| if hasattr(shape, "text"):
|
| content += shape.text + "\n"
|
| except Exception as e:
|
|
|
| continue
|
|
|
| else:
|
|
|
| try:
|
| with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
| content = f.read()
|
| except Exception as e:
|
|
|
| continue
|
|
|
|
|
| if regex.search(content):
|
|
|
| lines = content.split('\n')
|
| matching_lines = []
|
|
|
| for i, line in enumerate(lines):
|
| if regex.search(line):
|
|
|
| matching_lines.append({
|
| 'line_number': i + 1,
|
| 'content': line.strip()
|
| })
|
|
|
|
|
| if matching_lines:
|
| results.append({
|
| 'file_path': file_path,
|
| 'relative_path': os.path.relpath(file_path, directory),
|
| 'matching_lines': matching_lines[:5]
|
| })
|
| except Exception as e:
|
|
|
| continue
|
|
|
| return results
|
|
|
| @app.route('/')
|
| def index():
|
| return render_template('index.html')
|
|
|
| @app.route('/search', methods=['POST'])
|
| def search():
|
| data = request.json
|
| directory = data.get('directory', '')
|
| search_text = data.get('search_text', '')
|
| recursive = data.get('recursive', False)
|
|
|
| if not directory or not search_text:
|
| return jsonify({"error": "Directory and search text are required"})
|
|
|
| results = search_files(directory, search_text, recursive)
|
| return jsonify(results)
|
|
|
| @app.route('/open_directory', methods=['POST'])
|
| def open_directory():
|
| data = request.json
|
| file_path = data.get('file_path', '')
|
|
|
| if not file_path:
|
| return jsonify({"error": "ファイルパスが指定されていません"})
|
|
|
| try:
|
|
|
| directory_path = os.path.dirname(file_path)
|
|
|
|
|
| if not os.path.isdir(directory_path):
|
| return jsonify({"error": f"ディレクトリ '{directory_path}' が存在しません"})
|
|
|
|
|
|
|
| subprocess.Popen(f'explorer.exe /select,"{file_path}"')
|
|
|
| return jsonify({"success": True, "message": f"ディレクトリを開きました: {directory_path}"})
|
| except Exception as e:
|
| return jsonify({"error": f"エラーが発生しました: {str(e)}"})
|
|
|
| @app.route('/select_folder', methods=['POST'])
|
| def select_folder():
|
| data = request.json
|
| initial_dir = data.get('initial_dir', '')
|
|
|
| try:
|
|
|
| script_dir = os.path.dirname(os.path.abspath(__file__))
|
| script_path = os.path.join(script_dir, 'folder_dialog.py')
|
|
|
|
|
| cmd_args = [sys.executable, script_path]
|
| if initial_dir and os.path.isdir(initial_dir):
|
| cmd_args.append(initial_dir)
|
|
|
|
|
| startupinfo = None
|
| if os.name == 'nt':
|
| startupinfo = subprocess.STARTUPINFO()
|
| startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
| startupinfo.wShowWindow = 1
|
|
|
| process = subprocess.Popen(
|
| cmd_args,
|
| stdout=subprocess.PIPE,
|
| stderr=subprocess.PIPE,
|
| text=True,
|
| startupinfo=startupinfo
|
| )
|
|
|
|
|
| stdout, stderr = process.communicate()
|
|
|
|
|
| if stderr:
|
| return jsonify({"error": f"エラーが発生しました: {stderr}"})
|
|
|
|
|
| folder_path = stdout.strip()
|
|
|
|
|
| if not folder_path:
|
| return jsonify({"message": "フォルダ選択がキャンセルされました"})
|
|
|
| return jsonify({"folder_path": folder_path, "message": "フォルダが選択されました"})
|
| except Exception as e:
|
| return jsonify({"error": f"エラーが発生しました: {str(e)}"})
|
|
|
| if __name__ == '__main__':
|
| app.run(debug=True)
|
|
|