-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpdf2text.py
33 lines (25 loc) · 863 Bytes
/
pdf2text.py
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
import io
import PyPDF2
def extract_pdf_text(pdf_data):
"""
Extracts text from a given PDF byte data.
Parameters:
pdf_data (bytes): The byte data of the PDF to be read.
Returns:
str: The extracted text from the PDF.
"""
# Initialize a string to hold all extracted text
extracted_text = ""
try:
# Create a BytesIO object from the PDF data
pdf_buffer = io.BytesIO(pdf_data)
# Initialize PDF reader
pdf_reader = PyPDF2.PdfReader(pdf_buffer)
# Loop through all the pages and extract text
for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_num]
extracted_text += page.extract_text()
except Exception as e:
print(f"Error: An unexpected error occurred - {e}")
return None
return extracted_text