File size: 670 Bytes
f245c77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pymupdf
from io import BytesIO
import docx2txt


def extract_text_from_bytes(pdf_bytes):
    try:
        memory_stream = BytesIO(pdf_bytes)

        doc = pymupdf.open(stream=memory_stream, filetype="pdf")

        text = ""
        for page in doc:
            text += page.get_text()
        doc.close()
        return text
    except Exception as e:
        return f"Error reading PDF: {str(e)}"


def extract_text_from_docx(docx_bytes):
    try:
        docx_stream = BytesIO(docx_bytes)

        text = docx2txt.process(docx_stream)

        return text
    except Exception as e:
        return f"Error reading DOCX: {str(e)}"