from flask import Flask, request, render_template import pytesseract from PIL import Image import os app = Flask(__name__) # Define the path for uploaded files UPLOAD_FOLDER = 'uploads' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER # Make sure the upload folder exists os.makedirs(UPLOAD_FOLDER, exist_ok=True) # Route to home page with form @app.route('/') def index(): return render_template('index.html') # Route to handle file upload and invoice extraction @app.route('/upload', methods=['POST']) def upload_file(): if 'invoice_image' not in request.files: return 'No file part' file = request.files['invoice_image'] if file.filename == '': return 'No selected file' if file: file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename) file.save(file_path) # Use pytesseract to extract text from the image img = Image.open(file_path) extracted_text = pytesseract.image_to_string(img) # Return the extracted text return render_template('index.html', extracted_text=extracted_text) if __name__ == '__main__': app.run(debug=True)