Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import os
|
| 4 |
import shutil
|
| 5 |
import tempfile
|
| 6 |
from pathlib import Path
|
|
@@ -8,41 +7,51 @@ from zipfile import ZipFile
|
|
| 8 |
|
| 9 |
def rename_and_zip(files, excel_file):
|
| 10 |
if not excel_file:
|
| 11 |
-
return "β Mohon unggah file Excel dengan kolom A
|
| 12 |
|
| 13 |
try:
|
| 14 |
-
|
|
|
|
| 15 |
except Exception as e:
|
| 16 |
return f"β Gagal membaca Excel: {e}"
|
| 17 |
|
| 18 |
-
df.columns = ["
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
temp_dir = tempfile.mkdtemp()
|
| 24 |
renamed_dir = Path(temp_dir) / "renamed_files"
|
| 25 |
renamed_dir.mkdir(exist_ok=True)
|
| 26 |
|
| 27 |
-
for
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
new_path = renamed_dir / new_name
|
| 31 |
shutil.copy(file.name, new_path)
|
| 32 |
|
|
|
|
| 33 |
zip_path = Path(temp_dir) / "hasil_rename.zip"
|
| 34 |
with ZipFile(zip_path, "w") as zipf:
|
| 35 |
-
for
|
| 36 |
-
zipf.write(
|
| 37 |
|
| 38 |
return str(zip_path)
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
-
gr.Markdown("##
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
-
file_input = gr.File(file_count="multiple", label="π Upload
|
| 45 |
-
excel_input = gr.File(file_types=[".xls", ".xlsx"], label="π Upload Excel (Kolom A:
|
| 46 |
|
| 47 |
rename_button = gr.Button("π Proses & Rename ke ZIP")
|
| 48 |
zip_output = gr.File(label="π¦ Unduh File ZIP")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import shutil
|
| 4 |
import tempfile
|
| 5 |
from pathlib import Path
|
|
|
|
| 7 |
|
| 8 |
def rename_and_zip(files, excel_file):
|
| 9 |
if not excel_file:
|
| 10 |
+
return "β Mohon unggah file Excel dengan kolom A (nama lama) dan kolom B (nama baru)."
|
| 11 |
|
| 12 |
try:
|
| 13 |
+
# Baca 2 kolom Excel, tanpa header
|
| 14 |
+
df = pd.read_excel(excel_file.name, usecols=[0, 1], header=None)
|
| 15 |
except Exception as e:
|
| 16 |
return f"β Gagal membaca Excel: {e}"
|
| 17 |
|
| 18 |
+
df.columns = ["NamaLama", "NamaBaru"]
|
| 19 |
|
| 20 |
+
# Buat dictionary mapping nama lama -> nama baru
|
| 21 |
+
mapping = {str(row["NamaLama"]): str(row["NamaBaru"]) for _, row in df.iterrows()}
|
| 22 |
|
| 23 |
temp_dir = tempfile.mkdtemp()
|
| 24 |
renamed_dir = Path(temp_dir) / "renamed_files"
|
| 25 |
renamed_dir.mkdir(exist_ok=True)
|
| 26 |
|
| 27 |
+
for file in files:
|
| 28 |
+
original_path = Path(file.name)
|
| 29 |
+
ext = original_path.suffix
|
| 30 |
+
nama_file_tanpa_ext = original_path.stem # nama asli tanpa ekstensi
|
| 31 |
+
|
| 32 |
+
if nama_file_tanpa_ext in mapping:
|
| 33 |
+
new_name = mapping[nama_file_tanpa_ext] + ext
|
| 34 |
+
else:
|
| 35 |
+
# jika tidak cocok, tetap nama asli
|
| 36 |
+
new_name = original_path.name
|
| 37 |
+
|
| 38 |
new_path = renamed_dir / new_name
|
| 39 |
shutil.copy(file.name, new_path)
|
| 40 |
|
| 41 |
+
# Buat zip
|
| 42 |
zip_path = Path(temp_dir) / "hasil_rename.zip"
|
| 43 |
with ZipFile(zip_path, "w") as zipf:
|
| 44 |
+
for f in renamed_dir.iterdir():
|
| 45 |
+
zipf.write(f, arcname=f.name)
|
| 46 |
|
| 47 |
return str(zip_path)
|
| 48 |
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("## Rename File berdasarkan Kolom A ke Kolom B di Excel, lalu unduh ZIP")
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
+
file_input = gr.File(file_count="multiple", label="π Upload file apa saja")
|
| 54 |
+
excel_input = gr.File(file_types=[".xls", ".xlsx"], label="π Upload Excel (Kolom A: nama lama tanpa ekstensi, Kolom B: nama baru)")
|
| 55 |
|
| 56 |
rename_button = gr.Button("π Proses & Rename ke ZIP")
|
| 57 |
zip_output = gr.File(label="π¦ Unduh File ZIP")
|