58 lines
2.0 KiB
Python
58 lines
2.0 KiB
Python
import pandas as pd
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# --- CONFIGURATION ---
|
|
EXCEL_FILE = Path("/vol1/1002/入貨單/供應商結算方式.xlsx")
|
|
JSON_DB_PATH = Path("/vol1/@apphome/trim.openclaw/data/workspace/memory/payment_methods.json")
|
|
|
|
def build_payment_db():
|
|
"""
|
|
Reads the supplier payment methods Excel and converts it to a JSON database.
|
|
"""
|
|
print(f"[*] Starting conversion: {EXCEL_FILE} -> {JSON_DB_PATH}")
|
|
|
|
if not EXCEL_FILE.exists():
|
|
print(f"[!] Error: Excel file not found at {EXCEL_FILE}")
|
|
return False
|
|
|
|
try:
|
|
# Ensure parent directory for JSON exists
|
|
JSON_DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Read Excel. Based on previous info:
|
|
# Row 0 is header. Column 0 is Supplier, Column 1 is Method.
|
|
df = pd.read_excel(EXCEL_FILE)
|
|
|
|
# We need to handle potential variations in column names.
|
|
# Based on user input: Column 1 (index 0) is Supplier, Column 2 (index 1) is Method.
|
|
# Let's use position-based indexing to be safe.
|
|
|
|
methods = {}
|
|
for _, row in df.iterrows():
|
|
# Use iloc to get by position: 0 for first col, 1 for second col
|
|
supplier = str(row.iloc[0]).strip()
|
|
method = str(row.iloc[1]).strip()
|
|
|
|
# Skip empty or header-like rows
|
|
if not supplier or supplier in ["供應商", "Supplier", ""] or method in ["付款方式", "Method", ""]:
|
|
continue
|
|
|
|
methods[supplier] = method
|
|
|
|
# Write to JSON
|
|
with open(JSON_DB_PATH, 'w', encoding='utf-8') as f:
|
|
json.dump(methods, f, ensure_ascii=False, indent=2)
|
|
|
|
print(f"[+] Success! Created JSON database with {len(methods)} mappings.")
|
|
print(f" Saved to: {JSON_DB_PATH}")
|
|
return True
|
|
|
|
except Exception as e:
|
|
print(f"[!] Error during conversion: {e}")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
build_payment_db()
|