-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentReader.py
82 lines (68 loc) · 2.15 KB
/
DocumentReader.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import docx
import os
import pypdf
class DocumentReader:
"""
A class for reading text from PDF, Word, and text files.
Args:
file_path (str): The path to the input file.
Raises:
ValueError: If the file format is not supported.
"""
def __init__(self, file_path: str) -> None:
self.file_path = file_path
self.file_extension = os.path.splitext(file_path)[-1].lower()
def read(self) -> str:
"""
Reads the input file and extracts the text.
Returns:
str: The extracted text.
Raises:
ValueError: If the file format is not supported.
"""
if self.file_extension == ".pdf":
return self._read_pdf()
elif self.file_extension == ".docx":
return self._read_docx()
elif self.file_extension == ".txt":
return self._read_text()
else:
raise ValueError("Unsupported file format")
def _read_pdf(self) -> str:
"""
Reads text from a PDF file.
Returns:
str: The extracted text.
"""
with open(self.file_path, 'rb') as file:
pdf_reader = pypdf.PdfReader(file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
return text.strip()
def _read_docx(self) -> str:
"""
Reads text from a Word document.
Returns:
str: The extracted text.
"""
doc = docx.Document(self.file_path)
text = ""
for para in doc.paragraphs:
text += para.text + "\n"
return text.strip()
def _read_text(self) -> str:
"""
Reads text from a text file.
Returns:
str: The extracted text.
"""
with open(self.file_path, 'r') as file:
text = file.read()
return text.strip()
if __name__ == '__main__':
from pathlib import Path
file_path = Path("~/Downloads/PS1.pdf").expanduser()
reader = DocumentReader(file_path)
text = reader.read()
print(text)