Update app.py
Browse files
app.py
CHANGED
|
@@ -8,17 +8,17 @@ 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 |
-
df = pd.read_excel(excel_file.name, usecols=[0
|
| 15 |
except Exception as e:
|
| 16 |
return f"β Gagal membaca Excel: {e}"
|
| 17 |
|
| 18 |
-
df.columns = ["Nomor"
|
| 19 |
|
| 20 |
if len(df) < len(files):
|
| 21 |
-
return f"β Jumlah
|
| 22 |
|
| 23 |
temp_dir = tempfile.mkdtemp()
|
| 24 |
renamed_dir = Path(temp_dir) / "renamed_files"
|
|
@@ -26,11 +26,10 @@ def rename_and_zip(files, excel_file):
|
|
| 26 |
|
| 27 |
for i, file in enumerate(files):
|
| 28 |
ext = Path(file.name).suffix
|
| 29 |
-
new_name = f"{df.iloc[i]['
|
| 30 |
new_path = renamed_dir / new_name
|
| 31 |
shutil.copy(file.name, new_path)
|
| 32 |
|
| 33 |
-
# Buat ZIP
|
| 34 |
zip_path = Path(temp_dir) / "hasil_rename.zip"
|
| 35 |
with ZipFile(zip_path, "w") as zipf:
|
| 36 |
for file in renamed_dir.iterdir():
|
|
@@ -39,11 +38,11 @@ def rename_and_zip(files, excel_file):
|
|
| 39 |
return str(zip_path)
|
| 40 |
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
-
gr.Markdown("##
|
| 43 |
|
| 44 |
with gr.Row():
|
| 45 |
-
file_input = gr.File(file_count="multiple", label="π Upload hingga 100 file
|
| 46 |
-
excel_input = gr.File(file_types=[".xls", ".xlsx"], label="π Upload Excel (Kolom A: Nomor
|
| 47 |
|
| 48 |
rename_button = gr.Button("π Proses & Rename ke ZIP")
|
| 49 |
zip_output = gr.File(label="π¦ Unduh File ZIP")
|
|
|
|
| 8 |
|
| 9 |
def rename_and_zip(files, excel_file):
|
| 10 |
if not excel_file:
|
| 11 |
+
return "β Mohon unggah file Excel dengan kolom A berisi angka untuk nama file."
|
| 12 |
|
| 13 |
try:
|
| 14 |
+
df = pd.read_excel(excel_file.name, usecols=[0], header=None)
|
| 15 |
except Exception as e:
|
| 16 |
return f"β Gagal membaca Excel: {e}"
|
| 17 |
|
| 18 |
+
df.columns = ["Nomor"]
|
| 19 |
|
| 20 |
if len(df) < len(files):
|
| 21 |
+
return f"β Jumlah nomor di Excel ({len(df)}) kurang dari jumlah file ({len(files)})."
|
| 22 |
|
| 23 |
temp_dir = tempfile.mkdtemp()
|
| 24 |
renamed_dir = Path(temp_dir) / "renamed_files"
|
|
|
|
| 26 |
|
| 27 |
for i, file in enumerate(files):
|
| 28 |
ext = Path(file.name).suffix
|
| 29 |
+
new_name = f"{df.iloc[i]['Nomor']}{ext}"
|
| 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 file in renamed_dir.iterdir():
|
|
|
|
| 38 |
return str(zip_path)
|
| 39 |
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
+
gr.Markdown("## π’ Rename File Sesuai Angka di Kolom A Excel dan Download ZIP")
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
+
file_input = gr.File(file_count="multiple", label="π Upload hingga 100 file")
|
| 45 |
+
excel_input = gr.File(file_types=[".xls", ".xlsx"], label="π Upload Excel (Kolom A: Nomor untuk nama file)")
|
| 46 |
|
| 47 |
rename_button = gr.Button("π Proses & Rename ke ZIP")
|
| 48 |
zip_output = gr.File(label="π¦ Unduh File ZIP")
|