Spaces:
Paused
Paused
File size: 563 Bytes
a7bad65 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import PyPDF2
import docx
def extract_text_from_file(path):
if path.endswith(".pdf"):
return extract_from_pdf(path)
elif path.endswith(".docx"):
return extract_from_docx(path)
else:
return ""
def extract_from_pdf(path):
with open(path, "rb") as f:
reader = PyPDF2.PdfReader(f)
return "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
def extract_from_docx(path):
doc = docx.Document(path)
return "\n".join([para.text for para in doc.paragraphs])
|