Here is my code for searching a keyword in Docx files present in the folder. It should show all the Docx files which contain the keyword. But here when I use AutoIndex it only showing the whole directory as the output and I want that the directory should go in my logic and provide the Docx files which have that keyword present.
from flask import Flask, render_template, request from flask_autoindex import AutoIndex from docx import * import re import os app = Flask(__name__,template_folder='templates') directory="C:/Users/nanis/Downloads/New folder" AutoIndex(app, browse_root=directory) @app.route("/") def index(): return render_template("home.html") @app.route('/search',methods=["POST"]) def search(): if request.method=="POST": keyword=request.form["text"] for filename in os.listdir(directory): if filename.endswith(".docx"): z= os.path.join(directory, filename) with open(z,"rb") as f: document=Document(f) body_element=document._body._body if (re.findall(keyword,body_element.xml)): a=keyword +" is present in this file ", filename return render_template("search.html",i=a) if __name__ == "__main__": app.run(debug=True)
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)