File size: 2,912 Bytes
afd0a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Excel 파일 처리 μœ ν‹Έλ¦¬ν‹°

"""
import os
import pandas as pd
from typing import Dict, Any
from core import config as cfg
from pyexcel_xls import save_data
from collections import OrderedDict

def save_to_csv(df: pd.DataFrame, output_path: str, data_count: int = 0) -> bool:
    try:
        df.to_csv(output_path, index=False, encoding=cfg.CSV_OUTPUT_ENCODING)
        print(f"처리 μ™„λ£Œ: {data_count}개 행이 '{output_path}'에 μ €μž₯됨 ({cfg.CSV_OUTPUT_ENCODING} 인코딩)")
        print(f"λ°μ΄ν„°λŠ” {cfg.ERP_DATA_ROW_START}ν–‰λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€.")
        return True
    except Exception as e:
        print(f"CSV 파일 μ €μž₯ 쀑 였λ₯˜ λ°œμƒ: {e}")
        return False

def save_to_excel(df: pd.DataFrame, output_path: str, data_count: int = 0) -> bool:
    try:
        # Excel 97-2003 ν˜•μ‹(.xls)으둜 μ €μž₯
        xls_path = output_path.replace('.xlsx', '.xls')
        
        # λ°μ΄ν„°ν”„λ ˆμž„μ„ 리슀트둜 λ³€ν™˜
        headers = df.columns.tolist()
        data = [headers]  # 헀더λ₯Ό 첫 번째 ν–‰μœΌλ‘œ μΆ”κ°€
        
        # λ°μ΄ν„°ν”„λ ˆμž„μ˜ 각 행을 리슀트둜 λ³€ν™˜ν•˜μ—¬ data에 μΆ”κ°€
        for _, row in df.iterrows():
            data.append(row.tolist())
        
        # OrderedDict 생성 (Sheet1μ΄λΌλŠ” μ΄λ¦„μ˜ μ‹œνŠΈμ— 데이터 μ €μž₯)
        data_dict = OrderedDict()
        data_dict["Sheet1"] = data
        
        # xls 파일둜 μ €μž₯
        save_data(xls_path, data_dict)
        
        print(f"처리 μ™„λ£Œ: {data_count}개 행이 '{xls_path}'에 μ €μž₯됨")
        print(f"μ—‘μ…€ 파일이 μ„±κ³΅μ μœΌλ‘œ μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€: {os.path.abspath(xls_path)}")
        return True
    except Exception as e:
        print(f"μ—‘μ…€ 파일 μ €μž₯ 쀑 였λ₯˜ λ°œμƒ: {e}")
        
        # λŒ€μ²΄ μ €μž₯ μ‹œλ„ (일반 Excel ν˜•μ‹)
        try:
            backup_path = output_path
            df.to_excel(backup_path, index=False, engine='openpyxl')
            print(f"λŒ€μ²΄ ν˜•μ‹(.xlsx)으둜 파일 μ €μž₯ μ™„λ£Œ: {backup_path}")
            return True
        except Exception as ex:
            print(f"λŒ€μ²΄ μ €μž₯도 μ‹€νŒ¨: {ex}")
            return False

def save_to_files(result_df: pd.DataFrame, output_csv: str, output_excel: str, erp_data_count: int) -> None:
    # CSV 파일 μ €μž₯
    print(f"'{output_csv}'둜 CSV μ €μž₯ 쀑...")
    csv_saved = save_to_csv(result_df, output_csv, erp_data_count)
    
    # Excel 파일 μ €μž₯
    print(f"\n'{output_excel}'둜 μ—‘μ…€ 파일 μ €μž₯ 쀑...")
    excel_saved = save_to_excel(result_df, output_excel, erp_data_count)
    
    if csv_saved and not excel_saved:
        print("CSV νŒŒμΌμ€ μ •μƒμ μœΌλ‘œ μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
        print("CSV νŒŒμΌμ„ μ—΄ λ•ŒλŠ” Excel의 '데이터' νƒ­μ—μ„œ 'ν…μŠ€νŠΈ/CSVμ—μ„œ' κΈ°λŠ₯을 μ‚¬μš©ν•˜μ‹œκΈ° λ°”λžλ‹ˆλ‹€.")