-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordProc
More file actions
92 lines (80 loc) · 3.13 KB
/
WordProc
File metadata and controls
92 lines (80 loc) · 3.13 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
class WordProcessor:
def __init__(self):
self.document = ""
self.filename = None
def create_document(self):
self.document = ""
self.filename = None
print("New document created.")
def open_document(self, filename):
try:
with open(filename, "r") as file:
self.document = file.read()
self.filename = filename
print(f"Document '{filename}' opened.")
except FileNotFoundError:
print("File not found. Please make sure the file exists.")
def save_document(self):
if self.filename:
with open(self.filename, "w") as file:
file.write(self.document)
print(f"Document saved as '{self.filename}'.")
else:
print("Please save the document with a filename using 'save_as'.")
def save_document_as(self, filename):
self.filename = filename
self.save_document()
def edit_document(self):
print("Start editing the document. Type 'exit' on a new line to finish.")
while True:
line = input()
if line == "exit":
break
self.document += line + "\n"
def format_text(self):
print("Format options: bold, italic, underline, align_left, align_right, align_center")
option = input("Enter the format option: ").strip()
if option == "bold":
self.document = f"**{self.document.strip()}**"
elif option == "italic":
self.document = f"*{self.document.strip()}*"
elif option == "underline":
self.document = f"__{self.document.strip()}__"
elif option == "align_left":
self.document = self.document.lstrip()
elif option == "align_right":
self.document = self.document.rstrip()
elif option == "align_center":
self.document = self.document.center(80)
else:
print("Invalid format option.")
def print_document(self):
print("\n" + self.document + "\n")
def run(self):
print("Welcome to Simple Word Processor!")
while True:
print("Options: create, open, save, save_as, edit, format, print, exit")
command = input("Enter a command: ").strip()
if command == "create":
self.create_document()
elif command == "open":
filename = input("Enter the filename to open: ").strip()
self.open_document(filename)
elif command == "save":
self.save_document()
elif command == "save_as":
filename = input("Enter the filename to save as: ").strip()
self.save_document_as(filename)
elif command == "edit":
self.edit_document()
elif command == "format":
self.format_text()
elif command == "print":
self.print_document()
elif command == "exit":
break
else:
print("Invalid command. Try again.")
if __name__ == "__main__":
wp = WordProcessor()
wp.run()