Files
Document_Translator/regenerate_korean_excel.py
2025-09-03 15:07:34 +08:00

119 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
重新生成正確的韓文翻譯Excel檔案
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# 設定編碼
sys.stdout.reconfigure(encoding='utf-8')
from pathlib import Path
from app import create_app
def regenerate_korean_excel():
"""重新生成韓文翻譯Excel檔案"""
print("=" * 80)
print("重新生成韓文翻譯Excel檔案")
print("使用補充後的韓文快取 (覆蓋率: 97.4%)")
print("=" * 80)
# 檔案路徑
prod_dir = Path(r"C:\Users\EGG\WORK\data\user_scrip\TOOL\Document_translator_V2\uploads\98158984-f335-44f5-a0b4-88fb8ccd5d78")
original_file = prod_dir / "original_panjit_98158984.xlsx"
if not original_file.exists():
print(f"❌ 原始文件不存在: {original_file}")
return
print(f"✅ 原始文件: {original_file.name}")
app = create_app()
with app.app_context():
from app.services.translation_service import ExcelParser
from app import db
try:
print(f"\n1. 創建Excel解析器")
print("-" * 60)
parser = ExcelParser(str(original_file))
print(f"✅ Excel解析器創建成功")
print(f"\n2. 生成韓文翻譯檔案")
print("-" * 60)
# 使用空的translations字典讓系統從快取中查詢
translated_file_path = parser.generate_translated_document(
translations={},
target_language='ko',
output_dir=prod_dir
)
print(f"✅ 韓文翻譯檔案已生成: {Path(translated_file_path).name}")
print(f"\n3. 驗證翻譯結果")
print("-" * 60)
import openpyxl
# 檢查新生成的翻譯檔案
wb_trans = openpyxl.load_workbook(translated_file_path, data_only=False)
# 檢查關鍵儲存格
test_cells = ['D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'F2', 'F3', 'F4', 'F5', 'F6']
translated_count = 0
for cell_name in test_cells:
cell_val = wb_trans.active[cell_name].value
if isinstance(cell_val, str) and '\n' in cell_val:
lines = cell_val.split('\n')
if len(lines) >= 2:
original_text = lines[0].strip()
translated_text = '\n'.join(lines[1:]).strip()
print(f"{cell_name}: 已翻譯")
print(f" 原文: {original_text[:30]}...")
print(f" 韓文: {translated_text[:30]}...")
translated_count += 1
else:
print(f"{cell_name}: 格式異常")
else:
print(f"{cell_name}: 未翻譯")
wb_trans.close()
print(f"\n翻譯檢查結果: {translated_count}/{len(test_cells)} 個儲存格成功翻譯")
if translated_count >= len(test_cells) * 0.8: # 80%以上成功
print("🎉 韓文翻譯檔案生成成功!")
print(f" 檔案位置: {translated_file_path}")
print(" 大部分內容已正確翻譯")
else:
print("⚠️ 翻譯檔案生成部分成功,但部分內容可能未翻譯")
# 4. 提供下載資訊
print(f"\n4. 下載資訊")
print("-" * 60)
print(f"韓文翻譯檔案已準備就緒:")
print(f" 檔案名稱: {Path(translated_file_path).name}")
print(f" 檔案路徑: {translated_file_path}")
print(f" 檔案大小: {Path(translated_file_path).stat().st_size / 1024:.1f} KB")
except Exception as e:
print(f"❌ 生成韓文翻譯檔案時發生錯誤: {str(e)}")
import traceback
print(f"錯誤詳情: {traceback.format_exc()}")
print(f"\n" + "=" * 80)
print("韓文翻譯Excel檔案重新生成完成")
print("現在D2-D8和F2-F6欄位應該都有正確的韓文翻譯")
print("=" * 80)
if __name__ == "__main__":
regenerate_korean_excel()