Skip to content

Commit

Permalink
變更【製作網頁】作法
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanJui committed Nov 8, 2024
1 parent 003344c commit 15027a3
Show file tree
Hide file tree
Showing 16 changed files with 2,186 additions and 151 deletions.
Binary file modified Ho_Lok_Ue.db
Binary file not shown.
155 changes: 80 additions & 75 deletions a520_製作注音網頁.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#================================================================
# 《予我廣韻標音》
# 使用《廣韻》作為漢字標讀音之依據。
#================================================================
import getopt
import os
import sqlite3
import sys
Expand All @@ -11,90 +6,100 @@

import settings
from mod_file_access import (
San_Sing_Han_Ji_Zu_Im_Piau,
close_excel_file,
get_cmd_input,
open_excel_file,
save_to_a_working_copy,
write_to_excel_file,
)
from mod_廣韻 import init_sing_bu_dict, init_un_bu_dict
from p500_Import_Source_Sheet import San_Sing_Han_Ji_Tsh_Im_Piau
from p501_Kong_Un_Cha_Ji_Tian import Kong_Un_Piau_Im
from p502_TLPA_Cu_Im import Iong_TLPA_Cu_Im


def main():
# =========================================================="
# 資料庫",
# =========================================================="
# 自 .env 檔案取得資料庫名稱
DATABASE = settings.get_database_path()
conn = sqlite3.connect(DATABASE)
db_cursor = conn.cursor()
print(f"DATABASE = {DATABASE}")

# =========================================================================
# (1) 取得需要注音的「檔案名稱」及其「目錄路徑」。
# =========================================================================
# 自命令列取得 Input 檔案名稱
opts = get_cmd_input()
CONVERT_FILE_NAME = opts["input"]
print(f"CONVERT_FILE_NAME = {CONVERT_FILE_NAME}")

# 取得檔案所屬之目錄路徑
dir_path = opts["dir_path"]

# 指定提供來源的【檔案】
wb = open_excel_file(dir_path, CONVERT_FILE_NAME)
if wb is None:
print("無法開啟檔案,終止程式執行。")
sys.exit()

# =========================================================================
# (2) 建置「漢字注音表」
# 將存放在「工作表1」的「漢字」文章,製成「漢字注音表」以便填入注音。
# =========================================================================
# San_Sing_Han_Ji_Tsh_Im_Piau(CONVERT_FILE_NAME)

# =========================================================================
# (3) 在字典查注音,填入漢字注音表。
# =========================================================================
# Kong_Un_Piau_Im(CONVERT_FILE_NAME, db_cursor)

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

# 設定聲母及韻母之注音對照表
def initialize_dicts(db_cursor):
"""初始化聲母和韻母對照表字典"""
try:
sing_bu_dict = init_sing_bu_dict(db_cursor)
un_bu_dict = init_un_bu_dict(db_cursor)
print("字典初始化完成。")
except Exception as e:
print(f"字典初始化失敗:{e}")
sys.exit(1)
return sing_bu_dict, un_bu_dict


def create_annotation_file(wb, db_cursor):
"""建立注音表並進行查詢填寫"""
# 建立漢字注音表
San_Sing_Han_Ji_Zu_Im_Piau(wb.name)
# # 查詢注音並填寫表格
# Kong_Un_Piau_Im(wb.name, db_cursor)


def export_to_html(wb):
"""將已注音的漢字注音表導出為 HTML 格式"""
# 這裡可以使用已經填寫的漢字注音表進行轉換
print("將注音表轉換為 HTML 格式的功能可以在這裡實現。")


def main():
#-------------------------------------------------------------------------
# 使用已打開且處於作用中的 Excel 工作簿
#-------------------------------------------------------------------------
# 取得專案根目錄。
try:
wb = xw.apps.active.books.active
except Exception as e:
print(e)
Iong_TLPA_Cu_Im(wb, sing_bu_dict, un_bu_dict)

# ==========================================================
# 檢查「缺字表」狀態
# ==========================================================
# 指定來源工作表
source_sheet = wb.sheets["缺字表"]
# 取得工作表內總列數
end_row_no = (
source_sheet.range("A" + str(source_sheet.cells.last_cell.row)).end("up").row
)
if end_row_no > 1:
print(f"總計字典查不到注音的漢字共:{end_row_no}個。")

# =========================================================================
# (5) 依據《文章標題》另存新檔。
# =========================================================================
write_to_excel_file(wb)
close_excel_file(wb)

# ==========================================================
# 關閉資料庫
# ==========================================================
conn.close()
print(f"發生錯誤: {e}")
print("無法找到作用中的 Excel 工作簿")
sys.exit(2)

# 獲取活頁簿的完整檔案路徑
file_path = wb.fullname
print(f"完整檔案路徑: {file_path}")

# 獲取活頁簿的檔案名稱(不包括路徑)
file_name = wb.name
print(f"檔案名稱: {file_name}")

# 資料庫連接
DATABASE = settings.get_database_path()
with sqlite3.connect(DATABASE) as conn:
db_cursor = conn.cursor()
print(f"DATABASE = {DATABASE}")

# # 取得命令列參數和檔案路徑
# opts = get_cmd_input()
# CONVERT_FILE_NAME = opts["input"]
# dir_path = opts["dir_path"]
# print(f"處理檔案: {CONVERT_FILE_NAME}")

# # 開啟指定的 Excel 檔案
# wb = open_excel_file(dir_path, CONVERT_FILE_NAME)
# if wb is None:
# print("無法開啟檔案,終止程式執行。")
# sys.exit()

# 初始化字典
sing_bu_dict, un_bu_dict = initialize_dicts(db_cursor)

# 創建漢字注音表並查詢注音
create_annotation_file(wb, db_cursor)

# 注音轉換處理
Iong_TLPA_Zu_Im(wb, sing_bu_dict, un_bu_dict)

# 檢查缺字表狀態
source_sheet = wb.sheets["缺字表"]
end_row_no = source_sheet.range("A" + str(source_sheet.cells.last_cell.row)).end("up").row
if end_row_no > 1:
print(f"總計字典查不到注音的漢字共:{end_row_no}個。")

# 儲存 Excel 檔案並關閉
write_to_excel_file(wb)
close_excel_file(wb)

if __name__ == "__main__":
main()
main()
100 changes: 100 additions & 0 deletions a520_製作注音網頁_bak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#================================================================
# 《予我廣韻標音》
# 使用《廣韻》作為漢字標讀音之依據。
#================================================================
import getopt
import os
import sqlite3
import sys

import xlwings as xw

import settings
from mod_file_access import (
close_excel_file,
get_cmd_input,
open_excel_file,
save_to_a_working_copy,
write_to_excel_file,
)
from mod_廣韻_v3 import init_sing_bu_dict, init_un_bu_dict
from p500_Import_Source_Sheet import San_Sing_Han_Ji_Tsh_Im_Piau
from p501_Kong_Un_Cha_Ji_Tian import Kong_Un_Piau_Im
from p502_TLPA_Cu_Im import Iong_TLPA_Cu_Im


def main():
# =========================================================="
# 資料庫",
# =========================================================="
# 自 .env 檔案取得資料庫名稱
DATABASE = settings.get_database_path()
conn = sqlite3.connect(DATABASE)
db_cursor = conn.cursor()
print(f"DATABASE = {DATABASE}")

# =========================================================================
# (1) 取得需要注音的「檔案名稱」及其「目錄路徑」。
# =========================================================================
# 自命令列取得 Input 檔案名稱
opts = get_cmd_input()
CONVERT_FILE_NAME = opts["input"]
print(f"CONVERT_FILE_NAME = {CONVERT_FILE_NAME}")

# 取得檔案所屬之目錄路徑
dir_path = opts["dir_path"]

# 指定提供來源的【檔案】
wb = open_excel_file(dir_path, CONVERT_FILE_NAME)
if wb is None:
print("無法開啟檔案,終止程式執行。")
sys.exit()

# =========================================================================
# (2) 建置「漢字注音表」
# 將存放在「工作表1」的「漢字」文章,製成「漢字注音表」以便填入注音。
# =========================================================================
# San_Sing_Han_Ji_Tsh_Im_Piau(CONVERT_FILE_NAME)

# =========================================================================
# (3) 在字典查注音,填入漢字注音表。
# =========================================================================
# Kong_Un_Piau_Im(CONVERT_FILE_NAME, db_cursor)

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

# 設定聲母及韻母之注音對照表
try:
sing_bu_dict = init_sing_bu_dict(db_cursor)
un_bu_dict = init_un_bu_dict(db_cursor)
except Exception as e:
print(e)
Iong_TLPA_Cu_Im(wb, sing_bu_dict, un_bu_dict)

# ==========================================================
# 檢查「缺字表」狀態
# ==========================================================
# 指定來源工作表
source_sheet = wb.sheets["缺字表"]
# 取得工作表內總列數
end_row_no = (
source_sheet.range("A" + str(source_sheet.cells.last_cell.row)).end("up").row
)
if end_row_no > 1:
print(f"總計字典查不到注音的漢字共:{end_row_no}個。")

# =========================================================================
# (5) 依據《文章標題》另存新檔。
# =========================================================================
write_to_excel_file(wb)
close_excel_file(wb)

# ==========================================================
# 關閉資料庫
# ==========================================================
conn.close()

if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions a703_製作標音網頁.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
sheet.range('A1').select() # 將 A1 儲存格設為作用儲存格

# (2) A740: 將【漢字注音】工作表的內容,轉成 HTML 網頁檔案。
# tng_sing_bang_iah(wb, '漢字注音', 'V3')
tng_sing_bang_iah(wb, '十五音', 'V3')
han_ji_piau_im_huat = get_named_value(wb, '標音方法')
tng_sing_bang_iah(wb=wb, sheet_name='漢字注音', cell='V3', page_type='含頁頭')

# (3) A750: 將 Tai_Gi_Zu_Im_Bun.xlsx 檔案,依 env 工作表的設定,另存新檔到指定目錄。
try:
Expand Down
52 changes: 34 additions & 18 deletions a704_製作其它漢字標音.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import xlwings as xw

from mod_file_access import copy_excel_sheet, reset_han_ji_piau_im_cells
from p704_漢字以十五音標注音 import zap_goo_im_piau_im

# from p703_Kong_Un_Ca_Thak_Im import ca_han_ji_thak_im
from p704_漢字以十五音標注音 import han_ji_piau_im

# (0) 取得專案根目錄。
# 使用已打開且處於作用中的 Excel 工作簿
Expand All @@ -26,20 +24,46 @@
print(f"檔案名稱: {file_name}")

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

# (1) A720: 將 V3 儲存格內的漢字,逐個填入標音用方格。
sheet = wb.sheets['漢字注音'] # 選擇工作表
sheet.activate() # 將「漢字注音」工作表設為作用中工作表
sheet.range('A1').select() # 將 A1 儲存格設為作用儲存格
sheet = wb.sheets['漢字注音']
sheet.activate()
sheet.range('A1').select()

# (2) 複製【漢字注音】工作表,並將【漢字注音】工作表已有漢字標清除(不含上列之【台語音標】)
piau_im_huat = wb.names['標音方法'].refers_to_range.value

copy_excel_sheet(wb, '漢字注音', piau_im_huat)
reset_han_ji_piau_im_cells(wb, piau_im_huat)
zap_goo_im_piau_im(wb, sheet_name=piau_im_huat, cell='V3', hue_im="白話音")

# 根據標音方法選擇對應參數
if piau_im_huat == '十五音':
hue_im = "白話音"
module_name = 'mod_標音'
function_name = 'TLPA_Tng_Zap_Goo_Im'
elif piau_im_huat == '白話字':
# 以下為待完成之虛擬程式碼
hue_im = "白話字"
module_name = 'mod_白話字'
function_name = 'bah_hoat_tng_im'
elif piau_im_huat == '台羅拼音':
# 以下為待完成之虛擬程式碼
hue_im = "台羅拼音"
module_name = 'mod_台羅拼音'
function_name = 'tai_lo_pin_im'
elif piau_im_huat == '閩拚方案':
# 以下為待完成之虛擬程式碼
hue_im = "閩拚方案"
module_name = 'mod_閩拚方案'
function_name = 'min_pian_han_im'
else:
print(f"無法識別的標音方法:{piau_im_huat}")
sys.exit(1)

# 呼叫 han_ji_piau_im 函數,並傳入動態參數
han_ji_piau_im(wb, sheet_name=piau_im_huat, cell='V3', hue_im=hue_im, module_name=module_name, function_name=function_name)

# (3) A740: 將【漢字注音】工作表的內容,轉成 HTML 網頁檔案。
# tng_sing_bang_iah(wb, '漢字注音', 'V3')
Expand All @@ -48,22 +72,14 @@
try:
file_name = str(wb.names['TITLE'].refers_to_range.value).strip()
except KeyError:
# print("未找到命名範圍 'TITLE',使用預設名稱")
# file_name = "default_file_name.xlsx" # 提供一個預設檔案名稱
setting_sheet = wb.sheets["env"]
file_name = str(
setting_sheet.range("C4").value
).strip()
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")
f"【{piau_im_huat}{file_name}.xlsx")

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

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

Loading

0 comments on commit 15027a3

Please sign in to comment.