Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,9 @@ from numbers_parser import Document
|
|
5 |
from openpyxl import Workbook
|
6 |
import gradio as gr
|
7 |
import pandas as pd
|
8 |
-
from typing import Union
|
9 |
from pathlib import Path
|
10 |
|
11 |
-
def numbers_to_xlsx(numbers_file)
|
12 |
"""
|
13 |
Efficiently converts a Numbers file to XLSX format with optimized memory usage.
|
14 |
|
@@ -16,58 +15,59 @@ def numbers_to_xlsx(numbers_file) -> Union[str, tuple[str, bytes]]:
|
|
16 |
numbers_file: The uploaded Numbers file object.
|
17 |
|
18 |
Returns:
|
19 |
-
|
20 |
"""
|
21 |
if not numbers_file:
|
22 |
-
return
|
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 |
-
except Exception as e:
|
70 |
-
return f"Error converting file: {str(e)}"
|
71 |
|
72 |
# Define the Gradio interface with correct file handling
|
73 |
interface = gr.Interface(
|
|
|
5 |
from openpyxl import Workbook
|
6 |
import gradio as gr
|
7 |
import pandas as pd
|
|
|
8 |
from pathlib import Path
|
9 |
|
10 |
+
def numbers_to_xlsx(numbers_file):
|
11 |
"""
|
12 |
Efficiently converts a Numbers file to XLSX format with optimized memory usage.
|
13 |
|
|
|
15 |
numbers_file: The uploaded Numbers file object.
|
16 |
|
17 |
Returns:
|
18 |
+
str: Path to the converted xlsx file, or error message if conversion fails.
|
19 |
"""
|
20 |
if not numbers_file:
|
21 |
+
return None
|
22 |
|
23 |
+
try:
|
24 |
+
# Create output directory if it doesn't exist
|
25 |
+
output_dir = "outputs"
|
26 |
+
os.makedirs(output_dir, exist_ok=True)
|
27 |
+
output_path = os.path.join(output_dir, "converted.xlsx")
|
28 |
|
29 |
+
# Read the Numbers file efficiently
|
30 |
+
doc = Document(numbers_file.name)
|
31 |
+
|
32 |
+
# Validate document structure
|
33 |
+
if not doc.sheets or not doc.sheets[0].tables:
|
34 |
+
return None
|
35 |
+
|
36 |
+
# Get the first table's data efficiently
|
37 |
+
table = doc.sheets[0].tables[0]
|
38 |
+
|
39 |
+
# Extract headers and data in one pass
|
40 |
+
rows = list(table.rows(values_only=True))
|
41 |
+
if not rows:
|
42 |
+
return None
|
43 |
+
|
44 |
+
headers = rows[0]
|
45 |
+
data = rows[1:]
|
46 |
+
|
47 |
+
# Use pandas optimized DataFrame construction
|
48 |
+
df = pd.DataFrame(data, columns=headers)
|
49 |
+
|
50 |
+
# Optimize Excel writing with correct options
|
51 |
+
writer = pd.ExcelWriter(
|
52 |
+
output_path,
|
53 |
+
engine='openpyxl'
|
54 |
+
)
|
55 |
+
|
56 |
+
df.to_excel(
|
57 |
+
writer,
|
58 |
+
index=False,
|
59 |
+
sheet_name='Sheet1'
|
60 |
+
)
|
61 |
+
|
62 |
+
# Freeze the header row using openpyxl directly
|
63 |
+
writer.sheets['Sheet1'].freeze_panes = 'A2'
|
64 |
+
writer.close()
|
65 |
+
|
66 |
+
return output_path
|
67 |
|
68 |
+
except Exception as e:
|
69 |
+
print(f"Error converting file: {str(e)}")
|
70 |
+
return None
|
|
|
|
|
71 |
|
72 |
# Define the Gradio interface with correct file handling
|
73 |
interface = gr.Interface(
|