File size: 727 Bytes
ec99d5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import pypdf
import docx

def extract_text_from_pdf(pdf_bytes: bytes) -> tuple[str, int]:
    pdf_file = io.BytesIO(pdf_bytes)
    reader = pypdf.PdfReader(pdf_file)
    text = ""
    for page in reader.pages:
        page_text = page.extract_text()
        if page_text:
            text += page_text + "\n"
    return text.strip(), len(reader.pages)

def extract_text_from_docx(docx_bytes: bytes) -> tuple[str, int]:
    docx_file = io.BytesIO(docx_bytes)
    doc = docx.Document(docx_file)
    text = ""
    for paragraph in doc.paragraphs:
        if paragraph.text:
            text += paragraph.text + "\n"
    words = len(text.split())
    page_count = max(1, words // 400)
    return text.strip(), page_count