diff --git a/__pycache__/x2_master_export2translate.cpython-310.pyc b/__pycache__/x2_master_export2translate.cpython-310.pyc index ae0cef5..3617308 100644 Binary files a/__pycache__/x2_master_export2translate.cpython-310.pyc and b/__pycache__/x2_master_export2translate.cpython-310.pyc differ diff --git a/__pycache__/x6_update_from_master.cpython-310.pyc b/__pycache__/x6_update_from_master.cpython-310.pyc index b79170e..755bedb 100644 Binary files a/__pycache__/x6_update_from_master.cpython-310.pyc and b/__pycache__/x6_update_from_master.cpython-310.pyc differ diff --git a/translation_config.json b/translation_config.json index c2f8afc..e443b63 100644 --- a/translation_config.json +++ b/translation_config.json @@ -1,12 +1,12 @@ { "codigo_tipo_PLC": "allenbradley", - "codigo_columna_maestra": "en-US", - "codigo_idioma_seleccionado": "es-ES", - "codigo_idioma_secundario": "en-US", + "codigo_columna_maestra": "es-ES", + "codigo_idioma_seleccionado": "en-US", + "codigo_idioma_secundario": "es-ES", "work_dir": "C:/Trabajo/VM/31 - 9.3953 - Omega - Mexico - (ABradley)/Reporte/Languages", "master_name": "1_hmi_master_translates_allenbradley.xlsx", - "translate_name": "2_master_export2translate_allenbradley_es-ES.xlsx", - "auto_translate_name": "3_master_export2translate_translated_allenbradley_es-ES.xlsx", + "translate_name": "2_master_export2translate_allenbradley_en-US.xlsx", + "auto_translate_name": "3_master_export2translate_translated_allenbradley_en-US.xlsx", "nivel_afinidad_minimo": 0.5, "traducir_todo": false } \ No newline at end of file diff --git a/x2_master_export2translate.py b/x2_master_export2translate.py index 6f1cab4..19b7fe2 100644 --- a/x2_master_export2translate.py +++ b/x2_master_export2translate.py @@ -1,12 +1,11 @@ import pandas as pd import os -import sys -import time import PyLibrary.funciones_comunes as fc from translation_config import TranslationConfig import langid from openpyxl import load_workbook -from openpyxl.styles import PatternFill, Alignment +from openpyxl.styles import PatternFill, Alignment, Font +from collections import defaultdict # Definir el logger a nivel de módulo logger = None @@ -71,15 +70,26 @@ def exportar_para_traduccion(config: TranslationConfig): config.codigo_idioma_seleccionado ) fill = PatternFill(start_color="ADD8E6", end_color="ADD8E6", fill_type="solid") + bold_font = Font(bold=True) total_rows = worksheet.max_row - 1 # Excluimos la fila de encabezado progress_bar = fc.ProgressBar( total_rows, prefix="Procesando filas:", suffix="Completado" ) + print("Iniciando procesamiento de filas...") + + texto_a_filas = defaultdict(list) for row in range(2, worksheet.max_row + 1): texto = worksheet.cell(row=row, column=2).value if texto: + texto_limpio = fc.limpiar_texto(config.codigo_tipo_PLC, texto) + + # Solo considerar para duplicados si el texto limpio es igual al original + if texto == texto_limpio: + texto_a_filas[texto].append(row) + + # Detectar idioma y marcar si es incorrecto idioma_detectado = detectar_idioma(texto, config.codigo_tipo_PLC) if ( idioma_detectado != "unknown" @@ -88,7 +98,17 @@ def exportar_para_traduccion(config: TranslationConfig): worksheet.cell(row=row, column=2).fill = fill nombre_idioma = obtener_nombre_idioma(idioma_detectado) worksheet.cell(row=row, column=3).value = nombre_idioma - progress_bar.update(row - 1) + + progress_bar.increment() + + # Marcar celdas duplicadas en negrita + celdas_duplicadas = 0 + for filas in texto_a_filas.values(): + if len(filas) > 1: + for row in filas: + cell = worksheet.cell(row=row, column=2) + cell.font = bold_font + celdas_duplicadas += len(filas) progress_bar.finish() @@ -97,66 +117,8 @@ def exportar_para_traduccion(config: TranslationConfig): print( "Se ha añadido el nombre del idioma detectado cuando es diferente del esperado." ) - master_path = config.get_master_path() - if not os.path.exists(master_path): - print("El archivo maestro no existe.") - return - - configurar_detector_idiomas() - - df_maestro = fc.read_dataframe_with_cleanup_retries(master_path) - - df_export = pd.DataFrame() - primera_columna = df_maestro.columns[0] - df_export[primera_columna] = df_maestro[primera_columna] - df_export[config.codigo_idioma_seleccionado] = df_maestro[ - config.codigo_idioma_seleccionado - ] - df_export["Idioma_Detectado"] = "" - - ruta_export = config.get_translate_path() - - with pd.ExcelWriter(ruta_export, engine="openpyxl") as writer: - df_export.to_excel(writer, index=False, sheet_name="Sheet1") - - workbook = writer.book - worksheet = writer.sheets["Sheet1"] - - wrap_alignment = Alignment(wrap_text=True, vertical="top") - for col in ["A", "B"]: - for cell in worksheet[col]: - cell.alignment = wrap_alignment - worksheet.column_dimensions[col].width = 50 - - idioma_esperado = fc.idiomas_shortcodefromcode( - config.codigo_idioma_seleccionado - ) - fill = PatternFill(start_color="ADD8E6", end_color="ADD8E6", fill_type="solid") - - total_rows = worksheet.max_row - 1 # Excluimos la fila de encabezado - progress_bar = fc.ProgressBar( - total_rows, prefix="Progreso:", suffix="Completado" - ) - - for row in range(2, worksheet.max_row + 1): - texto = worksheet.cell(row=row, column=2).value - if texto: - idioma_detectado = detectar_idioma(texto, config.codigo_tipo_PLC) - if ( - idioma_detectado != "unknown" - and idioma_detectado != idioma_esperado - ): - worksheet.cell(row=row, column=2).fill = fill - nombre_idioma = obtener_nombre_idioma(idioma_detectado) - worksheet.cell(row=row, column=3).value = nombre_idioma - progress_bar.increment() - - progress_bar.finish() - - print(f"\nArchivo exportado para traducción: {ruta_export}") - print("Las celdas con idioma incorrecto han sido marcadas en azul.") print( - "Se ha añadido el nombre del idioma detectado cuando es diferente del esperado." + f"Se han marcado {celdas_duplicadas} celdas en negrita por tener texto duplicado en la columna del idioma seleccionado." ) diff --git a/x6_update_from_master.py b/x6_update_from_master.py index 7a6e296..ec5cc02 100644 --- a/x6_update_from_master.py +++ b/x6_update_from_master.py @@ -3,7 +3,7 @@ import os import PyLibrary.funciones_comunes as fc from translation_config import TranslationConfig from openpyxl import load_workbook -from openpyxl.styles import Font +from openpyxl.styles import Font, PatternFill from openpyxl.comments import Comment # Definir el logger a nivel de módulo @@ -27,13 +27,19 @@ def update_from_master(config: TranslationConfig, archivo_to_update): col_clave = config.codigo_columna_maestra celdas_con_errores = {} + celdas_vacias = {} + + progress_bar = fc.ProgressBar( + len(df_to_update), prefix="Actualizando filas:", suffix="Completado" + ) for index, fila in df_to_update.iterrows(): valor_original = fila[col_clave] clave = fc.compactar_celda_clave(config.codigo_tipo_PLC, valor_original) - logger.info(f"Fila {index} : Clave: {clave}") - if not pd.isnull(clave) and clave in df_maestro[col_clave].values: + if not pd.isna(clave) and clave in df_maestro[col_clave].values: + # logger.info(f"Fila {index} : Clave: {clave}") + indice_maestro = df_maestro.index[df_maestro[col_clave] == clave].tolist()[ 0 ] @@ -41,50 +47,60 @@ def update_from_master(config: TranslationConfig, archivo_to_update): indice_maestro, config.codigo_idioma_seleccionado ] - valor_traducido = fc.decompactar_celda_traducida( - config.codigo_tipo_PLC, - celda_original=valor_original, - celda_traducida=valor_traducido_compacto, - ) - if ( - not pd.isnull(valor_traducido) - and fila[config.codigo_idioma_seleccionado] != valor_traducido - ): - okToSave, Error = fc.verificar_celda_traducida( - config.codigo_tipo_PLC, clave, valor_traducido_compacto + if pd.isna(valor_traducido_compacto): + celdas_vacias[indice_maestro] = "Celda vacía en el archivo maestro" + logger.info(f"Fila {index}: Celda vacía en el archivo maestro") + else: + valor_traducido = fc.decompactar_celda_traducida( + config.codigo_tipo_PLC, + celda_original=valor_original, + celda_traducida=valor_traducido_compacto, ) - if okToSave: - logger.info("Actualizado.") - df_to_update.at[index, config.codigo_idioma_seleccionado] = ( - valor_traducido + if ( + not pd.isna(valor_traducido) + and fila[config.codigo_idioma_seleccionado] != valor_traducido + ): + okToSave, Error = fc.verificar_celda_traducida( + config.codigo_tipo_PLC, clave, valor_traducido_compacto ) - else: - df_to_update.at[index, config.codigo_idioma_seleccionado] = ( - valor_original - ) - logger.error(f"No actualizado porque: {Error}") - celdas_con_errores[indice_maestro] = Error + if okToSave: + logger.info(f"Actualizado: Fila {index} : Clave: {clave}") + df_to_update.at[index, config.codigo_idioma_seleccionado] = ( + valor_traducido + ) + else: + df_to_update.at[index, config.codigo_idioma_seleccionado] = ( + valor_original + ) + logger.error(f"No actualizado porque: {Error}") + celdas_con_errores[indice_maestro] = Error + + progress_bar.increment() + + progress_bar.finish() nombre, extension = os.path.splitext(archivo_to_update) nuevo_nombre = f"{nombre}_import{extension}" fc.save_dataframe_with_retries(df_to_update, output_path=nuevo_nombre) marcar_celdas_con_errores( - archivo_maestro, celdas_con_errores, config.codigo_idioma_seleccionado + archivo_maestro, + celdas_con_errores, + celdas_vacias, + config.codigo_idioma_seleccionado, ) print( - f"Se han actualizado las filas en {archivo_to_update} desde el archivo maestro. " - f"Se han marcado {len(celdas_con_errores)} celdas con errores en el archivo maestro." - ) - logger.info( - f"Se han actualizado las filas en {archivo_to_update} desde el archivo maestro. " + f"Se han actualizado las filas en {archivo_to_update} desde el archivo maestro." + ) + print( f"Se han marcado {len(celdas_con_errores)} celdas con errores en el archivo maestro." ) + print(f"Se han marcado {len(celdas_vacias)} celdas vacías en el archivo maestro.") logger.info(" .... ") -def marcar_celdas_con_errores(archivo_maestro, celdas_con_errores, target_lang_code): +def marcar_celdas_con_errores(archivo_maestro, celdas_con_errores, celdas_vacias, target_lang_code): workbook = load_workbook(archivo_maestro) sheet = workbook.active @@ -96,27 +112,35 @@ def marcar_celdas_con_errores(archivo_maestro, celdas_con_errores, target_lang_c print(f"No se encontró la columna para el idioma {target_lang_code}") return + error_fill = PatternFill(start_color="FF0000", end_color="FF0000", fill_type="solid") + empty_fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") + white_font = Font(color="FFFFFF") # Fuente blanca para celdas con fondo rojo + for indice_maestro, mensaje_error in celdas_con_errores.items(): row = indice_maestro + 2 cell = sheet.cell(row=row, column=target_col) - cell.font = Font(color="FF0000") - + cell.fill = error_fill + cell.font = white_font # Aplicar fuente blanca comment = Comment(mensaje_error, "Sistema de Traducción") cell.comment = comment + for indice_maestro, mensaje in celdas_vacias.items(): + row = indice_maestro + 2 + cell = sheet.cell(row=row, column=target_col) + cell.fill = empty_fill + comment = Comment(mensaje, "Sistema de Traducción") + cell.comment = comment + workbook.save(archivo_maestro) - print( - f"Se han marcado {len(celdas_con_errores)} celdas con errores en rojo en el archivo maestro {archivo_maestro}" - ) + print(f"Se han marcado las celdas con errores y vacías en el archivo maestro {archivo_maestro}") def run(config: TranslationConfig, archivo_to_update): global logger logger = fc.configurar_logger(config.work_dir) - if archivo_to_update: - script_name = os.path.basename(__file__) - print(f"\rIniciando: {script_name}\r") - update_from_master(config, archivo_to_update) + script_name = os.path.basename(__file__) + print(f"\rIniciando: {script_name}\r") + update_from_master(config, archivo_to_update) if __name__ == "__main__":