Skip to content

Commit

Permalink
優化程式碼架構
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanJui committed Jan 10, 2025
1 parent 69b66d4 commit f797b96
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 71 deletions.
46 changes: 27 additions & 19 deletions a700_重置漢字標音工作表.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,32 @@
#--------------------------------------------------------------------------
reset_han_ji_cells(wb)

# 將檔案存放路徑設為【專案根目錄】之下
try:
file_name = str(wb.names['TITLE'].refers_to_range.value).strip()
except KeyError:
print("未找到命名範圍 'TITLE',使用預設名稱")
# file_name = "Tai_Gi_Zu_Im_Bun.xlsx" # 提供一個預設檔案名稱
setting_sheet = wb.sheets["env"]
file_name = str(
setting_sheet.range("C4").value
).strip()

# 設定檔案輸出路徑,存於專案根目錄下的 output2 資料夾
output_path = wb.names['OUTPUT_PATH'].refers_to_range.value
new_file_path = os.path.join(
".\\{0}".format(output_path),
f"【河洛話注音】{file_name}.xlsx")

# 儲存新建立的工作簿
wb.save(new_file_path)
wb.save()

print(f"待注音漢字已備妥: {new_file_path}")
print(f"【漢字注音】工作表已清空、重置!")
#--------------------------------------------------------------------------
# 另存新檔
#--------------------------------------------------------------------------
# 將待注音的【漢字儲存格】,文字顏色重設為黑色(自動 RGB: 0, 0, 0);填漢顏色重設為無填滿
# # 將檔案存放路徑設為【專案根目錄】之下
# try:
# file_name = str(wb.names['TITLE'].refers_to_range.value).strip()
# except KeyError:
# print("未找到命名範圍 'TITLE',使用預設名稱")
# # file_name = "Tai_Gi_Zu_Im_Bun.xlsx" # 提供一個預設檔案名稱
# setting_sheet = wb.sheets["env"]
# file_name = str(
# setting_sheet.range("C4").value
# ).strip()

# # 設定檔案輸出路徑,存於專案根目錄下的 output2 資料夾
# output_path = wb.names['OUTPUT_PATH'].refers_to_range.value
# new_file_path = os.path.join(
# ".\\{0}".format(output_path),
# f"【河洛話注音】{file_name}.xlsx")

# # 儲存新建立的工作簿
# wb.save(new_file_path)

# print(f"待注音漢字已備妥: {new_file_path}")
129 changes: 77 additions & 52 deletions a701_作業中活頁檔填入漢字.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,80 @@
from p709_reset_han_ji_cells import reset_han_ji_cells
from p710_thiam_han_ji import fill_hanji_in_cells

# =========================================================================
# (1) 取得專案根目錄。
# =========================================================================
# 獲取當前檔案的路徑
current_file_path = Path(__file__).resolve()

# 專案根目錄
project_root = current_file_path.parent

print(f"專案根目錄為: {project_root}")

# =========================================================================
# (2) 若無指定輸入檔案,則獲取當前作用中的 Excel 檔案並另存新檔。
# =========================================================================
wb = None
# 使用已打開且處於作用中的 Excel 工作簿
try:
# 嘗試獲取當前作用中的 Excel 工作簿
wb = xw.apps.active.books.active
except Exception as e:
print(f"發生錯誤: {e}")
print("無法找到作用中的 Excel 工作簿")
sys.exit(2)

if not wb:
print("無法執行,可能原因:(1) 未指定輸入檔案;(2) 未找到作用中的 Excel 工作簿")
sys.exit(2)

# 將儲存格已填入之漢字及標音清除
clear_han_ji_kap_piau_im(wb)

# 將待注音的【漢字儲存格】,文字顏色重設為黑色(自動 RGB: 0, 0, 0);填漢顏色重設為無填滿
reset_han_ji_cells(wb)

# 將待注音的漢字填入
fill_hanji_in_cells(wb)

# A731: 自動為漢字查找讀音,並抄寫到漢字的上方(拼音)及下方(注音)。
type = get_sound_type(wb)
han_ji_khoo = get_han_ji_khoo(wb)
if han_ji_khoo == "河洛話" and type == "白話音":
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 == "河洛話" and type == "文讀音":
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')
else:
print("無法執行漢字標音作業,請確認【env】工作表【語音類型】及【漢字庫】欄位的設定是否正確!")
# sys.exit(2)

# 將檔案存放路徑設為【專案根目錄】之下
save_as_new_file(wb=wb)
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():
# =========================================================================
# (1) 取得專案根目錄
# =========================================================================
current_file_path = Path(__file__).resolve()
project_root = current_file_path.parent
print(f"專案根目錄為: {project_root}")

# =========================================================================
# (2) 若無指定輸入檔案,則獲取當前作用中的 Excel 檔案並另存新檔
# =========================================================================
try:
wb = xw.apps.active.books.active
except Exception as e:
print(f"發生錯誤: {e}")
print("無法找到作用中的 Excel 工作簿")
return EXIT_CODE_NO_FILE

if not wb:
print("無法作業,原因可能為:(1) 未指定輸入檔案;(2) 未找到作用中的 Excel 工作簿!")
return EXIT_CODE_NO_FILE

try:
cell_value = wb.sheets['漢字注音'].range('V3').value

# 判斷是否為 None 並執行處理
if cell_value is None:
print("【待注音漢字】儲存格未填入文字,作業無法繼續。")
return EXIT_CODE_INVALID_INPUT
else:
value_length = len(cell_value.strip())
print(f"【待注音漢字】總字數為: {value_length}")

# 將儲存格已填入之漢字及標音清除
clear_han_ji_kap_piau_im(wb)

# 將待注音的【漢字儲存格】重設格式
reset_han_ji_cells(wb)

# 將待注音的漢字填入
fill_hanji_in_cells(wb)

# A731: 自動為漢字查找讀音
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')
else:
print("無法執行漢字標音作業,請確認【env】工作表【語音類型】及【漢字庫】欄位的設定是否正確!")
return EXIT_CODE_PROCESS_FAILURE

# 將檔案存放路徑設為【專案根目錄】之下
save_as_new_file(wb=wb)

except Exception as e:
print(f"執行過程中發生未知錯誤: {e}")
return EXIT_CODE_UNKNOWN_ERROR

return EXIT_CODE_SUCCESS

if __name__ == "__main__":
exit_code = main()
if exit_code == 0:
print("作業成功完成!")
else:
print(f"程式結束,代碼: {exit_code}")
sys.exit(exit_code)
89 changes: 89 additions & 0 deletions a701_作業中活頁檔填入漢字_ R2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import os
import sys
from pathlib import Path

import xlwings as xw

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

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():
# =========================================================================
# (1) 取得專案根目錄
# =========================================================================
current_file_path = Path(__file__).resolve()
project_root = current_file_path.parent
print(f"專案根目錄為: {project_root}")

# =========================================================================
# (2) 若無指定輸入檔案,則獲取當前作用中的 Excel 檔案並另存新檔
# =========================================================================
try:
wb = xw.apps.active.books.active
except Exception as e:
print(f"發生錯誤: {e}")
print("無法找到作用中的 Excel 工作簿")
return EXIT_CODE_NO_FILE

if not wb:
print("無法作業,原因可能為:(1) 未指定輸入檔案;(2) 未找到作用中的 Excel 工作簿!")
return EXIT_CODE_NO_FILE

try:
cell_value = wb.sheets['漢字注音'].range('V3').value

# 判斷是否為 None 並執行處理
if cell_value is None:
print("【待注音漢字】儲存格未填入文字,作業無法繼續。")
return EXIT_CODE_INVALID_INPUT
else:
value_length = len(cell_value.strip())
print(f"【待注音漢字】總字數為: {value_length}")

# 將儲存格已填入之漢字及標音清除
clear_han_ji_kap_piau_im(wb)

# 將待注音的【漢字儲存格】重設格式
reset_han_ji_cells(wb)

# 將待注音的漢字填入
fill_hanji_in_cells(wb)

# A731: 自動為漢字查找讀音
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')
else:
print("無法執行漢字標音作業,請確認【env】工作表【語音類型】及【漢字庫】欄位的設定是否正確!")
return EXIT_CODE_PROCESS_FAILURE

# 將檔案存放路徑設為【專案根目錄】之下
save_as_new_file(wb=wb)

except Exception as e:
print(f"執行過程中發生未知錯誤: {e}")
return EXIT_CODE_UNKNOWN_ERROR

return EXIT_CODE_SUCCESS

if __name__ == "__main__":
exit_code = main()
if exit_code == 0:
print("作業成功完成!")
else:
print(f"程式結束,代碼: {exit_code}")
sys.exit(exit_code)
Loading

0 comments on commit f797b96

Please sign in to comment.