Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,83 @@
|
|
1 |
import io
|
|
|
|
|
2 |
from numbers_parser import Document
|
3 |
from openpyxl import Workbook
|
4 |
import gradio as gr
|
5 |
import pandas as pd
|
|
|
6 |
|
7 |
-
def numbers_to_xlsx(numbers_file):
|
8 |
-
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
return xls_path
|
28 |
-
except Exception as e:
|
29 |
-
return f"Error converting file: {e}"
|
30 |
-
|
31 |
# Define the Gradio interface
|
32 |
interface = gr.Interface(
|
33 |
fn=numbers_to_xlsx,
|
34 |
-
inputs="
|
35 |
-
outputs=gr.File(label="XLSX file"),
|
36 |
title="Numbers to XLSX Converter",
|
37 |
description="Convert your Numbers files to Excel format easily and download the result.",
|
|
|
|
|
38 |
)
|
39 |
|
40 |
# Launch the Gradio app
|
41 |
-
|
|
|
|
1 |
import io
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
from numbers_parser import Document
|
5 |
from openpyxl import Workbook
|
6 |
import gradio as gr
|
7 |
import pandas as pd
|
8 |
+
from typing import Optional, Union
|
9 |
|
10 |
+
def numbers_to_xlsx(numbers_file) -> Union[str, bytes]:
|
11 |
+
"""
|
12 |
+
Efficiently converts a Numbers file to XLSX format with optimized memory usage.
|
13 |
|
14 |
+
Args:
|
15 |
+
numbers_file: The uploaded Numbers file object.
|
16 |
|
17 |
+
Returns:
|
18 |
+
Union[str, bytes]: Path to the converted xlsx file, or error message if conversion fails.
|
19 |
+
"""
|
20 |
+
if not numbers_file:
|
21 |
+
return "Please upload a Numbers file"
|
22 |
+
|
23 |
+
# Create a temporary directory with context management
|
24 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
25 |
+
output_path = os.path.join(temp_dir, "converted.xlsx")
|
26 |
+
|
27 |
+
try:
|
28 |
+
# Read the Numbers file efficiently
|
29 |
+
doc = Document(numbers_file.name)
|
30 |
+
|
31 |
+
# Validate document structure
|
32 |
+
if not doc.sheets or not doc.sheets[0].tables:
|
33 |
+
return "Invalid Numbers file: No data tables found"
|
34 |
+
|
35 |
+
# Get the first table's data efficiently
|
36 |
+
table = doc.sheets[0].tables[0]
|
37 |
+
|
38 |
+
# Extract headers and data in one pass
|
39 |
+
rows = list(table.rows(values_only=True))
|
40 |
+
if not rows:
|
41 |
+
return "No data found in the table"
|
42 |
+
|
43 |
+
headers = rows[0]
|
44 |
+
data = rows[1:]
|
45 |
+
|
46 |
+
# Use pandas optimized DataFrame construction
|
47 |
+
df = pd.DataFrame(data, columns=headers)
|
48 |
+
|
49 |
+
# Optimize Excel writing
|
50 |
+
with pd.ExcelWriter(
|
51 |
+
output_path,
|
52 |
+
engine='openpyxl',
|
53 |
+
mode='w',
|
54 |
+
engine_kwargs={'options': {'strings_to_urls': False}}
|
55 |
+
) as writer:
|
56 |
+
df.to_excel(
|
57 |
+
writer,
|
58 |
+
index=False,
|
59 |
+
sheet_name='Sheet1',
|
60 |
+
freeze_panes=(1,0) # Freeze header row
|
61 |
+
)
|
62 |
+
|
63 |
+
# Read the file into memory before the temp directory is cleaned up
|
64 |
+
with open(output_path, 'rb') as f:
|
65 |
+
return f.read()
|
66 |
+
|
67 |
+
except Exception as e:
|
68 |
+
return f"Error converting file: {str(e)}"
|
69 |
|
|
|
|
|
|
|
|
|
70 |
# Define the Gradio interface
|
71 |
interface = gr.Interface(
|
72 |
fn=numbers_to_xlsx,
|
73 |
+
inputs=gr.File(label="Numbers File", file_types=[".numbers"]),
|
74 |
+
outputs=gr.File(label="XLSX file", file_types=[".xlsx"]),
|
75 |
title="Numbers to XLSX Converter",
|
76 |
description="Convert your Numbers files to Excel format easily and download the result.",
|
77 |
+
examples=None,
|
78 |
+
cache_examples=False
|
79 |
)
|
80 |
|
81 |
# Launch the Gradio app
|
82 |
+
if __name__ == "__main__":
|
83 |
+
interface.launch()
|