-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlauncher.py
More file actions
161 lines (138 loc) · 5.79 KB
/
launcher.py
File metadata and controls
161 lines (138 loc) · 5.79 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
"""
Main Launcher with UI Selection
This script provides a simple GUI to choose between Word-to-CSV and CSV-to-Word conversion.
"""
import os
import sys
import tkinter as tk
from tkinter import ttk, messagebox
import importlib.util
import subprocess
class LauncherApp:
"""Main launcher application with UI for conversion direction selection."""
def __init__(self, root):
self.root = root
self.root.title("Document Converter Launcher")
self.root.geometry("500x300")
self.root.resizable(False, False)
# Center the window
window_width = 500
window_height = 300
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
center_x = int(screen_width/2 - window_width/2)
center_y = int(screen_height/2 - window_height/2)
self.root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
self.setup_ui()
def setup_ui(self):
"""Set up the user interface."""
# Main frame
main_frame = ttk.Frame(self.root, padding="20")
main_frame.pack(fill=tk.BOTH, expand=True)
# Title
title_label = ttk.Label(main_frame, text="Document Converter", font=("Helvetica", 16, "bold"))
title_label.pack(pady=(0, 20))
# Description
desc_label = ttk.Label(main_frame, text="Select the conversion direction:", font=("Helvetica", 10))
desc_label.pack(pady=(0, 20))
# Button frame
button_frame = ttk.Frame(main_frame)
button_frame.pack(fill=tk.BOTH, expand=True)
# Word to CSV button
self.word_to_csv_button = ttk.Button(
button_frame,
text="Word to CSV",
command=self.launch_word_to_csv,
width=20
)
self.word_to_csv_button.pack(pady=10)
# Description
word_to_csv_desc = ttk.Label(
button_frame,
text="Convert Word documents with product specifications to CSV format",
font=("Helvetica", 9),
foreground="gray"
)
word_to_csv_desc.pack()
# CSV to Word button
self.csv_to_word_button = ttk.Button(
button_frame,
text="CSV to Word",
command=self.launch_csv_to_word,
width=20
)
self.csv_to_word_button.pack(pady=10)
# Description
csv_to_word_desc = ttk.Label(
button_frame,
text="Convert CSV files back to formatted Word documents",
font=("Helvetica", 9),
foreground="gray"
)
csv_to_word_desc.pack()
# Status bar
self.status_var = tk.StringVar()
self.status_var.set("Ready")
ttk.Label(self.root, textvariable=self.status_var, relief=tk.SUNKEN, anchor=tk.W).pack(side=tk.BOTTOM, fill=tk.X)
def launch_word_to_csv(self):
"""Launch the Word to CSV converter."""
self.status_var.set("Launching Word to CSV converter...")
self.root.update_idletasks()
try:
# Try to import and run the converter
self._run_module("converter_app", "Word to CSV converter")
except Exception as e:
messagebox.showerror("Error", f"Failed to launch Word to CSV converter: {str(e)}")
self.status_var.set("Error launching Word to CSV converter")
def launch_csv_to_word(self):
"""Launch the CSV to Word converter."""
self.status_var.set("Launching CSV to Word converter...")
self.root.update_idletasks()
try:
# Try to import and run the converter
self._run_module("csv_to_word_gui", "CSV to Word converter")
except Exception as e:
messagebox.showerror("Error", f"Failed to launch CSV to Word converter: {str(e)}")
self.status_var.set("Error launching CSV to Word converter")
def _run_module(self, module_name, module_description):
"""Try various methods to run a module."""
# First try to import and run the module
try:
module_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), f"{module_name}.py")
if os.path.exists(module_path):
# Method 1: Import and run
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Try to run the main function
if hasattr(module, 'main'):
self.root.withdraw() # Hide the launcher
try:
module.main()
finally:
self.root.deiconify() # Show the launcher again when done
self.status_var.set("Ready")
else:
# If no main function, try other methods
raise AttributeError(f"No main function found in {module_name}")
else:
raise FileNotFoundError(f"{module_name}.py not found")
except Exception as e:
# Method 2: Try to run as a subprocess
try:
result = subprocess.run(
[sys.executable, f"{module_name}.py"],
check=True,
stderr=subprocess.PIPE,
text=True
)
self.status_var.set("Ready")
except subprocess.CalledProcessError as sub_e:
raise Exception(f"Failed to run {module_description}: {sub_e.stderr}")
def main():
"""Run the launcher application."""
root = tk.Tk()
app = LauncherApp(root)
root.mainloop()
if __name__ == "__main__":
main()