-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
182 lines (143 loc) · 4.99 KB
/
main.py
File metadata and controls
182 lines (143 loc) · 4.99 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import win32com.client
from docx import Document
from fpdf import FPDF
import comtypes.client
from pdf2docx import converter
# Define conversion functions for DOC files
def convert_doc_to_docx(doc_file):
word = win32com.client.Dispatch("Word.Application")
doc = word.Documents.Open(doc_file)
doc.SaveAs(doc_file.replace(".doc", ".docx"), 12) # 12 for .docx format
doc.Close()
word.Quit()
def convert_doc_to_html(doc_file):
pass
def convert_doc_to_pdf(doc_file):
pass
# Define conversion functions for DOCX files
def convert_docx_to_doc(docx_file):
pass
def convert_docx_to_html(docx_file):
pass
def convert_docx_to_pdf(docx_file):
# Input and output file paths
docx_path = os.path.abspath(docx_file)
pdf_path = os.path.abspath(docx_file.replace(".docx", ".pdf"))
#Create a Word Application
word = comtypes.client.CreateObject("Word.Application")
word.Visible = False # Set to True if you want to see the Word application
try:
# Open the DOCX file
doc = word.Documents.Open(docx_path)
# Specify PDF format (17 is for PDF)
pdf_format = 17
# Save the document as PDF
doc.SaveAs(pdf_path, FileFormat=pdf_format)
doc.Close()
except Exception as e:
print(f"Error: {e}")
finally:
# Quit the Word application
word.Quit()
# Define conversion functions for XLSX files
def convert_xlsx_to_xls(xlsx_file):
pass
def convert_xlsx_to_pdf(xlsx_file):
pass
# Define conversion functions for XLS files
def convert_xls_to_xlsx(xls_file):
pass
def convert_xls_to_pdf(xls_file):
pass
# Define conversion functions for RT files
def convert_rt_to_docx(rt_file):
pass
def convert_rt_to_html(rt_file):
pass
def convert_rt_to_pdf(rt_file):
pass
# Define conversion functions for HTML files
def convert_html_to_docx(html_file):
pass
def convert_html_to_pdf(html_file):
pass
# Define conversion functions for PDF files
def convert_pdf_to_docx(pdf_file):
pdf_path = os.path.abspath(pdf_file)
docx_path = os.path.abspath(pdf_file.replace(".pdf",".docx"))
try:
cv = converter(pdf_file)
cv.convert(docx_file, start = 0, end = None)
cv.close()
except Exception as e:
print(f"Error: {e}")
def convert_pdf_to_xlsx(pdf_file):
pass
def convert_pdf_to_pptx(pdf_file):
pass
def convert_pdf_to_html(pdf_file):
pass
# Define conversion functions for PPTX files
def convert_pptx_to_pdf(pptx_file):
pass
def convert_pptx_to_ppt(pptx_file):
pass
# Menu Ddisplay
def display_menu():
print("Select a file type to convert:")
print("1. DOC")
print("2. DOCX")
print("3. XLSX")
print("4. XLS")
print("5. RT")
print("6. HTML")
print("7. PDF")
print("8. PPTX")
print("9. PPT")
choice = input("Enter your choice (1-9): ")
return choice
def main():
choice = display_menu()
file_types = {1: "doc", 2: "docx", 3: "xlsx", 4: "xls", 5: "rt", 6: "html", 7: "pdf", 8: "pptx", 9: "ppt"}
file_type = file_types.get(int(choice))
if file_type is None:
print("Invalid choice!")
return
file_path = input("Enter the path of the file: ")
if not os.path.exists(file_path):
print("File not found!")
return
# Define conversion options for each file type
conversions = {
"doc": {"docx": convert_doc_to_docx, "html": convert_doc_to_html, "pdf": convert_doc_to_pdf},
"docx": {"doc": convert_docx_to_doc, "html": convert_docx_to_html, "pdf": convert_docx_to_pdf},
"xlsx": {"xls": convert_xlsx_to_xls, "pdf": convert_xlsx_to_pdf},
"xls": {"xlsx": convert_xls_to_xlsx, "pdf": convert_xls_to_pdf},
"rt": {"docx": convert_rt_to_docx, "html": convert_rt_to_html, "pdf": convert_rt_to_pdf},
"html": {"docx": convert_html_to_docx, "pdf": convert_html_to_pdf},
"pdf": {"docx": convert_pdf_to_docx, "xlsx": convert_pdf_to_xlsx, "pptx": convert_pdf_to_pptx, "html": convert_pdf_to_html},
"pptx": {"pdf": convert_pptx_to_pdf, "ppt": convert_pptx_to_ppt},
"ppt": {"pptx": convert_pptx_to_ppt},
}
# Retrieve conversion options for the selected file type
conversion_options = conversions.get(file_type)
if conversion_options is None:
print("Conversion options not available for this file type!")
return
# Display available conversion options for the selected file type
print(f"Conversion options for {file_type.upper()}:")
for target_format in conversion_options.keys():
print(f"- {target_format.upper()}")
# Get user's target format choice
target_format = input("Enter the target format: ").lower()
# Retrieve conversion function for the target format
conversion_function = conversion_options.get(target_format)
if conversion_function is None:
print("Invalid target format!")
return
# Perform conversion
conversion_function(file_path)
print("Conversion complete!")
if __name__ == "__main__":
main()