Skip to content

Commit

Permalink
(1)新增功能 a701 可清除已輸入之漢字及注音;(2)變更 p710 填入漢字之迴圈結構。
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanJui committed Sep 6, 2024
1 parent cada8e2 commit 5092fa7
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 29 deletions.
Binary file modified Tai_Gi_Zu_Im_Bun.xlsx
Binary file not shown.
5 changes: 2 additions & 3 deletions a700_Iong_Excel_Piau_Zu_Im.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def get_input_and_output_options(argv):
"output": arg_output,
}


if __name__ == "__main__":
# =========================================================================
# (1) 取得需要注音的「檔案名稱」及其「目錄路徑」。
Expand Down Expand Up @@ -76,12 +75,12 @@ def get_input_and_output_options(argv):
thiam_zu_im(wb, '漢字注音', 'V3')

# =========================================================================
# (4) 將已注音之「漢字注音表」,製作成 HTML 格式之「注音/拼音/標音」網頁。
# (3) 將已注音之「漢字注音表」,製作成 HTML 格式之「注音/拼音/標音」網頁。
# =========================================================================
tng_sing_bang_iah(wb, '漢字注音', 'V3')

# =========================================================================
# (5) 依據《文章標題》另存新檔。
# (4) 依據《文章標題》另存新檔。
# =========================================================================
wb = xw.Book(CONVERT_FILE_NAME)
setting_sheet = wb.sheets["env"]
Expand Down
106 changes: 106 additions & 0 deletions a701_Clear_Cells.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# 填漢字等標音:將整段的文字拆解,個別填入儲存格,以便後續人工手動填入台語音標、注音符號。
import getopt
import math
import sys

import xlwings as xw

import settings


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

try:
opts, args = getopt.getopt( # pyright: ignore
argv[1:], "hi:u:o:", ["help", "input=", "user=", "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 ("-u", "--user"):
arg_user = arg
elif opt in ("-o", "--output"):
arg_output = arg

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

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


def clear_hanji_in_cells(wb, sheet_name='漢字注音', cell='V3'):
# 選擇指定的工作表
sheet = wb.sheets[sheet_name]

# 取得 V3 儲存格的字串
v3_value = sheet.range(cell).value

# 確認 V3 不為空
if v3_value:
# 計算字串的總長度
total_length = len(v3_value)

# 每列最多處理 15 個字元,計算總共需要多少列
chars_per_row = 15
total_rows_needed = math.ceil(total_length / chars_per_row) # 無條件進位

# 迴圈清空所有漢字的上下方儲存格 (羅馬拼音和台語注音符號)
row = 5
for i in range(total_rows_needed+1):
for col in range(4, 19): # 【D欄=4】到【R欄=18】
# 清空漢字儲存格 (Row)
sheet.range((row, col)).value = None
# 清空上方的台語拼音儲存格 (Row-1)
sheet.range((row - 1, col)).value = None
# 清空下方的台語注音儲存格 (Row+1)
sheet.range((row + 1, col)).value = None
# 清空填入注音的儲存格 (Row-2)
sheet.range((row - 2, col)).value = None

# 每處理 15 個字元後,換到下一行
row += 4



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) 清除原先已填入的漢字
# =========================================================================
clear_hanji_in_cells(wb, '漢字注音', 'V3')
2 changes: 1 addition & 1 deletion a710_Thiam_Han_Ji_Tan_Piau_Im.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 填漢字等標音:將整段的文字拆解,個別填入儲存格,以便後續人工手動填入台語音標、注音符號。
import getopt
import os
import math
import sys

import xlwings as xw
Expand Down
31 changes: 6 additions & 25 deletions p710_thiam_han_ji.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import math


def fill_hanji_in_cells(wb, sheet_name='漢字注音', cell='V3'):
# 選擇指定的工作表
sheet = wb.sheets[sheet_name]
Expand All @@ -15,28 +12,13 @@ def fill_hanji_in_cells(wb, sheet_name='漢字注音', cell='V3'):

# 每列最多處理 15 個字元,計算總共需要多少列
chars_per_row = 15
total_rows_needed = math.ceil(total_length / chars_per_row) # 無條件進位

# 迴圈清空所有漢字的上下方儲存格 (羅馬拼音和台語注音符號)
row = 5
for i in range(total_rows_needed+1):
for col in range(4, 19): # 【D欄=4】到【R欄=18】
# 清空漢字儲存格 (Row)
sheet.range((row, col)).value = None
# 清空上方的台語拼音儲存格 (Row-1)
sheet.range((row - 1, col)).value = None
# 清空下方的台語注音儲存格 (Row+1)
sheet.range((row + 1, col)).value = None
# 清空填入注音的儲存格 (Row-2)
sheet.range((row - 2, col)).value = None

# 每處理 15 個字元後,換到下一行
row += 4

# 逐字處理字串,並填入對應的儲存格
row = 5
index = 0 # 用來追蹤目前處理到的字元位置
for i in range(total_rows_needed+1):

# 逐字處理字串
while index < total_length: # 使用 while 而非 for,確保處理完整個字串
for col in range(4, 19): # 【D欄=4】到【R欄=18】
# 確認是否還有字元可以處理
if index < total_length:
Expand All @@ -47,9 +29,9 @@ def fill_hanji_in_cells(wb, sheet_name='漢字注音', cell='V3'):
# 將字元填入對應的儲存格
sheet.range((row, col)).value = char
else:
# 若遇到換行字元,退出迴圈
# 若遇到換行字元,直接跳過
index += 1
break;
break

# 更新索引,處理下一個字元
index += 1
Expand All @@ -61,10 +43,9 @@ def fill_hanji_in_cells(wb, sheet_name='漢字注音', cell='V3'):

# 保存 Excel 檔案
wb.save()
# wb.close()

# 選擇名為 "顯示注音輸入" 的命名範圍
named_range = wb.names['顯示注音輸入']
named_range.refers_to_range.value = True

print(f"已成功更新,漢字已填入對應儲存格,上下方儲存格已清空。")
print(f"已成功更新,漢字已填入對應儲存格,上下方儲存格已清空。")

0 comments on commit 5092fa7

Please sign in to comment.