first commit

This commit is contained in:
sepehr 2025-04-06 21:40:04 +02:00
parent 64e54819aa
commit 11b85d6111

75
main.py
View File

@ -5,6 +5,7 @@ import os
from tqdm import tqdm from tqdm import tqdm
import copy import copy
import re import re
from openpyxl.utils import get_column_letter
async def translate_text(translator, text, target_language): async def translate_text(translator, text, target_language):
try: try:
@ -25,17 +26,53 @@ def copy_cell_format(source_cell, target_cell):
if source_cell.has_style: if source_cell.has_style:
try: try:
# Copy individual style attributes instead of the entire style object # Copy individual style attributes instead of the entire style object
target_cell.font = copy.copy(source_cell.font) if source_cell.font:
target_cell.border = copy.copy(source_cell.border) target_cell.font = copy.copy(source_cell.font)
target_cell.fill = copy.copy(source_cell.fill) if source_cell.border:
target_cell.number_format = source_cell.number_format target_cell.border = copy.copy(source_cell.border)
target_cell.protection = copy.copy(source_cell.protection) if source_cell.fill:
target_cell.alignment = copy.copy(source_cell.alignment) target_cell.fill = copy.copy(source_cell.fill)
if source_cell.number_format:
target_cell.number_format = source_cell.number_format
if source_cell.protection:
target_cell.protection = copy.copy(source_cell.protection)
if source_cell.alignment:
target_cell.alignment = copy.copy(source_cell.alignment)
# Copy any hyperlink # Copy any hyperlink
if source_cell.hyperlink: if source_cell.hyperlink:
target_cell.hyperlink = source_cell.hyperlink target_cell.hyperlink = source_cell.hyperlink
except Exception as e: except Exception as e:
print(f"Error copying format: {e}") print(f"Error copying format for cell {source_cell.coordinate}: {e}")
def copy_sheet_properties(source_sheet, target_sheet):
"""Copy all sheet properties from source to target sheet"""
# Copy column dimensions for all columns
for col_idx in range(1, source_sheet.max_column + 1):
col_letter = get_column_letter(col_idx)
if col_letter in source_sheet.column_dimensions:
source_dim = source_sheet.column_dimensions[col_letter]
target_dim = target_sheet.column_dimensions[col_letter]
# Copy all available attributes
if hasattr(source_dim, 'width') and source_dim.width:
target_dim.width = source_dim.width
if hasattr(source_dim, 'hidden'):
target_dim.hidden = source_dim.hidden
if hasattr(source_dim, 'outlineLevel'):
target_dim.outlineLevel = source_dim.outlineLevel
# Copy row dimensions for all rows
for row_idx in range(1, source_sheet.max_row + 1):
if row_idx in source_sheet.row_dimensions:
source_dim = source_sheet.row_dimensions[row_idx]
target_dim = target_sheet.row_dimensions[row_idx]
if hasattr(source_dim, 'height') and source_dim.height:
target_dim.height = source_dim.height
if hasattr(source_dim, 'hidden'):
target_dim.hidden = source_dim.hidden
if hasattr(source_dim, 'outlineLevel'):
target_dim.outlineLevel = source_dim.outlineLevel
async def translate_excel(file_path: str, target_language: str): async def translate_excel(file_path: str, target_language: str):
translator = Translator() translator = Translator()
@ -54,32 +91,32 @@ async def translate_excel(file_path: str, target_language: str):
original_sheet = workbook[sheet_name] original_sheet = workbook[sheet_name]
translated_sheet = translated_workbook.create_sheet(title=sheet_name) translated_sheet = translated_workbook.create_sheet(title=sheet_name)
# Copy sheet properties (column dimensions, etc.) print(f"Processing sheet: {sheet_name} with {original_sheet.max_column} columns")
for key, dimension in original_sheet.column_dimensions.items():
if hasattr(dimension, 'width') and dimension.width: # Copy sheet properties using the improved function
translated_sheet.column_dimensions[key].width = dimension.width copy_sheet_properties(original_sheet, translated_sheet)
for key, dimension in original_sheet.row_dimensions.items():
if hasattr(dimension, 'height') and dimension.height:
translated_sheet.row_dimensions[key].height = dimension.height
# Copy merged cells # Copy merged cells
for merged_cell_range in original_sheet.merged_cells: for merged_cell_range in original_sheet.merged_cells:
translated_sheet.merge_cells(str(merged_cell_range)) translated_sheet.merge_cells(str(merged_cell_range))
# Process each cell
for row in original_sheet.iter_rows(): for row in original_sheet.iter_rows():
for cell in row: for cell in row:
progress_bar.update(1) progress_bar.update(1)
col_idx = cell.column
row_idx = cell.row
# Create the cell at the same position in the new sheet # Create the cell at the same position in the new sheet
if cell.value: if cell.value is not None: # Use is not None to include 0 values
if is_formula(cell.value): if is_formula(cell.value):
# Don't translate formulas # Don't translate formulas
translated_cell = translated_sheet.cell(row=cell.row, column=cell.column, value=cell.value) translated_cell = translated_sheet.cell(row=row_idx, column=col_idx, value=cell.value)
else: else:
translated_text = await translate_text(translator, str(cell.value), target_language) translated_text = await translate_text(translator, str(cell.value), target_language)
translated_cell = translated_sheet.cell(row=cell.row, column=cell.column, value=translated_text) translated_cell = translated_sheet.cell(row=row_idx, column=col_idx, value=translated_text)
else: else:
translated_cell = translated_sheet.cell(row=cell.row, column=cell.column) translated_cell = translated_sheet.cell(row=row_idx, column=col_idx)
# Copy formatting # Copy formatting
copy_cell_format(cell, translated_cell) copy_cell_format(cell, translated_cell)