import os from PyPDF2 import PdfReader from docx import Document def extract_text_from_pdf(file_path): text = "" try: reader = PdfReader(file_path) for page in reader.pages: text += page.extract_text() + "\n" except Exception as e: print(f"Error extracting PDF: {e}") return text def extract_text_from_docx(file_path): text = "" try: doc = Document(file_path) for para in doc.paragraphs: text += para.text + "\n" except Exception as e: print(f"Error extracting DOCX: {e}") return text def extract_text_from_txt(file_path): try: with open(file_path, 'r', encoding='utf-8') as f: return f.read() except Exception as e: print(f"Error extracting TXT: {e}") return "" def get_text_from_file(file_path): ext = os.path.splitext(file_path)[1].lower() if ext == '.pdf': return extract_text_from_pdf(file_path) elif ext == '.docx': return extract_text_from_docx(file_path) elif ext == '.txt': return extract_text_from_txt(file_path) else: return ""