Add system prompt, glossary, presets for Ollama/WebLLM, image translation support

This commit is contained in:
2025-11-30 16:45:41 +01:00
parent 465cab8a61
commit e48ea07e44
6 changed files with 497 additions and 51 deletions

View File

@@ -3,6 +3,8 @@ Excel Translation Module
Translates Excel files while preserving all formatting, formulas, images, and layout
"""
import re
import tempfile
import os
from pathlib import Path
from typing import Dict, Set
from openpyxl import load_workbook
@@ -40,6 +42,10 @@ class ExcelTranslator:
worksheet = workbook[sheet_name]
self._translate_worksheet(worksheet, target_language)
# Translate images if enabled
if getattr(self.translation_service, 'translate_images', False):
self._translate_images(worksheet, target_language)
# Prepare translated sheet name (but don't rename yet)
translated_sheet_name = self.translation_service.translate_text(
sheet_name, target_language
@@ -155,6 +161,54 @@ class ExcelTranslator:
return False
return True
def _translate_images(self, worksheet: Worksheet, target_language: str):
"""
Translate text in images using vision model and add as comments
"""
from services.translation_service import OllamaTranslationProvider
if not isinstance(self.translation_service.provider, OllamaTranslationProvider):
return
try:
# Get images from worksheet
images = getattr(worksheet, '_images', [])
for idx, image in enumerate(images):
try:
# Get image data
image_data = image._data()
ext = image.format or 'png'
# Save to temp file
with tempfile.NamedTemporaryFile(suffix=f'.{ext}', delete=False) as tmp:
tmp.write(image_data)
tmp_path = tmp.name
# Translate with vision
translated_text = self.translation_service.provider.translate_image(tmp_path, target_language)
# Clean up
os.unlink(tmp_path)
if translated_text and translated_text.strip():
# Add translation as a cell near the image
anchor = image.anchor
if hasattr(anchor, '_from'):
cell_ref = f"{get_column_letter(anchor._from.col + 1)}{anchor._from.row + 1}"
cell = worksheet[cell_ref]
# Add as comment
from openpyxl.comments import Comment
cell.comment = Comment(f"Image translation: {translated_text}", "Translator")
print(f"Added Excel image translation at {cell_ref}: {translated_text[:50]}...")
except Exception as e:
print(f"Error translating Excel image {idx}: {e}")
continue
except Exception as e:
print(f"Error processing Excel images: {e}")
# Global translator instance