Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,76 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
|
|
|
|
|
|
| 3 |
import zipfile
|
|
|
|
| 4 |
import shutil
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import subprocess
|
| 7 |
|
| 8 |
def convert_xlsx_to_xls(xlsx_path, xls_path):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def process_zip(zip_file):
|
| 18 |
temp_dir = tempfile.mkdtemp()
|
| 19 |
output_dir = tempfile.mkdtemp()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
for
|
| 28 |
-
if file.endswith(".xlsx"):
|
| 29 |
-
xlsx_file = os.path.join(root, file)
|
| 30 |
-
rel_path = os.path.relpath(xlsx_file, temp_dir)
|
| 31 |
-
xls_path = os.path.join(output_dir, os.path.splitext(rel_path)[0] + ".xls")
|
| 32 |
-
try:
|
| 33 |
-
convert_xlsx_to_xls(xlsx_file, xls_path)
|
| 34 |
-
except Exception as e:
|
| 35 |
-
print(f"Failed to convert {xlsx_file}: {e}")
|
| 36 |
-
|
| 37 |
-
# Repack output
|
| 38 |
-
output_zip = os.path.join(tempfile.gettempdir(), "converted_xls.zip")
|
| 39 |
-
with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 40 |
-
for root, _, files in os.walk(output_dir):
|
| 41 |
for file in files:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
shutil.rmtree(temp_dir)
|
| 47 |
-
shutil.rmtree(output_dir)
|
| 48 |
return output_zip
|
| 49 |
|
| 50 |
demo = gr.Interface(
|
| 51 |
fn=process_zip,
|
| 52 |
inputs=gr.File(file_types=[".zip"], label="Upload ZIP with .xlsx files"),
|
| 53 |
outputs=gr.File(label="Download Converted .xls ZIP"),
|
| 54 |
-
title="XLSX to XLS Converter
|
| 55 |
-
description="Upload a ZIP of Excel .xlsx files. Get back a ZIP with .xls versions
|
| 56 |
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
+
from openpyxl import load_workbook
|
| 3 |
+
import xlwt
|
| 4 |
+
import gradio as gr
|
| 5 |
import zipfile
|
| 6 |
+
import tempfile
|
| 7 |
import shutil
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def convert_xlsx_to_xls(xlsx_path, xls_path):
|
| 10 |
+
try:
|
| 11 |
+
wb_xlsx = load_workbook(xlsx_path, data_only=True)
|
| 12 |
+
wb_xls = xlwt.Workbook()
|
| 13 |
+
|
| 14 |
+
for sheet_name in wb_xlsx.sheetnames:
|
| 15 |
+
ws_xlsx = wb_xlsx[sheet_name]
|
| 16 |
+
ws_xls = wb_xls.add_sheet(sheet_name[:31]) # .xls sheet name limit is 31
|
| 17 |
|
| 18 |
+
for r_idx, row in enumerate(ws_xlsx.iter_rows(values_only=True)):
|
| 19 |
+
for c_idx, value in enumerate(row):
|
| 20 |
+
if value is not None:
|
| 21 |
+
# Handle wide variety of types safely
|
| 22 |
+
try:
|
| 23 |
+
ws_xls.write(r_idx, c_idx, str(value) if isinstance(value, (dict, list, tuple)) else value)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
ws_xls.write(r_idx, c_idx, str(value))
|
| 26 |
+
|
| 27 |
+
os.makedirs(os.path.dirname(xls_path), exist_ok=True)
|
| 28 |
+
wb_xls.save(xls_path)
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error converting {xlsx_path} → {xls_path}: {e}")
|
| 31 |
|
| 32 |
def process_zip(zip_file):
|
| 33 |
temp_dir = tempfile.mkdtemp()
|
| 34 |
output_dir = tempfile.mkdtemp()
|
| 35 |
|
| 36 |
+
try:
|
| 37 |
+
# Extract zip
|
| 38 |
+
with zipfile.ZipFile(zip_file.name, 'r') as zip_ref:
|
| 39 |
+
zip_ref.extractall(temp_dir)
|
| 40 |
+
|
| 41 |
+
# Walk through and convert
|
| 42 |
+
for root, _, files in os.walk(temp_dir):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
for file in files:
|
| 44 |
+
if file.endswith(".xlsx"):
|
| 45 |
+
xlsx_file = os.path.join(root, file)
|
| 46 |
+
rel_path = os.path.relpath(xlsx_file, temp_dir)
|
| 47 |
+
xls_path = os.path.join(output_dir, os.path.splitext(rel_path)[0] + ".xls")
|
| 48 |
+
try:
|
| 49 |
+
convert_xlsx_to_xls(xlsx_file, xls_path)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Failed to convert {xlsx_file}: {e}")
|
| 52 |
+
|
| 53 |
+
# Repack output
|
| 54 |
+
output_zip = os.path.join(tempfile.gettempdir(), "converted_xls.zip")
|
| 55 |
+
with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 56 |
+
for root, _, files in os.walk(output_dir):
|
| 57 |
+
for file in files:
|
| 58 |
+
abs_file = os.path.join(root, file)
|
| 59 |
+
rel_path = os.path.relpath(abs_file, output_dir)
|
| 60 |
+
zipf.write(abs_file, rel_path)
|
| 61 |
+
|
| 62 |
+
finally:
|
| 63 |
+
shutil.rmtree(temp_dir, ignore_errors=True)
|
| 64 |
+
shutil.rmtree(output_dir, ignore_errors=True)
|
| 65 |
|
|
|
|
|
|
|
| 66 |
return output_zip
|
| 67 |
|
| 68 |
demo = gr.Interface(
|
| 69 |
fn=process_zip,
|
| 70 |
inputs=gr.File(file_types=[".zip"], label="Upload ZIP with .xlsx files"),
|
| 71 |
outputs=gr.File(label="Download Converted .xls ZIP"),
|
| 72 |
+
title="XLSX to XLS Converter",
|
| 73 |
+
description="Upload a ZIP of Excel .xlsx files. Get back a ZIP with .xls versions."
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|