Skip to content

Commit

Permalink
a760 改成可直接使用 “作用中活頁簿檔” ,另存新檔成 Tai_Gi_Zu_Im_Bun.xlsx 工作檔。
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanJui committed Sep 28, 2024
1 parent 67eef89 commit 7384710
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"program": "a760_漢字注音檔存成工作暫存檔.py",
"console": "integratedTerminal",
"args": [
"-i 【河洛話注音】金剛般若波羅蜜經-006.xlsx",
"-o Tai_Gi_Zu_Im_Bun.xlsx",
// "-i 【河洛話注音】金剛般若波羅蜜經。無為福勝分第十一.xlsx",
// "-o Tai_Gi_Zu_Im_Bun.xlsx",
]
},
{
Expand Down
Binary file modified Tai_Gi_Zu_Im_Bun.xlsx
Binary file not shown.
86 changes: 58 additions & 28 deletions a760_漢字注音檔存成工作暫存檔.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#==============================================================================
# 說明:
#
# 本程式可將當前打開的 Excel 檔案另存為指定的檔名。
#
# 1. wb = xw.apps.active.books.active:這行代碼使用 xlwings 獲取當前作用中的 Excel 工作簿。
# 如果沒有已打開的工作簿,它會提示並退出程式。
#
# 2. output_file:通過命令行參數獲取輸出的檔名,若沒有指定,則使用預設的 Tai_Gi_Zu_Im_Bun.xlsx。
# project_root:將新檔案儲存到專案根目錄。
#
# 【執行範例】:
# 在CMD 執行以下命令:
# python your_script.py -o Tai_Gi_Zu_Im_Bun.xlsx
#
# 3. 如果沒有指定參數 -o,預設之另存新檔名稱為 Tai_Gi_Zu_Im_Bun.xlsx。
#==============================================================================
import getopt
import os
import sys
from pathlib import Path

import xlwings as xw

import settings


def get_input_and_output_options(argv):
arg_input = ""
Expand Down Expand Up @@ -42,16 +57,8 @@ def get_input_and_output_options(argv):

if __name__ == "__main__":
# =========================================================================
# (1) 取得需要注音的「檔案名稱」及其「目錄路徑」
# (1) 取得專案根目錄
# =========================================================================
# 取得 Input 檔案名稱
opts = get_input_and_output_options(sys.argv)
if opts["input"] != "":
CONVERT_FILE_NAME = opts["input"].replace(" ", "")
else:
CONVERT_FILE_NAME = ""
print(f"CONVERT_FILE_NAME = {CONVERT_FILE_NAME}")

# 獲取當前檔案的路徑
current_file_path = Path(__file__).resolve()

Expand All @@ -60,30 +67,53 @@ def get_input_and_output_options(argv):

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

# 打開 Excel 檔案
source_path = "output2"
old_file_path = os.path.join(
project_root,
"{0}".format(source_path),
f"{CONVERT_FILE_NAME}")
wb = xw.Book(f"{old_file_path}")

# =========================================================================
# (2) 依據《文章標題》另存新檔
# (2) 取得輸入及輸出的檔案名稱
# =========================================================================
setting_sheet = wb.sheets["env"]
new_file_name = str(
setting_sheet.range("C4").value
).strip()

# 將檔案存放路徑設為:【專案根目錄】之下
# 取得新的檔案名稱
opts = get_input_and_output_options(sys.argv)
output_file = opts["output"].replace(" ", "")
if output_file == "":
output_file = "Tai_Gi_Zu_Im_Bun.xlsx" # 預設檔名

# =========================================================================
# (3) 若無指定輸入檔案,則獲取當前作用中的 Excel 檔案並另存新檔。
# =========================================================================
wb = None
input_file = opts["input"].replace(" ", "")
if input_file:
# 打開 Excel 檔案
source_path = "output2"
old_file_path = os.path.join(
project_root,
"{0}".format(source_path),
f"{input_file}")
wb = xw.Book(f"{old_file_path}")
else:
# 使用已打開且處於作用中的 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)

# 將檔案存放路徑設為【專案根目錄】之下
new_file_path = os.path.join(
project_root,
f"{output_file}")
f"{output_file}"
)

# 儲存新建立的工作簿
print(f"正在將檔案另存為: {new_file_path}")
wb.save(new_file_path)

# 保存 Excel 檔案
# 關閉工作簿
wb.close()

print(f"檔案已成功存為 {new_file_path}")

0 comments on commit 7384710

Please sign in to comment.