Загрузка данных


import os
import struct

input_dir = r"C:\Otomate\original"
output_dir = r"C:\Otomate\txt_output"

if not os.path.exists(output_dir):
    os.makedirs(output_dir)

def extract_strings_from_stcm(filepath):
    with open(filepath, 'rb') as f:
        data = f.read()
    
    # Проверяем сигнатуру STCM
    if not data.startswith(b'STCM'):
        return []
    
    extracted = []
    # Ищем английский текст (длиной от 3 символов, буквы, цифры, знаки препинания)
    # В файлах STCM строки обычно заканчиваются нулевым байтом (0x00)
    current_str = bytearray()
    for byte in data:
        if 32 <= byte <= 126: # Печатные ASCII символы (английский язык)
            current_str.append(byte)
        else:
            if len(current_str) >= 3:
                try:
                    text = current_str.decode('ascii').strip()
                    # Убираем технический мусор движка
                    if text and not text.startswith(('STCM', 'CODE', 'DATA', 'EXPT', 'NAME')):
                        extracted.append(text)
                except:
                    pass
            current_str = bytearray()
    return extracted

print("Начинаю извлечение текста...")
count = 0

for filename in os.listdir(input_dir):
    if filename.endswith(".stcm2l"):
        file_path = os.path.join(input_dir, filename)
        strings = extract_strings_from_stcm(file_path)
        
        if strings:
            out_name = filename.replace(".stcm2l", ".txt")
            out_path = os.path.join(output_dir, out_name)
            with open(out_path, 'w', encoding='utf-8') as out_f:
                for s in strings:
                    out_f.write(f'text "{s}"\n')
            count += 1

print(f"Готово! Успешно обработано файлов: {count}. Результаты в папочке C:\\Otomate\\txt_output")