Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


Python
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)    


What I have tried:

I have tried autoindex by assigning a variable to it and the calling but it was not possible.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900