HMI_Translate_Helper_wMaste.../x2_master_export2translate.py

51 lines
2.0 KiB
Python
Raw Normal View History

import pandas as pd
import os
import PyLibrary.funciones_comunes as fc
2024-09-19 04:05:19 -03:00
def exportar_para_traduccion(tipo_PLC, archivo_maestro, target_lang_code):
if not os.path.exists(archivo_maestro):
print("El archivo maestro no existe.")
return
# Leer el archivo maestro
df_maestro = fc.read_dataframe_with_cleanup_retries(archivo_maestro)
# Crear un nuevo DataFrame para la exportación
df_export = pd.DataFrame()
# Copiar la primera columna y la columna del idioma de destino
primera_columna = df_maestro.columns[0]
df_export[primera_columna] = df_maestro[primera_columna]
df_export[target_lang_code] = df_maestro[target_lang_code]
# Guardar el archivo exportado
2024-09-19 04:05:19 -03:00
ruta_export = os.path.join(os.path.dirname(archivo_maestro), f'2_master_export2translate_{tipo_PLC}.xlsx')
# Usar ExcelWriter para tener más control sobre el proceso de escritura
with pd.ExcelWriter(ruta_export, engine='openpyxl') as writer:
df_export.to_excel(writer, index=False, sheet_name='Sheet1')
# Ajustar el ancho de las columnas
worksheet = writer.sheets['Sheet1']
for idx, col in enumerate(df_export.columns):
max_length = max(df_export[col].astype(str).map(len).max(), len(col))
if max_length > 50 : max_length = 50
worksheet.column_dimensions[chr(65 + idx)].width = max_length + 2
print(f"Archivo exportado para traducción: {ruta_export}")
2024-09-19 15:55:58 -03:00
def run(tipo_PLC, codigo_columna_maestra, seleccion_idioma):
2024-09-19 04:05:19 -03:00
archivo_maestro = f".\\data\\1_hmi_master_translates_{tipo_PLC}.xlsx"
if seleccion_idioma not in fc.IDIOMAS:
print("Selección inválida.")
else:
_, target_lang_code = fc.IDIOMAS[seleccion_idioma]
2024-09-19 04:05:19 -03:00
exportar_para_traduccion(tipo_PLC, archivo_maestro, target_lang_code)
if __name__ == "__main__":
2024-09-19 04:05:19 -03:00
tipo_PLC = "siemens"
codigo_columna_maestra = "it-IT"
fc.mostrar_idiomas()
seleccion_idioma = int(input("Introduce el número del idioma de destino: "))
run(tipo_PLC, codigo_columna_maestra, seleccion_idioma)