-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_processor.py
More file actions
94 lines (74 loc) · 2.85 KB
/
Copy pathdocument_processor.py
File metadata and controls
94 lines (74 loc) · 2.85 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
# document_processor.py
from pathlib import Path
from typing import List
import pypdf
import docx2txt
class DocumentProcessor:
"""
Handles the processing of different document types (PDF, DOCX, TXT) and combines their content.
This class is responsible for extracting text from various document formats that can be used
as reference material for meeting summaries.
"""
def __init__(self, upload_dir: Path):
"""
Initialize the DocumentProcessor with a directory path where uploaded files are stored.
Args:
upload_dir (Path): Directory path where uploaded documents are stored
"""
self.upload_dir = upload_dir
def process_documents(self, filenames: List[str]) -> str:
"""
Process a list of documents and combine their text content.
Args:
filenames (List[str]): List of filenames to process
Returns:
str: Combined text from all processed documents
"""
combined_text = []
for filename in filenames:
file_path = self.upload_dir / filename
if not file_path.exists():
continue
# Process based on file type
if file_path.suffix.lower() == '.pdf':
text = self._process_pdf(file_path)
elif file_path.suffix.lower() in ['.doc', '.docx']:
text = self._process_docx(file_path)
elif file_path.suffix.lower() == '.txt':
text = self._process_txt(file_path)
else:
continue
combined_text.append(text)
return "\n\n".join(combined_text)
def _process_pdf(self, file_path: Path) -> str:
"""
Extract text from a PDF file.
Args:
file_path (Path): Path to the PDF file
Returns:
str: Extracted text content
"""
text = []
with open(file_path, 'rb') as file:
pdf = pypdf.PdfReader(file)
for page in pdf.pages:
text.append(page.extract_text())
return "\n".join(text)
def _process_docx(self, file_path: Path) -> str:
"""
Extract text from a DOCX file.
Args:
file_path (Path): Path to the DOCX file
Returns:
str: Extracted text content
"""
return docx2txt.process(file_path)
def _process_txt(self, file_path: Path) -> str:
"""
Read text from a TXT file.
Args:
file_path (Path): Path to the TXT file
Returns:
str: File content
"""
return file_path.read_text()