-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_processing.py
More file actions
45 lines (38 loc) · 1.36 KB
/
file_processing.py
File metadata and controls
45 lines (38 loc) · 1.36 KB
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
ALLOWED_EXTENSIONS = {'pdf', 'doc', 'docx', 'xls', 'xlsx', 'txt'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
from werkzeug.utils import secure_filename
from docx import Document
import PyPDF2
import pandas as pd
def extract_text_from_txt(file_stream):
return file_stream.read().decode('utf-8', errors='ignore')
def extract_text_from_docx(file_stream):
doc = Document(file_stream)
return '\n'.join([p.text for p in doc.paragraphs])
def extract_text_from_pdf(file_stream):
reader = PyPDF2.PdfReader(file_stream)
text = ''
for page in reader.pages:
text += page.extract_text() or ''
return text
def extract_text_from_excel(file_stream):
# Retorna el contenido como CSV para mejor comprensión
try:
df = pd.read_excel(file_stream, header=None)
return df.to_csv(index=False, header=False)
except Exception:
return ''
def extract_text(file, filename):
ext = filename.rsplit('.', 1)[1].lower()
if ext == 'txt':
return extract_text_from_txt(file.stream)
elif ext in ['doc', 'docx']:
return extract_text_from_docx(file.stream)
elif ext == 'pdf':
return extract_text_from_pdf(file.stream)
elif ext in ['xls', 'xlsx']:
return extract_text_from_excel(file.stream)
else:
return ''