Skip to content

Commit

Permalink
新增功能: a702 模組,可以 “批次作業” 方式,將清單中所有的待標音 Excel 檔案,全部依 “雅俗通十五音漢字典” 自動完成漢字…
Browse files Browse the repository at this point in the history
…的標註台語音標及方音符號。
  • Loading branch information
AlanJui committed Oct 2, 2024
1 parent e6d5059 commit be448c8
Show file tree
Hide file tree
Showing 44 changed files with 2,256 additions and 2,382 deletions.
14 changes: 14 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
"program": "mod_雅俗通.py",
"console": "integratedTerminal",
},
{
"name": "a702_Batch_Zu_Tong_Piau_Zu_Im",
"type": "debugpy",
"request": "launch",
"program": "a702_Batch_Zu_Tong_Piau_Zu_Im.py",
"console": "integratedTerminal",
},
{
"name": "a710_清理原填入漢字及注音",
"type": "debugpy",
Expand Down Expand Up @@ -39,6 +46,13 @@
"program": "a730_將漢字注音填入.py",
"console": "integratedTerminal",
},
{
"name": "a731_自動為漢字查注音",
"type": "debugpy",
"request": "launch",
"program": "a731_自動為漢字查注音.py",
"console": "integratedTerminal",
},
{
"name": "a740_漢字注音轉網頁",
"type": "debugpy",
Expand Down
Binary file modified Tai_Gi_Zu_Im_Bun.xlsx
Binary file not shown.
Binary file modified Tai_Gi_Zu_Im_Bun_BAK.xlsx
Binary file not shown.
27 changes: 27 additions & 0 deletions a701_Zu_Tong_Piau_Zu_Im.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# =========================================================================
# 當 Tai_Gi_Zu_Im_Bun.xlsx 檔案已完成人工手動注音後,執行此程式可完成以下工作:
# (1) A730: 將人工填入之拼音及注音,抄寫到漢字的上方(拼音)及下方(注音)。
# (2) A740: 將【漢字注音】工作表的內容,轉成 HTML 網頁檔案。
# (3) A750: 將 Tai_Gi_Zu_Im_Bun.xlsx 檔案,依 env 工作表的設定,另存新檔到指定目錄。
# =========================================================================
import os
import subprocess
import sys

# 指定虛擬環境的 Python 路徑
venv_python = os.path.join(".venv", "Scripts", "python.exe") if sys.platform == "win32" else os.path.join(".venv", "bin", "python")

# 依次執行三個 Python 檔案

# (1) A720: 將 V3 儲存格內的漢字,填入標音用方格。
subprocess.run([venv_python, "a720_待注音漢字填入標音用方格.py", "-i", "Tai_Gi_Zu_Im_Bun.xlsx"])

# (2) A731: 自動為漢字查找讀音,並抄寫到漢字的上方(拼音)及下方(注音)。
subprocess.run([venv_python, "a731_自動為漢字查注音.py", "-i", "Tai_Gi_Zu_Im_Bun.xlsx"])

# (3) A740: 將【漢字注音】工作表的內容,轉成 HTML 網頁檔案。
subprocess.run([venv_python, "a740_漢字注音轉網頁.py", "-i", "Tai_Gi_Zu_Im_Bun.xlsx"])

# (4) A750: 將 Tai_Gi_Zu_Im_Bun.xlsx 檔案,依 env 工作表的設定,另存新檔到指定目錄。
# subprocess.run([venv_python, "a750_漢字注音存檔.py"])
subprocess.run([venv_python, "a750_漢字注音存檔.py", "-i", "Tai_Gi_Zu_Im_Bun.xlsx"])
74 changes: 74 additions & 0 deletions a702_Batch_Zu_Tong_Piau_Zu_Im.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import os
import subprocess
import sys

import xlwings as xw

from p702_Ca_Han_Ji_Thak_Im import ca_han_ji_thak_im
from p710_thiam_han_ji import fill_hanji_in_cells
from p730_Tng_Sing_Bang_Iah import tng_sing_bang_iah

# 指定虛擬環境的 Python 路徑
venv_python = os.path.join(".venv", "Scripts", "python.exe") if sys.platform == "win32" else os.path.join(".venv", "bin", "python")

# 定義檔案目錄
directory = r"C:\work\Piau-Im\output2"

# 所有檔案名稱
files = [
# "【河洛話注音】金剛般若波羅蜜經001。法會因由分第一.xlsx",
# "【河洛話注音】金剛般若波羅蜜經002。善現啟請分第二.xlsx",
# "【河洛話注音】金剛般若波羅蜜經003。大乘正宗分第三.xlsx",
# "【河洛話注音】金剛般若波羅蜜經004。妙行無住分第四.xlsx",
# "【河洛話注音】金剛般若波羅蜜經005。如理實見分第五.xlsx",
# "【河洛話注音】金剛般若波羅蜜經006。正信希有分第六.xlsx",
# "【河洛話注音】金剛般若波羅蜜經007。無得無說分第七.xlsx",
# "【河洛話注音】金剛般若波羅蜜經009。一相無相分第九.xlsx",
# "【河洛話注音】金剛般若波羅蜜經010。莊嚴淨土分第十.xlsx",
# "【河洛話注音】金剛般若波羅蜜經011。無為福勝分第十一.xlsx",
# "【河洛話注音】金剛般若波羅蜜經012。尊重正教分第十二.xlsx",
# "【河洛話注音】金剛般若波羅蜜經013。如法受持分第十三.xlsx"
]

# 迴圈遍歷所有檔案並依次執行 Python 檔案
for file_name in files:
file_path = os.path.join(directory, file_name)

# 打開 Excel 檔案
wb = xw.Book(file_path)

# 顯示「已輸入之拼音字母及注音符號」
named_range = wb.names['顯示注音輸入'] # 選擇名為 "顯示注音輸入" 的命名範圍# 選擇名為 "顯示注音輸入" 的命名範圍
named_range.refers_to_range.value = True

# 清空儲存格內容
sheet = wb.sheets['漢字注音'] # 選擇指定的工作表# 選擇工作表
sheet.range('D3:R166').clear_contents() # 清除 C3:R166 範圍的內容

# (1) A720: 將 V3 儲存格內的漢字,逐個填入標音用方格。
fill_hanji_in_cells(wb)

# (2) A731: 自動為漢字查找讀音,並抄寫到漢字的上方(拼音)及下方(注音)。
ca_han_ji_thak_im(wb, '漢字注音', 'V3')

# (3) A740: 將【漢字注音】工作表的內容,轉成 HTML 網頁檔案。
tng_sing_bang_iah(wb, '漢字注音', 'V3')

# (4) A750: 將 Tai_Gi_Zu_Im_Bun.xlsx 檔案,依 env 工作表的設定,另存新檔到指定目錄。
setting_sheet = wb.sheets["env"]
new_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"【河洛話注音】{new_file_name}" + ".xlsx")

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

# 保存 Excel 檔案
wb.close()

23 changes: 6 additions & 17 deletions a720_待注音漢字填入標音用方格.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,11 @@ def get_input_and_output_options(argv):
# - 上方:台語音標
# - 下方:台語注音符號
# =========================================================================
fill_hanji_in_cells(wb) # 將漢字逐個填入各儲存格
# 選擇工作表
sheet = wb.sheets['漢字注音']

# =========================================================================
# (3) 依據《文章標題》另存新檔。
# =========================================================================
# wb = xw.Book(CONVERT_FILE_NAME)
# setting_sheet = wb.sheets["env"]
# new_file_name = str(
# setting_sheet.range("C4").value
# ).strip()
# new_file_path = os.path.join(
# ".\\output",
# f"【河洛話注音】{new_file_name}" + ".xlsx")

# # 儲存新建立的工作簿
# wb.save(new_file_path)
# 清除 C3:R166 範圍的內容
sheet.range('D3:R166').clear_contents()

# # 保存 Excel 檔案
# wb.close()
# 將漢字逐個填入各儲存格
fill_hanji_in_cells(wb)
68 changes: 68 additions & 0 deletions a731_自動為漢字查注音.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import getopt
import sys

import xlwings as xw

import settings
from p702_Ca_Han_Ji_Thak_Im import ca_han_ji_thak_im


def get_input_and_output_options(argv):
arg_input = ""
arg_output = ""
arg_help = "{0} -i <input> -o <output>".format(argv[0])

try:
opts, args = getopt.getopt( # pyright: ignore
argv[1:], "hi:o:", ["help", "input=", "output="]
)
except Exception as e:
print(e)
print(arg_help)
sys.exit(2)

for opt, arg in opts:
if opt in ("-h", "--help"):
print(arg_help) # print the help message
sys.exit(2)
elif opt in ("-i", "--input"):
arg_input = arg
elif opt in ("-o", "--output"):
arg_output = arg

print("input:", arg_input)
print("output:", arg_output)

return {
"input": arg_input,
"output": arg_output,
}


if __name__ == "__main__":
# =========================================================================
# (1) 取得需要注音的「檔案名稱」及其「目錄路徑」。
# =========================================================================
# 取得 Input 檔案名稱
file_path = settings.get_tai_gi_zu_im_bun_path()
if not file_path:
print("未設定 .env 檔案")
# sys.exit(2)
opts = get_input_and_output_options(sys.argv)
if opts["input"] != "":
CONVERT_FILE_NAME = opts["input"]
else:
CONVERT_FILE_NAME = "Tai_Gi_Zu_Im_Bun.xlsx"
else:
CONVERT_FILE_NAME = file_path
print(f"CONVERT_FILE_NAME = {CONVERT_FILE_NAME}")

# 打開 Excel 檔案
wb = xw.Book(CONVERT_FILE_NAME)

# =========================================================================
# (2) 分析已輸入的【台語音標】及【台語注音符號】,將之各別填入漢字之上、下方。
# - 上方:台語音標
# - 下方:台語注音符號
# =========================================================================
ca_han_ji_thak_im(wb, '漢字注音', 'V3')
Loading

0 comments on commit be448c8

Please sign in to comment.