-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentProcessor.py
More file actions
95 lines (77 loc) · 2.9 KB
/
Copy pathDocumentProcessor.py
File metadata and controls
95 lines (77 loc) · 2.9 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
95
"""Design and Implement a Template Method for Document Processing (word, pdf,
excel) """
from abc import ABC, abstractmethod
class DocumentProcessor(ABC):
def process(self, file_path):
self.read_document(file_path)
self.process_document()
self.save_document()
@abstractmethod
def read_document(self, file_path):
pass
@abstractmethod
def process_document(self):
pass
@abstractmethod
def save_document(self):
pass
class WordProcessor(DocumentProcessor):
def __init__(self):
self.content = ""
self.metadata = {}
def read_document(self, file_path):
print(f"Reading Word document from {file_path}")
self.content = "Sample content from the Word document."
self.metadata = {"title": "Sample Word", "author": "Author Name"}
def process_document(self):
print("Processing Word document")
print(f"Content: {self.content}")
print(f"Metadata: {self.metadata}")
def save_document(self):
print("Saving Word document")
print(f"Document saved with title: {self.metadata['title']}")
print(f"Final Content: {self.content}")
class PDFProcessor(DocumentProcessor):
def __init__(self):
self.content = ""
self.metadata = {}
def read_document(self, file_path):
print(f"Reading PDF document from {file_path}")
self.content = "Sample content from the PDF document."
self.metadata = {"title": "Sample PDF", "author": "Author Name"}
def process_document(self):
print("Processing PDF document")
print(f"Content: {self.content}")
print(f"Metadata: {self.metadata}")
def save_document(self):
print("Saving PDF document")
print(f"Document saved with title: {self.metadata['title']}")
print(f"Final Content: {self.content}")
class ExcelProcessor(DocumentProcessor):
def __init__(self):
self.content = ""
self.metadata = {}
def read_document(self, file_path):
print(f"Reading Excel document from {file_path}")
self.content = "Sample content from the Excel document."
self.metadata = {"title": "Sample Excel", "author": "Author Name"}
def process_document(self):
print("Processing Excel document")
print(f"Content: {self.content}")
print(f"Metadata: {self.metadata}")
def save_document(self):
print("Saving Excel document")
print(f"Document saved with title: {self.metadata['title']}")
print(f"Final Content: {self.content}")
def main():
processors = [
WordProcessor(),
PDFProcessor(),
ExcelProcessor()
]
file_paths = ["word_document.docx", "pdf_document.pdf", "excel_document.xlsx"]
for i, processor in enumerate(processors):
print(f"\nProcessing {file_paths[i]}:")
processor.process(file_paths[i])
if __name__ == "__main__":
main()