-
Notifications
You must be signed in to change notification settings - Fork 0
/
filefetch.py
38 lines (30 loc) · 1.05 KB
/
filefetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
import fitz # PyMuPDF
app = Flask(__name__)
CORS(app, resources={r"/upload": {"origins": "http://localhost:5173"}})
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
if 'file' not in request.files:
return jsonify({'error': 'No file part'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'})
if file and file.filename.endswith('.pdf'):
text = extract_text_from_pdf(file)
return jsonify({'text': text})
else:
return jsonify({'error': 'Invalid file format. Please upload a PDF file'})
def extract_text_from_pdf(file):
doc = fitz.open(stream=file.read(), filetype="pdf")
text = ""
for page_num in range(doc.page_count):
page = doc[page_num]
text += page.get_text()
return text
if __name__ == '__main__':
# app.run(debug=True)
app.run(debug=True, port=5001)