File size: 1,309 Bytes
25a0387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import fitz  # PyMuPDF
from docx import Document
import io
import logging

def extract_text_from_pdf(file_bytes: bytes) -> str:
    """Extracts text content from a PDF file."""
    try:
        pdf_document = fitz.open(stream=file_bytes, filetype="pdf")
        text = ""
        for page_num in range(len(pdf_document)):
            page = pdf_document.load_page(page_num)
            text += page.get_text()
        return text
    except Exception as e:
        logging.error(f"Error parsing PDF: {e}", exc_info=True)
        return ""

def extract_text_from_docx(file_bytes: bytes) -> str:
    """Extracts text content from a DOCX file."""
    try:
        document = Document(io.BytesIO(file_bytes))
        text = "\n".join([para.text for para in document.paragraphs])
        return text
    except Exception as e:
        logging.error(f"Error parsing DOCX: {e}", exc_info=True)
        return ""

def parse_document(file_name: str, file_bytes: bytes) -> str:
    """Parses a document based on its file extension."""
    if file_name.lower().endswith(".pdf"):
        return extract_text_from_pdf(file_bytes)
    elif file_name.lower().endswith(".docx"):
        return extract_text_from_docx(file_bytes)
    else:
        raise ValueError("Unsupported file format. Please upload a PDF or DOCX file.")