0% found this document useful (0 votes)
2 views2 pages

ceban

The document is a Python script that processes Excel files in a specified directory to search for specific keywords. It uses the pandas library to read the files and checks each cell in the sheets for the presence of these keywords, collecting results in a structured format. Finally, it saves the results to a new Excel file if any matches are found.

Uploaded by

tjarselan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

ceban

The document is a Python script that processes Excel files in a specified directory to search for specific keywords. It uses the pandas library to read the files and checks each cell in the sheets for the presence of these keywords, collecting results in a structured format. Finally, it saves the results to a new Excel file if any matches are found.

Uploaded by

tjarselan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import os

import pandas as pd
from tqdm import tqdm
import warnings
from colorama import init, Fore

# Inisialisasi colorama untuk mendukung warna di terminal


init(autoreset=True)

# Abaikan warning dari openpyxl jika tidak ada default style


warnings.filterwarnings("ignore", category=UserWarning, module="openpyxl")

KEYWORDS = {
"WGI003", "SSU012", "OSU011", "SSU006", "WTP164", "COI843", "SKG203", "NTI034",
"ISM012", "ISM057", "KTG037", "MDO467", "NTI045", "SKG044", "THN115", "NTI005",
"PKJ066", "SNS029", "SSU010", "SSU011", "PAL019", "SDR025", "COI018", "COI019",
"MRW037", "SSU123", "MBA040", "BIT012", "LBA013", "COI016"
}
KEYWORDS_LOWER = [k.lower() for k in KEYWORDS]
FOLDER_PATH = r"D:\TI\Pre sales"

def contains_keyword(value):
val = str(value).strip().lower()
return any(k in val for k in KEYWORDS_LOWER)

def process_excel_file(file_path):
results = []
try:
xl = pd.ExcelFile(file_path)
for sheet_name in xl.sheet_names:
try:
df = xl.parse(sheet_name, dtype=str).fillna('')
arr = df.to_numpy(dtype=str)
for row_idx, row in enumerate(arr):
for col_idx, cell in enumerate(row):
if contains_keyword(cell):
results.append({
"File": os.path.basename(file_path),
"Path": file_path,
"Sheet": sheet_name,
"Kolom": df.columns[col_idx],
"Baris": row_idx + 2,
"Isi": cell
})
except Exception:
continue
except Exception:
pass
return results

def get_all_excel_files(root_dir):
excel_files = []
for root, _, files in os.walk(root_dir):
for file in files:
if file.endswith((".xlsx", ".xls", ".xlsm")) and not
file.startswith("~$"):
excel_files.append(os.path.join(root, file))
return excel_files
def main():
files = get_all_excel_files(FOLDER_PATH)
print(f"🔍 Ditemukan {len(files)} file Excel untuk diproses...")

all_results = []
for file in tqdm(files, desc=f"{Fore.GREEN}🔄 Memproses", ncols=80,
colour="green"):
result = process_excel_file(file)
all_results.extend(result)

if all_results:
df_result = pd.DataFrame(all_results)
output_file = os.path.join(FOLDER_PATH, "hasil_pencarian_kode.xlsx")
df_result.to_excel(output_file, index=False)
print(f"\n✅ Ditemukan {len(all_results)} kecocokan. Hasil disimpan di:\
n{output_file}")
else:
print("❌ Tidak ditemukan kecocokan di file Excel mana pun.")

if __name__ == "__main__":
main()

You might also like