Saditiya commited on
Commit
7f8fc26
Β·
verified Β·
1 Parent(s): 84b2b60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
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 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"
25
  renamed_dir.mkdir(exist_ok=True)
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():
36
- zipf.write(file, arcname=file.name)
37
 
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")
 
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")