diff --git a/main.py b/main.py index ecd1323..371f097 100644 --- a/main.py +++ b/main.py @@ -5,6 +5,7 @@ import os from tqdm import tqdm import copy import re +from openpyxl.utils import get_column_letter async def translate_text(translator, text, target_language): try: @@ -25,17 +26,53 @@ def copy_cell_format(source_cell, target_cell): if source_cell.has_style: try: # Copy individual style attributes instead of the entire style object - target_cell.font = copy.copy(source_cell.font) - target_cell.border = copy.copy(source_cell.border) - target_cell.fill = copy.copy(source_cell.fill) - target_cell.number_format = source_cell.number_format - target_cell.protection = copy.copy(source_cell.protection) - target_cell.alignment = copy.copy(source_cell.alignment) + if source_cell.font: + target_cell.font = copy.copy(source_cell.font) + if source_cell.border: + target_cell.border = copy.copy(source_cell.border) + if source_cell.fill: + 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 if source_cell.hyperlink: target_cell.hyperlink = source_cell.hyperlink 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): translator = Translator() @@ -54,32 +91,32 @@ async def translate_excel(file_path: str, target_language: str): original_sheet = workbook[sheet_name] translated_sheet = translated_workbook.create_sheet(title=sheet_name) - # Copy sheet properties (column dimensions, etc.) - for key, dimension in original_sheet.column_dimensions.items(): - if hasattr(dimension, 'width') and dimension.width: - translated_sheet.column_dimensions[key].width = dimension.width - - for key, dimension in original_sheet.row_dimensions.items(): - if hasattr(dimension, 'height') and dimension.height: - translated_sheet.row_dimensions[key].height = dimension.height + print(f"Processing sheet: {sheet_name} with {original_sheet.max_column} columns") + + # Copy sheet properties using the improved function + copy_sheet_properties(original_sheet, translated_sheet) # Copy merged cells for merged_cell_range in original_sheet.merged_cells: translated_sheet.merge_cells(str(merged_cell_range)) + # Process each cell for row in original_sheet.iter_rows(): for cell in row: progress_bar.update(1) + col_idx = cell.column + row_idx = cell.row + # 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): # 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: 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: - 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_cell_format(cell, translated_cell)