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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -19
app.py CHANGED
@@ -5,9 +5,10 @@ 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
 
@@ -15,7 +16,7 @@ def numbers_to_xlsx(numbers_file) -> Union[str, bytes]:
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"
@@ -46,28 +47,29 @@ def numbers_to_xlsx(numbers_file) -> Union[str, bytes]:
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"]),
 
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
  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"
 
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(
74
  fn=numbers_to_xlsx,
75
  inputs=gr.File(label="Numbers File", file_types=[".numbers"]),