-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
96 lines (68 loc) · 2.45 KB
/
hello.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from flask import Flask, request, render_template, redirect
import boto3
import json
import csv
import PyPDF2, io
textractclient = boto3.client("textract", aws_access_key_id="AKIASODT36CXC2DG3PWR",
aws_secret_access_key="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", region_name="us-east-1")
app = Flask(__name__)
@app.route("/", methods=["GET"])
def main():
return render_template("index.html", jsonData=json.dumps({}))
@app.route("/<string:page_name>")
def html_page(page_name):
return render_template(page_name)
@app.route("/extractImg", methods=["POST"])
def extractImage():
file = request.files.get("filename")
binaryFile = file.read()
response = textractclient.detect_document_text(
Document={
'Bytes': binaryFile
}
)
extractedText = ""
for block in response["Blocks"]:
if block["BlockType"] == "LINE":
#print('\033[94m' + item["Text"] + '\033[0m')
extractedText = extractedText+block["Text"]+" "
responseJson = {
"text": extractedText
}
# Print or return the extracted text
print(responseJson)
return render_template("works.html", jsonData=json.dumps(responseJson))
@app.route("/extractPdf", methods=["POST"])
def extractpdf():
file = request.files.get("pdffilename")
page_text = ""
if file:
pdf_data = file.read()
# Open the PDF using PyMuPDF
pdf_reader = PyPDF2.PdfFileReader(io.BytesIO(pdf_data))
# Extract text from all pages
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
page_text += page.extractText() + "\n"
responseJson = {
"text": page_text
}
# Print or return the extracted text
print(responseJson)
return render_template("works.html", jsonData=json.dumps(responseJson))
def write_to_csv(data):
with open('database12.csv', newline='', mode='a') as database:
email = data["email"]
subject = data["subject"]
message = data["feedback"]
csv_writer = csv.writer(database, delimiter=',', quotechar=',', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow([email, subject, message])
@app.route('/submit_form', methods=['POST', 'GET'])
def submit_form():
if request.method == 'POST':
data = request.form.to_dict()
write_to_csv(data)
print(data)
return redirect('/thankyou.html')
if __name__ == '__main__':
app.run()