hellorahulk commited on
Commit
04f69e7
·
verified ·
1 Parent(s): 1f99de8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -49
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) -> Union[str, tuple[str, bytes]]:
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
- Union[str, tuple[str, bytes]]: Either error message or tuple of (filename, file_content)
20
  """
21
  if not numbers_file:
22
- return "Please upload a Numbers file"
23
 
24
- # Create a temporary directory with context management
25
- with tempfile.TemporaryDirectory() as temp_dir:
26
- output_path = os.path.join(temp_dir, "converted.xlsx")
 
 
27
 
28
- try:
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 "Invalid Numbers file: No data tables found"
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 "No data found in the table"
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 filename and content as tuple
67
- return ("converted.xlsx", Path(output_path).read_bytes())
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(