Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,20 @@ from pptx import Presentation
|
|
9 |
import fitz # PyMuPDF
|
10 |
import os
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
def convert_file(files, output_format):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
results = []
|
14 |
for file_obj in files:
|
15 |
name = file_obj.name
|
@@ -149,11 +162,17 @@ def convert_file(files, output_format):
|
|
149 |
|
150 |
try:
|
151 |
data, filename = converters[output_format]()
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
except Exception as e:
|
154 |
-
results.append(("
|
155 |
|
156 |
-
return
|
157 |
|
158 |
with gr.Blocks(title="Universal File Converter", theme=gr.themes.Soft()) as demo:
|
159 |
gr.Markdown(
|
@@ -175,8 +194,7 @@ with gr.Blocks(title="Universal File Converter", theme=gr.themes.Soft()) as demo
|
|
175 |
)
|
176 |
output_format = gr.Radio(
|
177 |
["PDF", "Word", "Excel", "PowerPoint", "HD Image"],
|
178 |
-
label="π― Select Output Format"
|
179 |
-
info="Choose the format you want to convert to"
|
180 |
)
|
181 |
convert_btn = gr.Button("π Convert Now")
|
182 |
|
|
|
9 |
import fitz # PyMuPDF
|
10 |
import os
|
11 |
|
12 |
+
def create_error_file(directory, filename, message):
|
13 |
+
path = os.path.join(directory, filename)
|
14 |
+
with open(path, "w") as f:
|
15 |
+
f.write(message)
|
16 |
+
return path
|
17 |
+
|
18 |
def convert_file(files, output_format):
|
19 |
+
output_dir = "converted"
|
20 |
+
os.makedirs(output_dir, exist_ok=True)
|
21 |
+
|
22 |
+
# Clean old files
|
23 |
+
for f in os.listdir(output_dir):
|
24 |
+
os.remove(os.path.join(output_dir, f))
|
25 |
+
|
26 |
results = []
|
27 |
for file_obj in files:
|
28 |
name = file_obj.name
|
|
|
162 |
|
163 |
try:
|
164 |
data, filename = converters[output_format]()
|
165 |
+
if data is not None:
|
166 |
+
save_path = os.path.join(output_dir, filename)
|
167 |
+
with open(save_path, "wb") as f:
|
168 |
+
f.write(data)
|
169 |
+
results.append(save_path)
|
170 |
+
else:
|
171 |
+
results.append(create_error_file(output_dir, f"{name}_unsupported.txt", "Unsupported conversion."))
|
172 |
except Exception as e:
|
173 |
+
results.append(create_error_file(output_dir, f"{name}_error.txt", str(e)))
|
174 |
|
175 |
+
return results
|
176 |
|
177 |
with gr.Blocks(title="Universal File Converter", theme=gr.themes.Soft()) as demo:
|
178 |
gr.Markdown(
|
|
|
194 |
)
|
195 |
output_format = gr.Radio(
|
196 |
["PDF", "Word", "Excel", "PowerPoint", "HD Image"],
|
197 |
+
label="π― Select Output Format"
|
|
|
198 |
)
|
199 |
convert_btn = gr.Button("π Convert Now")
|
200 |
|