-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
27 lines (21 loc) · 734 Bytes
/
Copy pathparser.py
File metadata and controls
27 lines (21 loc) · 734 Bytes
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
import pdfplumber
from docx import Document
from PIL import Image
import pytesseract
def extract_text(file_path):
if file_path.endswith(".pdf"):
text = ""
with pdfplumber.open(file_path) as pdf:
for page in pdf.pages:
text += page.extract_text() or ""
return text
elif file_path.endswith(".docx"):
doc = Document(file_path)
return "\n".join([p.text for p in doc.paragraphs])
elif file_path.endswith((".png", ".jpg", ".jpeg")):
img = Image.open(file_path)
return pytesseract.image_to_string(img)
elif file_path.endswith(".txt"):
with open(file_path, "r", encoding="utf-8") as f:
return f.read()
return ""