Spaces:
Sleeping
Sleeping
File size: 4,422 Bytes
bd6d9a2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
"""
νμΌ μ
μΆλ ₯ κ΄λ ¨ λͺ¨λ
"""
import os
import pandas as pd
from typing import Dict, Any
import config as cfg
def load_erp_form_template(erp_form_file: str) -> pd.DataFrame:
"""
ERP μμ νμΌ λ‘λ
Args:
erp_form_file: ERP μμ νμΌ κ²½λ‘
Returns:
ERP μμ λ°μ΄ν°νλ μ
"""
try:
erp_form = pd.read_csv(erp_form_file, encoding=cfg.DEFAULT_ENCODING)
print(f"ERP μμ νμΌ '{erp_form_file}'μ μ±κ³΅μ μΌλ‘ λ‘λνμ΅λλ€.")
return erp_form
except Exception as e:
print(f"ERP μμ νμΌ λ‘λ μ€ν¨: {e}")
print("κΈ°λ³Έ μμ μμ΄ μ§νν©λλ€.")
return None
def save_to_files(result_df: pd.DataFrame, output_csv: str, output_excel: str, erp_data_count: int) -> None:
"""
κ²°κ³Όλ₯Ό CSV λ° Excel νμΌλ‘ μ μ₯
Args:
result_df: κ²°κ³Ό λ°μ΄ν°νλ μ
output_csv: CSV μΆλ ₯ νμΌ κ²½λ‘
output_excel: Excel μΆλ ₯ νμΌ κ²½λ‘
erp_data_count: ERP λ°μ΄ν° ν μ
Returns:
None
"""
# CSV νμΌ μ μ₯
print(f"'{output_csv}'λ‘ CSV μ μ₯ μ€...")
try:
result_df.to_csv(output_csv, index=False, encoding=cfg.CSV_OUTPUT_ENCODING)
print(f"μ²λ¦¬ μλ£: {erp_data_count}κ° νμ΄ '{output_csv}'μ μ μ₯λ¨ ({cfg.CSV_OUTPUT_ENCODING} μΈμ½λ©)")
print(f"λ°μ΄ν°λ {cfg.ERP_DATA_ROW_START}νλΆν° μμν©λλ€.")
except Exception as e:
print(f"CSV νμΌ μ μ₯ μ€ μ€λ₯ λ°μ: {e}")
# Excel νμΌ μ μ₯
print(f"\n'{output_excel}'λ‘ μμ
νμΌ μ μ₯ μ€...")
try:
result_df.to_excel(output_excel, index=False, engine='openpyxl')
print(f"μ²λ¦¬ μλ£: {erp_data_count}κ° νμ΄ '{output_excel}'μ μ μ₯λ¨")
print(f"μμ
νμΌμ΄ μ±κ³΅μ μΌλ‘ μμ±λμμ΅λλ€: {os.path.abspath(output_excel)}")
except Exception as e:
print(f"μμ
νμΌ μ μ₯ μ€ μ€λ₯ λ°μ: {e}")
print("CSV νμΌμ μ μμ μΌλ‘ μ μ₯λμμ΅λλ€.")
print("CSV νμΌμ μ΄ λλ Excelμ 'λ°μ΄ν°' νμμ 'ν
μ€νΈ/CSVμμ' κΈ°λ₯μ μ¬μ©νμκΈ° λ°λλλ€.")
def prepare_file_with_template(erp_df: pd.DataFrame, erp_form: pd.DataFrame) -> pd.DataFrame:
"""
ERP μμμ μ μ©νμ¬ νμΌ μ€λΉ
Args:
erp_df: ERP λ°μ΄ν°νλ μ
erp_form: ERP μμ λ°μ΄ν°νλ μ
Returns:
κ²°κ³Ό λ°μ΄ν°νλ μ
"""
if erp_form is not None:
# μμ νμΌμ μ»¬λΌ μμ μ¬μ©
form_columns = erp_form.columns.tolist()
# κ²°κ³Ό λ°μ΄ν°νλ μμ μμ μ»¬λΌ μμμ λ§κ² μ¬μ λ ¬
for col in form_columns:
if col not in erp_df.columns:
erp_df[col] = ""
erp_df = erp_df[form_columns]
# erp_form λ³΅μ¬ (μμ νμΌμ μ²μ 4νλ§ μ¬μ©)
result_df = erp_form.copy()
# μμ νμΌμ΄ 4νλ³΄λ€ λ§μΌλ©΄ 4νλ§ μ μ§
if len(result_df) > cfg.ERP_DATA_ROW_START - 1:
result_df = result_df.iloc[:(cfg.ERP_DATA_ROW_START - 1)]
# λΉ ν μΆκ° (νμν κ²½μ°)
current_rows = len(result_df)
target_rows = cfg.ERP_DATA_ROW_START - 1 # μμν - 1 (μΈλ±μ€λ 0λΆν° μμνλ―λ‘)
# νμ¬ ν μκ° νκ² ν μλ³΄λ€ μ μΌλ©΄ λΉ ν μΆκ°
if current_rows < target_rows:
empty_rows_needed = target_rows - current_rows
empty_df = pd.DataFrame([[""] * len(form_columns) for _ in range(empty_rows_needed)], columns=form_columns)
result_df = pd.concat([result_df, empty_df], ignore_index=True)
# μ²λ¦¬λ λ°μ΄ν° μΆκ° (ERP_DATA_ROW_STARTνλΆν° μμ)
result_df = pd.concat([result_df, erp_df], ignore_index=True)
return result_df
else:
# μμ νμΌμ΄ μλ κ²½μ° λΉ λ°μ΄ν°νλ μ μμ± ν λ°μ΄ν° μΆκ°
# νμν λΉ ν μμ± (ERP_DATA_ROW_START-1κ°μ λΉ ν)
empty_rows = cfg.ERP_DATA_ROW_START - 1
empty_df = pd.DataFrame([[""] * len(erp_df.columns) for _ in range(empty_rows)], columns=erp_df.columns)
result_df = pd.concat([empty_df, erp_df], ignore_index=True)
return result_df |