Spaces:
Sleeping
Sleeping
| """ | |
| 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μμ' κΈ°λ₯μ μ¬μ©νμκΈ° λ°λλλ€.") |