diff --git "a/a701_\344\275\234\346\245\255\344\270\255\346\264\273\351\240\201\346\252\224\345\241\253\345\205\245\346\274\242\345\255\227.py" "b/a701_\344\275\234\346\245\255\344\270\255\346\264\273\351\240\201\346\252\224\345\241\253\345\205\245\346\274\242\345\255\227.py" index b7174d4..3cc66e5 100644 --- "a/a701_\344\275\234\346\245\255\344\270\255\346\264\273\351\240\201\346\252\224\345\241\253\345\205\245\346\274\242\345\255\227.py" +++ "b/a701_\344\275\234\346\245\255\344\270\255\346\264\273\351\240\201\346\252\224\345\241\253\345\205\245\346\274\242\345\255\227.py" @@ -1,88 +1,148 @@ +import logging import os import sys from pathlib import Path +# 載入第三方套件 import xlwings as xw +from dotenv import load_dotenv +# 載入自訂模組 from mod_file_access import get_han_ji_khoo, get_sound_type, save_as_new_file from p701_Clear_Cells import clear_han_ji_kap_piau_im from p702_Ca_Han_Ji_Thak_Im import ca_han_ji_thak_im from p709_reset_han_ji_cells import reset_han_ji_cells from p710_thiam_han_ji import fill_hanji_in_cells +# ========================================================================= +# 載入環境變數 +# ========================================================================= +load_dotenv() + +# 預設檔案名稱從環境變數讀取 +DB_HO_LOK_UE = os.getenv('DB_HO_LOK_UE', 'Ho_Lok_Ue.db') +DB_KONG_UN = os.getenv('DB_KONG_UN', 'Kong_Un.db') + +# ========================================================================= +# 設定日誌 +# ========================================================================= +logging.basicConfig( + filename='process_log.txt', + level=logging.INFO, + format='%(asctime)s - %(levelname)s - %(message)s' +) + +# ========================================================================= +# 定義 Exit Code +# ========================================================================= EXIT_CODE_SUCCESS = 0 # 成功 EXIT_CODE_NO_FILE = 1 # 無法找到檔案 EXIT_CODE_INVALID_INPUT = 2 # 輸入錯誤 EXIT_CODE_PROCESS_FAILURE = 3 # 過程失敗 EXIT_CODE_UNKNOWN_ERROR = 99 # 未知錯誤 + def main(): + logging.info("作業開始") + # ========================================================================= # (1) 取得專案根目錄 # ========================================================================= current_file_path = Path(__file__).resolve() project_root = current_file_path.parent print(f"專案根目錄為: {project_root}") + logging.info(f"專案根目錄為: {project_root}") # ========================================================================= - # (2) 若無指定輸入檔案,則獲取當前作用中的 Excel 檔案並另存新檔 + # (2) 嘗試獲取當前作用中的 Excel 工作簿 # ========================================================================= + wb = None try: wb = xw.apps.active.books.active except Exception as e: - print(f"發生錯誤: {e}") + logging.error(f"無法找到作用中的 Excel 工作簿: {e}", exc_info=True) print("無法找到作用中的 Excel 工作簿") return EXIT_CODE_NO_FILE if not wb: print("無法作業,原因可能為:(1) 未指定輸入檔案;(2) 未找到作用中的 Excel 工作簿!") + logging.error("無法作業,未指定輸入檔案或 Excel 無效。") return EXIT_CODE_NO_FILE try: + # ========================================================================= + # (3) 讀取儲存格資料 + # ========================================================================= cell_value = wb.sheets['漢字注音'].range('V3').value - # 判斷是否為 None 並執行處理 if cell_value is None: print("【待注音漢字】儲存格未填入文字,作業無法繼續。") + logging.warning("【待注音漢字】儲存格為空") return EXIT_CODE_INVALID_INPUT - else: - value_length = len(cell_value.strip()) - print(f"【待注音漢字】總字數為: {value_length}") - # 將儲存格已填入之漢字及標音清除 + value_length = len(cell_value.strip()) + print(f"【待注音漢字】總字數為: {value_length}") + logging.info(f"【待注音漢字】總字數為: {value_length}") + + # ========================================================================= + # (4) 執行 Excel 作業 + # ========================================================================= + print("正在清除儲存格...") clear_han_ji_kap_piau_im(wb) + logging.info("儲存格清除完畢") - # 將待注音的【漢字儲存格】重設格式 + print("重設格式...") reset_han_ji_cells(wb) + logging.info("儲存格格式重設完畢") - # 將待注音的漢字填入 + print("填入待注音漢字...") fill_hanji_in_cells(wb) + logging.info("填入待注音漢字完成") - # A731: 自動為漢字查找讀音 + # ========================================================================= + # (5) 自動查找讀音 + # ========================================================================= type = get_sound_type(wb) han_ji_khoo = get_han_ji_khoo(wb) - if han_ji_khoo == "河洛話" and type in ["白話音", "文讀音"]: - ca_han_ji_thak_im(wb, sheet_name='漢字注音', cell='V3', hue_im=type, han_ji_khoo="河洛話", - db_name='Ho_Lok_Ue.db', module_name='mod_河洛話', function_name='han_ji_ca_piau_im') - elif han_ji_khoo == "廣韻": - ca_han_ji_thak_im(wb, sheet_name='漢字注音', cell='V3', hue_im="文讀音", han_ji_khoo="廣韻", - db_name='Kong_Un.db', module_name='mod_廣韻', function_name='han_ji_ca_piau_im') + + if han_ji_khoo in ["河洛話", "廣韻"]: + db_name = DB_HO_LOK_UE if han_ji_khoo == "河洛話" else DB_KONG_UN + module_name = 'mod_河洛話' if han_ji_khoo == "河洛話" else 'mod_廣韻' + hue_im = type if han_ji_khoo == "河洛話" else "文讀音" + + logging.info(f"開始標音作業 - {han_ji_khoo}: {type}") + ca_han_ji_thak_im(wb, sheet_name='漢字注音', cell='V3', hue_im=hue_im, + han_ji_khoo=han_ji_khoo, db_name=db_name, + module_name=module_name, function_name='han_ji_ca_piau_im') + logging.info(f"標音作業完成 - {han_ji_khoo}: {type}") else: - print("無法執行漢字標音作業,請確認【env】工作表【語音類型】及【漢字庫】欄位的設定是否正確!") + print("無法執行漢字標音作業,請確認設定是否正確!") + logging.warning("無法執行標音作業,檢查【env】設定。") return EXIT_CODE_PROCESS_FAILURE - # 將檔案存放路徑設為【專案根目錄】之下 + # ========================================================================= + # (6) 儲存檔案 + # ========================================================================= + print("儲存檔案...") save_as_new_file(wb=wb) + logging.info("檔案已成功儲存") except Exception as e: print(f"執行過程中發生未知錯誤: {e}") + logging.error(f"執行過程中發生未知錯誤: {e}", exc_info=True) return EXIT_CODE_UNKNOWN_ERROR - return EXIT_CODE_SUCCESS + finally: + if wb: + # 是否關閉 Excel 視窗可根據需求決定 + xw.apps.active.quit() # 確保 Excel 被釋放資源,避免開啟殘留 + + return EXIT_CODE_SUCCESS + if __name__ == "__main__": exit_code = main() - if exit_code == 0: + if exit_code == EXIT_CODE_SUCCESS: print("作業成功完成!") else: print(f"程式結束,代碼: {exit_code}") diff --git a/output7/working.xlsx b/output7/working.xlsx index 569d5dc..441efbb 100644 Binary files a/output7/working.xlsx and b/output7/working.xlsx differ diff --git "a/output7/\343\200\220\346\262\263\346\264\233\347\231\275\350\251\261\346\263\250\351\237\263-\351\233\205\344\277\227\351\200\232\343\200\221\343\200\212\345\260\232\346\234\211\345\212\233\351\207\217\347\232\204\346\257\217\346\227\245\347\245\210\347\246\261\346\226\207\343\200\213.xlsx" "b/output7/\343\200\220\346\262\263\346\264\233\347\231\275\350\251\261\346\263\250\351\237\263-\351\233\205\344\277\227\351\200\232\343\200\221\343\200\212\345\260\232\346\234\211\345\212\233\351\207\217\347\232\204\346\257\217\346\227\245\347\245\210\347\246\261\346\226\207\343\200\213.xlsx" index 3410ec6..d32f01d 100644 Binary files "a/output7/\343\200\220\346\262\263\346\264\233\347\231\275\350\251\261\346\263\250\351\237\263-\351\233\205\344\277\227\351\200\232\343\200\221\343\200\212\345\260\232\346\234\211\345\212\233\351\207\217\347\232\204\346\257\217\346\227\245\347\245\210\347\246\261\346\226\207\343\200\213.xlsx" and "b/output7/\343\200\220\346\262\263\346\264\233\347\231\275\350\251\261\346\263\250\351\237\263-\351\233\205\344\277\227\351\200\232\343\200\221\343\200\212\345\260\232\346\234\211\345\212\233\351\207\217\347\232\204\346\257\217\346\227\245\347\245\210\347\246\261\346\226\207\343\200\213.xlsx" differ diff --git a/process_log.txt b/process_log.txt new file mode 100644 index 0000000..334aada --- /dev/null +++ b/process_log.txt @@ -0,0 +1,34 @@ +2025-01-10 13:16:10,153 - INFO - 作業開始 +2025-01-10 13:16:10,154 - INFO - 專案根目錄為: C:\work\Piau-Im +2025-01-10 13:16:10,192 - INFO - 【待注音漢字】總字數為: 143 +2025-01-10 13:16:10,286 - ERROR - 執行過程中發生未知錯誤: (-2147352567, '發生例外狀況。', (0, None, None, None, 0, -2146827284), None) +Traceback (most recent call last): + File "a701_作業中活頁檔填入漢字.py", line 91, in main + clear_han_ji_kap_piau_im(wb) + ~~~~~~~~~~~~~~~~~~~~~~~~^^^^ + File "c:\work\Piau-Im\p701_Clear_Cells.py", line 15, in clear_han_ji_kap_piau_im + sheet.range(cells_range).clear_contents() # 清除 C3:R{end_of_row} 範圍的內容 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^ + File "c:\work\Piau-Im\.venv\Lib\site-packages\xlwings\main.py", line 2048, in clear_contents + return self.impl.clear_contents() + ~~~~~~~~~~~~~~~~~~~~~~~~^^ + File "c:\work\Piau-Im\.venv\Lib\site-packages\xlwings\_xlwindows.py", line 1213, in clear_contents + self.xl.ClearContents() + ~~~~~~~~~~~~~~~~~~~~~^^ + File "c:\work\Piau-Im\.venv\Lib\site-packages\xlwings\_xlwindows.py", line 122, in __call__ + v = self.__method(*args, **kwargs) + File "", line 2, in ClearContents + File "c:\work\Piau-Im\.venv\Lib\site-packages\win32com\client\dynamic.py", line 355, in _ApplyTypes_ + result = self._oleobj_.InvokeTypes( + *(dispid, LCID, wFlags, retType, argTypes) + args + ) +pywintypes.com_error: (-2147352567, '發生例外狀況。', (0, None, None, None, 0, -2146827284), None) +2025-01-10 13:17:02,147 - INFO - 作業開始 +2025-01-10 13:17:02,148 - INFO - 專案根目錄為: C:\work\Piau-Im +2025-01-10 13:17:02,184 - INFO - 【待注音漢字】總字數為: 143 +2025-01-10 13:17:02,368 - INFO - 儲存格清除完畢 +2025-01-10 13:17:10,237 - INFO - 儲存格格式重設完畢 +2025-01-10 13:17:26,865 - INFO - 填入待注音漢字完成 +2025-01-10 13:17:26,908 - INFO - 開始標音作業 - 河洛話: 白話音 +2025-01-10 13:17:46,412 - INFO - 標音作業完成 - 河洛話: 白話音 +2025-01-10 13:17:46,761 - INFO - 檔案已成功儲存