-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha703_製作標音網頁.py
512 lines (445 loc) · 20.2 KB
/
a703_製作標音網頁.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# =========================================================================
# 載入程式所需套件/模組/函式庫
# =========================================================================
import logging
import os
import sys
from pathlib import Path
# 載入第三方套件
import xlwings as xw
from dotenv import load_dotenv
# 載入自訂模組
from mod_file_access import save_as_new_file
from mod_標音 import split_tai_gi_im_piau # 分解台語音標
from mod_標音 import PiauIm, is_punctuation
# from p730_Tng_Sing_Bang_Iah import tng_sing_bang_iah
# =========================================================================
# 載入環境變數
# =========================================================================
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'
)
def logging_process_step(msg):
print(msg)
logging.info(msg)
# =========================================================================
# 常數定義
# =========================================================================
# 定義 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 create_html_file(output_path, content, title='您的標題'):
template = f"""
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<title>{title}</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="assets/styles/styles.css">
</head>
<body>
{content}
</body>
</html>
"""
# Write to HTML file
with open(output_path, 'w', encoding='utf-8') as file:
file.write(template)
# 顯示輸出之網頁檔案及其存放目錄路徑
print(f"\n輸出網頁檔案:{output_path}")
def put_picture(wb, source_sheet_name):
html_str = ""
title = wb.sheets["env"].range("TITLE").value
# web_page_title = f"《{title}》【{source_sheet_name}】\n"
web_page_title = f"《{title}》\n"
image_url = wb.sheets["env"].range("IMAGE_URL").value
# ruff: noqa: E501
div_tag = (
"<div class='separator' style='clear: both'>\n"
" <a href='圖片' style='display: block; padding: 1em 0; text-align: center'>\n"
" <img alt='%s' border='0' width='400' data-original-height='630' data-original-width='1200'\n"
" src='%s' />\n"
" </a>\n"
"</div>\n"
)
# 寫入文章附圖
# html_str = f"《{title}》【{source_sheet_name}】\n"
html_str = f"{title}\n"
# html_str += div_tag % (title, image_url)
html_str += (div_tag % (title, image_url) + "\n")
return html_str
def tng_uann_piau_im(piau_im, zu_im_huat, siann_bu, un_bu, tiau_ho):
"""根據指定的標音方法,轉換台語音標之羅馬拚音字母"""
if zu_im_huat == "十五音":
return piau_im.SNI_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "雅俗通":
return piau_im.NST_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "白話字":
return piau_im.POJ_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "台羅拼音":
return piau_im.TL_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "閩拼方案":
return piau_im.BP_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "方音符號":
return piau_im.TPS_piau_im(siann_bu, un_bu, tiau_ho)
elif zu_im_huat == "台語音標":
siann = piau_im.Siann_Bu_Dict[siann_bu]["台語音標"] or ""
un = piau_im.Un_Bu_Dict[un_bu]["台語音標"]
return f"{siann}{un}{tiau_ho}"
return ""
def concat_ruby_tag(wb, piau_im, han_ji, tai_gi_im_piau):
"""將漢字、台語音標及台語注音符號,合併成一個 Ruby Tag"""
zu_im_list = split_tai_gi_im_piau(tai_gi_im_piau)
if zu_im_list[0] == "" or zu_im_list[0] == None:
siann_bu = "ø"
else:
siann_bu = zu_im_list[0]
style = wb.names['網頁格式'].refers_to_range.value
piau_im_hong_sik = wb.names['標音方式'].refers_to_range.value
siong_pinn_piau_im = wb.names['上邊標音'].refers_to_range.value
zian_pinn_piau_im = wb.names['右邊標音'].refers_to_range.value
ruby_tag = ""
siong_piau_im = ""
zian_piau_im = ""
# 根據【網頁格式】,決定【漢字】之上方或右方,是否該顯示【標音】
if style == "無預設":
# 若【網頁格式】設定為【無預設】,則根據【標音方式】決定漢字之上方及右方,是否需要放置標音
if piau_im_hong_sik == "上及右":
# 漢字上方顯示【上邊標音】,下方顯示【下邊標音】
siong_piau_im = tng_uann_piau_im(
piau_im=piau_im, # 注音法物件
zu_im_huat=siong_pinn_piau_im,
siann_bu=siann_bu,
un_bu=zu_im_list[1],
tiau_ho=zu_im_list[2]
)
zian_piau_im = tng_uann_piau_im(
piau_im=piau_im, # 注音法物件
zu_im_huat=zian_pinn_piau_im,
siann_bu=siann_bu,
un_bu=zu_im_list[1],
tiau_ho=zu_im_list[2]
)
elif piau_im_hong_sik == "上":
siong_piau_im = tng_uann_piau_im(
piau_im=piau_im, # 注音法物件
zu_im_huat=siong_pinn_piau_im,
siann_bu=siann_bu,
un_bu=zu_im_list[1],
tiau_ho=zu_im_list[2]
)
elif piau_im_hong_sik == "右":
zian_piau_im = tng_uann_piau_im(
piau_im=piau_im, # 注音法物件
zu_im_huat=zian_pinn_piau_im,
siann_bu=siann_bu,
un_bu=zu_im_list[1],
tiau_ho=zu_im_list[2]
)
else:
if style == "POJ" or style == "TL" or style == "BP" or style == "TLPA_Plus":
# 羅馬拼音字母標音法,將標音置於漢字上方
siong_piau_im = tng_uann_piau_im(
piau_im=piau_im, # 注音法物件
zu_im_huat=siong_pinn_piau_im,
siann_bu=siann_bu,
un_bu=zu_im_list[1],
tiau_ho=zu_im_list[2]
)
elif style == "SNI":
# 十五音反切法,將標音置於漢字上方
siong_piau_im = tng_uann_piau_im(
piau_im=piau_im, # 注音法物件
zu_im_huat=siong_pinn_piau_im,
siann_bu=siann_bu,
un_bu=zu_im_list[1],
tiau_ho=zu_im_list[2]
)
elif style == "TPS":
# 注音符號標音法,將標音置於漢字右方
zian_piau_im = tng_uann_piau_im(
piau_im=piau_im, # 注音法物件
zu_im_huat=zian_pinn_piau_im,
siann_bu=siann_bu,
un_bu=zu_im_list[1],
tiau_ho=zu_im_list[2]
)
elif style == "DBL":
# 漢字上方顯示台語音標,下方顯示台語注音符號
siong_piau_im = tng_uann_piau_im(
piau_im=piau_im, # 注音法物件
zu_im_huat=siong_pinn_piau_im,
siann_bu=siann_bu,
un_bu=zu_im_list[1],
tiau_ho=zu_im_list[2]
)
zian_piau_im = tng_uann_piau_im(
piau_im=piau_im, # 注音法物件
zu_im_huat=zian_pinn_piau_im,
siann_bu=siann_bu,
un_bu=zu_im_list[1],
tiau_ho=zu_im_list[2]
)
# 根據標音方式,設定 Ruby Tag
if siong_piau_im != "" and zian_piau_im == "":
# 將標音置於漢字上方
ruby_tag = f" <ruby><rb>{han_ji}</rb><rp>(</rp><rt>{siong_piau_im}</rt><rp>)</rp></ruby>"
elif siong_piau_im == "" and zian_piau_im != "":
# 將標音置於漢字右方
ruby_tag = f" <ruby><rb>{han_ji}</rb><rp>(</rp><rtc>{zian_piau_im}</rtc><rp>)</rp></ruby>"
elif siong_piau_im != "" and zian_piau_im != "":
# 將標音置於漢字上方及右方
ruby_tag = f" <ruby><rb>{han_ji}</rb><rt>{siong_piau_im}</rt><rp>(</rp><rtc>{zian_piau_im}</rtc><rp>)</rp></ruby>"
return ruby_tag
# =========================================================
# 依據指定的【注音方法】,輸出含 Ruby Tags 之 HTML 網頁
# =========================================================
def build_web_page(wb, sheet, source_chars, total_length, page_type='含頁頭', piau_im_huat='方音符號', piau_im=None):
# ==========================================================
# 注音法設定和共用變數
# ==========================================================
zu_im_huat_list = {
"SNI": ["fifteen_yin", "rt", "十五音切語"],
"TPS": ["Piau_Im", "rt", "方音符號注音"],
"POJ": ["pin_yin", "rt", "白話字拼音"],
"TL": ["pin_yin", "rt", "台羅拼音"],
"BP": ["pin_yin", "rt", "閩拼標音"],
"TLPA_Plus": ["pin_yin", "rt", "台羅改良式"],
"DBL": ["Siang_Pai", "rtc", "雙排注音"],
"無預設": ["Siang_Pai", "rtc", "雙排注音"],
}
# 選擇工作表
sheet = wb.sheets['漢字注音']
sheet.activate()
write_buffer = ""
#--------------------------------------------------------------------------
# 輸出放置圖片的 HTML Tag
#--------------------------------------------------------------------------
# 寫入文章附圖
if page_type == '含頁頭':
write_buffer += put_picture(wb, sheet.name)
#--------------------------------------------------------------------------
# 輸出 <div> tag
#--------------------------------------------------------------------------
div_class = zu_im_huat_list[Web_Page_Style][0]
html_str = f"<div class='{div_class}'><p>"
write_buffer += (html_str + "\n")
#--------------------------------------------------------------------------
# 作業處理:逐列取出漢字,組合成純文字檔
#--------------------------------------------------------------------------
# 設定起始及結束的【列】位址(【第5列】、【第9列】、【第13列】等列)
TOTAL_LINES = int(wb.names['每頁總列數'].refers_to_range.value)
ROWS_PER_LINE = 4
start_row = 5
end_row = start_row + (TOTAL_LINES * ROWS_PER_LINE)
line = 1 # 處理行號指示器
# 設定起始及結束的【欄】位址(【D欄=4】到【R欄=18】)
CHARS_PER_ROW = int(wb.names['每列總字數'].refers_to_range.value)
start_col = 4
end_col = start_col + CHARS_PER_ROW
# 逐列處理作業
end_of_file = False
for row in range(start_row, end_row, ROWS_PER_LINE):
if end_of_file or line > TOTAL_LINES:
break
# 設定【作用儲存格】為列首
sheet.range((row, 1)).select()
# 逐欄取出儲存格內容
for col in range(start_col, end_col):
col_name = xw.utils.col_name(col) # 取得欄位名稱
ruby_tag = ""
cell_value = sheet.range((row, col)).value
if cell_value == 'φ': # 讀到【結尾標示】
end_of_file = True
break
elif cell_value == '\n': # 讀到【換行標示】
# 若遇到換行字元,退出迴圈
write_buffer += "</p><p>\n"
print("\n")
break
elif cell_value == None: # 讀到【空白】
msg = f"({row}, {col_name}) = 《空白》"
else: # 讀到:漢字或標點符號
# 當 han_ji 是標點符號時,不需要注音
if is_punctuation(cell_value):
ruby_tag = f"<span>{han_ji}</span>\n"
msg = f"({row}, {col_name}) = {cell_value}"
else:
han_ji = cell_value # 取得漢字
# 取得漢字的【台語音標】
tai_gi_im_piau = sheet.range((row - 1, col)).value # 取得漢字的台語音標
# 當儲存格寫入之資料為 None 情況時之處理作法:給予空字串
tai_gi_im_piau = tai_gi_im_piau if tai_gi_im_piau is not None else ""
# 將已注音之漢字加入【漢字注音表】
ruby_tag = concat_ruby_tag(
wb=wb,
piau_im=piau_im, # 注音法物件
han_ji=han_ji,
tai_gi_im_piau=tai_gi_im_piau
)
msg =f"({row}, {col_name}) = {han_ji} [{tai_gi_im_piau}]"
write_buffer += ruby_tag
print(msg)
# 已到【結尾處】之作業結束處理
if end_of_file:
print(f"第 {row} 列為檔案結尾處,結束處理作業。")
break
# 換行處理:(1)每處理完 15 字後,換下一行 ;(2) 讀到【換行標示】
line += 1
print(f"({row}, {col_name}) = 《換行》")
# =========================================================
# 輸出 </div>
# =========================================================
html_str = "</p></div>"
write_buffer += html_str
# 返回網頁輸出暫存區
return write_buffer
def tng_sing_bang_iah(wb, sheet_name='漢字注音', han_ji_source='V3', page_type='含頁頭'):
global source_sheet # 宣告 source_sheet 為全域變數
global source_sheet_name # 宣告 source_sheet_name 為全域變數
global total_length # 宣告 total_length 為全域變數
global Web_Page_Style
# -------------------------------------------------------------------------
# 連接指定資料庫
# -------------------------------------------------------------------------
han_ji_khoo = wb.names['漢字庫'].refers_to_range.value
Web_Page_Style = wb.names['網頁格式'].refers_to_range.value
piau_im = PiauIm(han_ji_khoo)
# -------------------------------------------------------------------------
# 選擇指定的工作表
# -------------------------------------------------------------------------
sheet = wb.sheets[sheet_name] # 選擇工作表
sheet.activate() # 將「漢字注音」工作表設為作用中工作表
sheet.range('A1').select() # 將 A1 儲存格設為作用儲存格
source_sheet_name = sheet.name
han_ji_piau_im_huat = wb.names['標音方法'].refers_to_range.value
# -----------------------------------------------------
# 產生 HTML 網頁用文字檔
# -----------------------------------------------------
title = wb.names['TITLE'].refers_to_range.value
web_page_title = f"{title}"
# 確保 output 子目錄存在
output_dir = 'docs'
output_file = f"{title}_{han_ji_piau_im_huat}.html"
output_path = os.path.join(output_dir, output_file)
# 開啟文字檔,準備寫入網頁內容
f = open(output_path, 'w', encoding='utf-8')
# 取得 V3 儲存格的字串
source_chars = sheet.range(han_ji_source).value
if source_chars:
# 計算字串的總長度
total_length = len(source_chars)
# ==========================================================
# 自「漢字注音表」,製作各種注音法之 HTML 網頁
# ==========================================================
print(f"開始製作【漢字注音】網頁!")
html_content = build_web_page(
wb=wb,
sheet=sheet,
source_chars=source_chars,
total_length=total_length,
page_type=page_type,
piau_im_huat=han_ji_piau_im_huat,
piau_im= piau_im
)
# 輸出到網頁檔案
create_html_file(output_path, html_content, web_page_title)
print(f"【漢字注音】網頁製作完畢!")
return 0
# =========================================================================
# 作業程序
# =========================================================================
def process(wb):
# ---------------------------------------------------------------------
# 將【漢字注音】工作表中的標音漢字,轉成 HTML 網頁檔案。
# ---------------------------------------------------------------------
result_code = tng_sing_bang_iah(
wb=wb,
sheet_name='漢字注音',
han_ji_source='V3',
page_type='含頁頭'
)
if result_code != EXIT_CODE_SUCCESS:
logging.error("標音漢字轉換為 HTML 網頁檔案失敗!")
return result_code
# ---------------------------------------------------------------------
# 作業結尾處理
# ---------------------------------------------------------------------
file_path = save_as_new_file(wb=wb)
if not file_path:
logging.error("儲存檔案失敗!")
return EXIT_CODE_PROCESS_FAILURE # 作業異當終止:無法儲存檔案
else:
logging_process_step(f"儲存檔案至路徑:{file_path}")
return EXIT_CODE_SUCCESS # 作業正常結束
# =============================================================================
# 程式主流程
# =============================================================================
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 工作簿
# =========================================================================
wb = None
try:
wb = xw.apps.active.books.active
except Exception as 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
# =========================================================================
# (3) 處理作業
# =========================================================================
try:
result_code = process(wb)
if result_code != EXIT_CODE_SUCCESS:
logging.error("處理作業失敗,過程中出錯!")
return result_code
except Exception as e:
print(f"執行過程中發生未知錯誤: {e}")
logging.error(f"執行過程中發生未知錯誤: {e}", exc_info=True)
return EXIT_CODE_UNKNOWN_ERROR
finally:
if wb:
logging_process_step(f"製作【漢字標音】網頁己完成!")
# =========================================================================
# 結束作業
# =========================================================================
logging.info("作業完成!")
return EXIT_CODE_SUCCESS
if __name__ == "__main__":
exit_code = main()
if exit_code == EXIT_CODE_SUCCESS:
print("程式正常完成!")
else:
print(f"程式異常終止,錯誤代碼為: {exit_code}")
sys.exit(exit_code)