hariqueen commited on
Commit
afd0a81
Β·
verified Β·
1 Parent(s): 900d5a1

Upload 5 files

Browse files
utils/__init__.py ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ from utils.file_utils import ensure_directory_exists
2
+ from utils.excel_utils import save_to_files, save_to_csv, save_to_excel
3
+ from utils.template_utils import load_erp_form_template, prepare_file_with_template
4
+ from utils.reporting_utils import print_data_summary, generate_report_file
utils/excel_utils.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Excel 파일 처리 μœ ν‹Έλ¦¬ν‹°
3
+ """
4
+ import os
5
+ import pandas as pd
6
+ from typing import Dict, Any
7
+ from core import config as cfg
8
+ from pyexcel_xls import save_data
9
+ from collections import OrderedDict
10
+
11
+ def save_to_csv(df: pd.DataFrame, output_path: str, data_count: int = 0) -> bool:
12
+ try:
13
+ df.to_csv(output_path, index=False, encoding=cfg.CSV_OUTPUT_ENCODING)
14
+ print(f"처리 μ™„λ£Œ: {data_count}개 행이 '{output_path}'에 μ €μž₯됨 ({cfg.CSV_OUTPUT_ENCODING} 인코딩)")
15
+ print(f"λ°μ΄ν„°λŠ” {cfg.ERP_DATA_ROW_START}ν–‰λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€.")
16
+ return True
17
+ except Exception as e:
18
+ print(f"CSV 파일 μ €μž₯ 쀑 였λ₯˜ λ°œμƒ: {e}")
19
+ return False
20
+
21
+ def save_to_excel(df: pd.DataFrame, output_path: str, data_count: int = 0) -> bool:
22
+ try:
23
+ # Excel 97-2003 ν˜•μ‹(.xls)으둜 μ €μž₯
24
+ xls_path = output_path.replace('.xlsx', '.xls')
25
+
26
+ # λ°μ΄ν„°ν”„λ ˆμž„μ„ 리슀트둜 λ³€ν™˜
27
+ headers = df.columns.tolist()
28
+ data = [headers] # 헀더λ₯Ό 첫 번째 ν–‰μœΌλ‘œ μΆ”κ°€
29
+
30
+ # λ°μ΄ν„°ν”„λ ˆμž„μ˜ 각 행을 리슀트둜 λ³€ν™˜ν•˜μ—¬ data에 μΆ”κ°€
31
+ for _, row in df.iterrows():
32
+ data.append(row.tolist())
33
+
34
+ # OrderedDict 생성 (Sheet1μ΄λΌλŠ” μ΄λ¦„μ˜ μ‹œνŠΈμ— 데이터 μ €μž₯)
35
+ data_dict = OrderedDict()
36
+ data_dict["Sheet1"] = data
37
+
38
+ # xls 파일둜 μ €μž₯
39
+ save_data(xls_path, data_dict)
40
+
41
+ print(f"처리 μ™„λ£Œ: {data_count}개 행이 '{xls_path}'에 μ €μž₯됨")
42
+ print(f"μ—‘μ…€ 파일이 μ„±κ³΅μ μœΌλ‘œ μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€: {os.path.abspath(xls_path)}")
43
+ return True
44
+ except Exception as e:
45
+ print(f"μ—‘μ…€ 파일 μ €μž₯ 쀑 였λ₯˜ λ°œμƒ: {e}")
46
+
47
+ # λŒ€μ²΄ μ €μž₯ μ‹œλ„ (일반 Excel ν˜•μ‹)
48
+ try:
49
+ backup_path = output_path
50
+ df.to_excel(backup_path, index=False, engine='openpyxl')
51
+ print(f"λŒ€μ²΄ ν˜•μ‹(.xlsx)으둜 파일 μ €μž₯ μ™„λ£Œ: {backup_path}")
52
+ return True
53
+ except Exception as ex:
54
+ print(f"λŒ€μ²΄ μ €μž₯도 μ‹€νŒ¨: {ex}")
55
+ return False
56
+
57
+ def save_to_files(result_df: pd.DataFrame, output_csv: str, output_excel: str, erp_data_count: int) -> None:
58
+ # CSV 파일 μ €μž₯
59
+ print(f"'{output_csv}'둜 CSV μ €μž₯ 쀑...")
60
+ csv_saved = save_to_csv(result_df, output_csv, erp_data_count)
61
+
62
+ # Excel 파일 μ €μž₯
63
+ print(f"\n'{output_excel}'둜 μ—‘μ…€ 파일 μ €μž₯ 쀑...")
64
+ excel_saved = save_to_excel(result_df, output_excel, erp_data_count)
65
+
66
+ if csv_saved and not excel_saved:
67
+ print("CSV νŒŒμΌμ€ μ •μƒμ μœΌλ‘œ μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
68
+ print("CSV νŒŒμΌμ„ μ—΄ λ•ŒλŠ” Excel의 '데이터' νƒ­μ—μ„œ 'ν…μŠ€νŠΈ/CSVμ—μ„œ' κΈ°λŠ₯을 μ‚¬μš©ν•˜μ‹œκΈ° λ°”λžλ‹ˆλ‹€.")
utils/file_utils.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ 파일 κΈ°λ³Έ 처리 μœ ν‹Έλ¦¬ν‹°
3
+ """
4
+ import os
5
+ from typing import Dict, Any
6
+
7
+ def ensure_directory_exists(dir_path: str) -> None:
8
+ """
9
+ 디렉토리가 μ—†μœΌλ©΄ 생성
10
+
11
+ Args:
12
+ dir_path: 생성할 디렉토리 경둜
13
+ """
14
+ if not os.path.exists(dir_path):
15
+ os.makedirs(dir_path)
16
+ print(f"디렉토리 생성: {dir_path}")
utils/reporting_utils.py ADDED
File without changes
utils/template_utils.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ERP 양식 및 ν…œν”Œλ¦Ώ κ΄€λ ¨ μœ ν‹Έλ¦¬ν‹°
3
+ """
4
+ import pandas as pd
5
+ from typing import Dict, Any, Optional
6
+ from core import config as cfg
7
+
8
+ def load_erp_form_template(erp_form_file: str) -> Optional[pd.DataFrame]:
9
+ """
10
+ ERP 양식 파일 λ‘œλ“œ
11
+
12
+ Args:
13
+ erp_form_file: ERP 양식 파일 경둜
14
+
15
+ Returns:
16
+ ERP 양식 λ°μ΄ν„°ν”„λ ˆμž„ λ˜λŠ” None
17
+ """
18
+ try:
19
+ erp_form = pd.read_csv(erp_form_file, encoding=cfg.DEFAULT_ENCODING)
20
+ print(f"ERP 양식 파일 '{erp_form_file}'을 μ„±κ³΅μ μœΌλ‘œ λ‘œλ“œν–ˆμŠ΅λ‹ˆλ‹€.")
21
+ return erp_form
22
+ except Exception as e:
23
+ print(f"ERP 양식 파일 λ‘œλ“œ μ‹€νŒ¨: {e}")
24
+ print("κΈ°λ³Έ 양식 없이 μ§„ν–‰ν•©λ‹ˆλ‹€.")
25
+ return None
26
+
27
+ def prepare_file_with_template(erp_df: pd.DataFrame, erp_form: Optional[pd.DataFrame]) -> pd.DataFrame:
28
+ """
29
+ ERP 양식을 μ μš©ν•˜μ—¬ 파일 μ€€λΉ„
30
+
31
+ Args:
32
+ erp_df: ERP λ°μ΄ν„°ν”„λ ˆμž„
33
+ erp_form: ERP 양식 λ°μ΄ν„°ν”„λ ˆμž„
34
+
35
+ Returns:
36
+ κ²°κ³Ό λ°μ΄ν„°ν”„λ ˆμž„
37
+ """
38
+ if erp_form is not None:
39
+ # 양식 파일의 컬럼 μˆœμ„œ μ‚¬μš©
40
+ form_columns = erp_form.columns.tolist()
41
+
42
+ # κ²°κ³Ό λ°μ΄ν„°ν”„λ ˆμž„μ„ 양식 컬럼 μˆœμ„œμ— 맞게 μž¬μ •λ ¬
43
+ for col in form_columns:
44
+ if col not in erp_df.columns:
45
+ erp_df[col] = ""
46
+
47
+ erp_df = erp_df[form_columns]
48
+
49
+ # erp_form 볡사 (양식 파일의 처음 nν–‰λ§Œ μ‚¬μš©)
50
+ result_df = erp_form.copy()
51
+
52
+ # 양식 파일이 μ§€μ •λœ μ‹œμž‘ν–‰λ³΄λ‹€ 많으면 ν•„μš”ν•œ 만큼만 μœ μ§€
53
+ if len(result_df) > cfg.ERP_DATA_ROW_START - 1:
54
+ result_df = result_df.iloc[:(cfg.ERP_DATA_ROW_START - 1)]
55
+
56
+ # 빈 ν–‰ μΆ”κ°€ (ν•„μš”ν•œ 경우)
57
+ current_rows = len(result_df)
58
+ target_rows = cfg.ERP_DATA_ROW_START - 1 # μ‹œμž‘ν–‰ - 1 (μΈλ±μŠ€λŠ” 0λΆ€ν„° μ‹œμž‘ν•˜λ―€λ‘œ)
59
+
60
+ # ν˜„μž¬ ν–‰ μˆ˜κ°€ νƒ€κ²Ÿ ν–‰ μˆ˜λ³΄λ‹€ 적으면 빈 ν–‰ μΆ”κ°€
61
+ if current_rows < target_rows:
62
+ empty_rows_needed = target_rows - current_rows
63
+ empty_df = pd.DataFrame([[""] * len(form_columns) for _ in range(empty_rows_needed)], columns=form_columns)
64
+ result_df = pd.concat([result_df, empty_df], ignore_index=True)
65
+
66
+ # 처리된 데이터 μΆ”κ°€ (ERP_DATA_ROW_STARTν–‰λΆ€ν„° μ‹œμž‘)
67
+ result_df = pd.concat([result_df, erp_df], ignore_index=True)
68
+ return result_df
69
+ else:
70
+ # 양식 파일이 μ—†λŠ” 경우 빈 λ°μ΄ν„°ν”„λ ˆμž„ 생성 ν›„ 데이터 μΆ”κ°€
71
+ # ν•„μš”ν•œ 빈 ν–‰ 생성 (ERP_DATA_ROW_START-1개의 빈 ν–‰)
72
+ empty_rows = cfg.ERP_DATA_ROW_START - 1
73
+ empty_df = pd.DataFrame([[""] * len(erp_df.columns) for _ in range(empty_rows)], columns=erp_df.columns)
74
+ result_df = pd.concat([empty_df, erp_df], ignore_index=True)
75
+ return result_df