Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,34 @@ 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 pathlib import Path
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def numbers_to_xlsx(numbers_file):
|
11 |
"""
|
12 |
-
|
13 |
|
14 |
Args:
|
15 |
numbers_file: The uploaded Numbers file object from Gradio.
|
@@ -21,12 +42,11 @@ def numbers_to_xlsx(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
|
30 |
doc = Document(numbers_file.name)
|
31 |
|
32 |
# Get all sheets and validate
|
@@ -35,39 +55,52 @@ def numbers_to_xlsx(numbers_file):
|
|
35 |
print("No sheets found in the document")
|
36 |
return None
|
37 |
|
38 |
-
#
|
39 |
-
|
|
|
40 |
|
41 |
for sheet_index, sheet in enumerate(sheets):
|
42 |
if not sheet.tables:
|
43 |
continue
|
44 |
|
45 |
-
#
|
|
|
46 |
table = sheet.tables[0]
|
47 |
|
48 |
-
#
|
49 |
-
rows = list(table.rows(
|
50 |
if not rows:
|
51 |
continue
|
52 |
-
|
53 |
-
# Split headers and data
|
54 |
-
headers = rows[0] if rows else []
|
55 |
-
data = rows[1:] if len(rows) > 1 else []
|
56 |
|
57 |
-
#
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# Freeze the header row
|
68 |
-
|
69 |
|
70 |
-
|
|
|
71 |
|
72 |
# Verify the file was created and has content
|
73 |
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|
|
|
3 |
import tempfile
|
4 |
from numbers_parser import Document
|
5 |
from openpyxl import Workbook
|
6 |
+
from openpyxl.styles import PatternFill, Font, Alignment
|
7 |
import gradio as gr
|
8 |
import pandas as pd
|
9 |
from pathlib import Path
|
10 |
|
11 |
+
def get_excel_color(numbers_color):
|
12 |
+
"""Convert Numbers color to Excel color format"""
|
13 |
+
if not numbers_color:
|
14 |
+
return None
|
15 |
+
# Convert RGB values to hex color
|
16 |
+
r = int(numbers_color.red * 255)
|
17 |
+
g = int(numbers_color.green * 255)
|
18 |
+
b = int(numbers_color.blue * 255)
|
19 |
+
return f"{r:02x}{g:02x}{b:02x}"
|
20 |
+
|
21 |
+
def apply_cell_formatting(cell, cell_data):
|
22 |
+
"""Apply formatting from Numbers cell to Excel cell"""
|
23 |
+
if hasattr(cell_data, 'background_color') and cell_data.background_color:
|
24 |
+
color = get_excel_color(cell_data.background_color)
|
25 |
+
if color:
|
26 |
+
cell.fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
|
27 |
+
|
28 |
+
if hasattr(cell_data, 'font_bold'):
|
29 |
+
cell.font = Font(bold=cell_data.font_bold)
|
30 |
+
|
31 |
def numbers_to_xlsx(numbers_file):
|
32 |
"""
|
33 |
+
Converts a Numbers file to XLSX format preserving colors and formatting.
|
34 |
|
35 |
Args:
|
36 |
numbers_file: The uploaded Numbers file object from Gradio.
|
|
|
42 |
return None
|
43 |
|
44 |
try:
|
|
|
45 |
output_dir = "outputs"
|
46 |
os.makedirs(output_dir, exist_ok=True)
|
47 |
output_path = os.path.join(output_dir, "converted.xlsx")
|
48 |
|
49 |
+
# Read the Numbers file
|
50 |
doc = Document(numbers_file.name)
|
51 |
|
52 |
# Get all sheets and validate
|
|
|
55 |
print("No sheets found in the document")
|
56 |
return None
|
57 |
|
58 |
+
# Create a new workbook
|
59 |
+
wb = Workbook()
|
60 |
+
wb.remove(wb.active) # Remove default sheet
|
61 |
|
62 |
for sheet_index, sheet in enumerate(sheets):
|
63 |
if not sheet.tables:
|
64 |
continue
|
65 |
|
66 |
+
# Create new sheet
|
67 |
+
ws = wb.create_sheet(title=f"Sheet{sheet_index + 1}")
|
68 |
table = sheet.tables[0]
|
69 |
|
70 |
+
# Get all rows with their formatting
|
71 |
+
rows = list(table.rows())
|
72 |
if not rows:
|
73 |
continue
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
# Write data and apply formatting
|
76 |
+
for row_idx, row in enumerate(rows, 1):
|
77 |
+
for col_idx, cell_data in enumerate(row, 1):
|
78 |
+
# Write cell value
|
79 |
+
cell = ws.cell(row=row_idx, column=col_idx)
|
80 |
+
cell.value = cell_data.value if hasattr(cell_data, 'value') else cell_data
|
81 |
+
|
82 |
+
# Apply formatting if available
|
83 |
+
if hasattr(cell_data, 'background_color') or hasattr(cell_data, 'font_bold'):
|
84 |
+
apply_cell_formatting(cell, cell_data)
|
85 |
|
86 |
+
# Adjust column widths
|
87 |
+
for column in ws.columns:
|
88 |
+
max_length = 0
|
89 |
+
column_letter = column[0].column_letter
|
90 |
+
for cell in column:
|
91 |
+
try:
|
92 |
+
if len(str(cell.value)) > max_length:
|
93 |
+
max_length = len(str(cell.value))
|
94 |
+
except:
|
95 |
+
pass
|
96 |
+
adjusted_width = (max_length + 2)
|
97 |
+
ws.column_dimensions[column_letter].width = adjusted_width
|
98 |
|
99 |
# Freeze the header row
|
100 |
+
ws.freeze_panes = 'A2'
|
101 |
|
102 |
+
# Save the workbook
|
103 |
+
wb.save(output_path)
|
104 |
|
105 |
# Verify the file was created and has content
|
106 |
if os.path.exists(output_path) and os.path.getsize(output_path) > 0:
|