Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import openpyxl # ensure XLSX engine is available
|
|
3 |
import gradio as gr
|
4 |
import uuid
|
5 |
import os
|
|
|
6 |
|
7 |
# 7-Day Schedule Converter with explicit iteration and header fill and download support
|
8 |
|
@@ -104,7 +105,9 @@ def convert_schedule(file_path, direction):
|
|
104 |
display_df = result.reset_index().rename(columns={'index': first_col_name})
|
105 |
|
106 |
# 6. Save to Excel for download with better error handling
|
107 |
-
|
|
|
|
|
108 |
|
109 |
# Clean the DataFrame before saving
|
110 |
result_clean = result.copy()
|
@@ -117,8 +120,21 @@ def convert_schedule(file_path, direction):
|
|
117 |
result_clean[col] = result_clean[col].astype(str)
|
118 |
|
119 |
# Save with additional parameters for better compatibility
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
# Return both DataFrame and download path
|
124 |
return display_df, output_filename
|
|
|
3 |
import gradio as gr
|
4 |
import uuid
|
5 |
import os
|
6 |
+
import tempfile
|
7 |
|
8 |
# 7-Day Schedule Converter with explicit iteration and header fill and download support
|
9 |
|
|
|
105 |
display_df = result.reset_index().rename(columns={'index': first_col_name})
|
106 |
|
107 |
# 6. Save to Excel for download with better error handling
|
108 |
+
# Use temporary directory for file creation
|
109 |
+
temp_dir = tempfile.gettempdir()
|
110 |
+
output_filename = os.path.join(temp_dir, f"converted_{uuid.uuid4().hex}.xlsx")
|
111 |
|
112 |
# Clean the DataFrame before saving
|
113 |
result_clean = result.copy()
|
|
|
120 |
result_clean[col] = result_clean[col].astype(str)
|
121 |
|
122 |
# Save with additional parameters for better compatibility
|
123 |
+
try:
|
124 |
+
with pd.ExcelWriter(output_filename, engine='openpyxl', mode='w') as writer:
|
125 |
+
result_clean.to_excel(writer, sheet_name='Schedule', index=True, header=True)
|
126 |
+
|
127 |
+
# Verify file was created successfully
|
128 |
+
if not os.path.exists(output_filename):
|
129 |
+
raise FileNotFoundError("Excel file was not created successfully")
|
130 |
+
|
131 |
+
except Exception as save_error:
|
132 |
+
# If Excel save fails, try alternative approach
|
133 |
+
print(f"Excel save error: {save_error}")
|
134 |
+
# Try saving as CSV as fallback
|
135 |
+
csv_filename = os.path.join(temp_dir, f"converted_{uuid.uuid4().hex}.csv")
|
136 |
+
result_clean.to_csv(csv_filename, index=True)
|
137 |
+
output_filename = csv_filename
|
138 |
|
139 |
# Return both DataFrame and download path
|
140 |
return display_df, output_filename
|