Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -55,9 +55,20 @@ def translate_document(file, target_language, target_country, api_key):
|
|
55 |
for slide in prs.slides:
|
56 |
for shape in slide.shapes:
|
57 |
if hasattr(shape, "text") and shape.text.strip(): # 只翻譯非空文本框
|
|
|
|
|
|
|
|
|
|
|
58 |
translated_text = GoogleTranslator(source='auto', target=target_language).translate(shape.text)
|
59 |
shape.text = translated_text # 用翻譯後的文本替換原始文本
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
output_path = "translated_" + os.path.basename(original_file_path)
|
62 |
prs.save(output_path)
|
63 |
log_messages.append(f"Saved translated presentation as {output_path}.")
|
|
|
55 |
for slide in prs.slides:
|
56 |
for shape in slide.shapes:
|
57 |
if hasattr(shape, "text") and shape.text.strip(): # 只翻譯非空文本框
|
58 |
+
# 保存原始格式信息
|
59 |
+
original_font_size = None
|
60 |
+
if shape.has_text_frame:
|
61 |
+
original_font_size = shape.text_frame.paragraphs[0].runs[0].font.size
|
62 |
+
|
63 |
translated_text = GoogleTranslator(source='auto', target=target_language).translate(shape.text)
|
64 |
shape.text = translated_text # 用翻譯後的文本替換原始文本
|
65 |
|
66 |
+
# 恢復原始格式
|
67 |
+
if shape.has_text_frame and original_font_size is not None:
|
68 |
+
for paragraph in shape.text_frame.paragraphs:
|
69 |
+
for run in paragraph.runs:
|
70 |
+
run.font.size = original_font_size
|
71 |
+
|
72 |
output_path = "translated_" + os.path.basename(original_file_path)
|
73 |
prs.save(output_path)
|
74 |
log_messages.append(f"Saved translated presentation as {output_path}.")
|