Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| Скрипт для тестирования парсера DOCX документов | |
| и сохранения результатов в JSON формате. | |
| Положите ваш файл test.docx в директорию test_input и запустите скрипт. | |
| Результат будет сохранен в файл test_output/test.json | |
| """ | |
| from datetime import datetime | |
| import json | |
| import logging | |
| import sys | |
| from pathlib import Path | |
| # Добавляем родительскую директорию в sys.path, чтобы импорты работали корректно | |
| # при запуске скрипта из верхнего уровня | |
| sys.path.insert(0, str(Path(__file__).parent.parent.parent)) | |
| from parser import UniversalParser # type: ignore | |
| logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler()]) | |
| def main(): | |
| """ | |
| Основная функция скрипта. | |
| 1. Считывает файл test_input/test.docx | |
| 2. Парсит его с помощью UniversalParser | |
| 3. Сохраняет результат в test_output/test.json | |
| """ | |
| # Получаем абсолютные пути к файлам относительно корневой директории проекта | |
| project_root = Path(__file__).parent.parent | |
| input_file = project_root / "test_input" / "test.docx" | |
| output_file = project_root / "test_output" / "test.json" | |
| # Проверяем существование входного файла | |
| if not input_file.exists(): | |
| print( | |
| f"Ошибка: Файл {input_file} не найден. Пожалуйста, поместите файл test.docx в директорию test_input." | |
| ) | |
| return 1 | |
| # Создаем директорию для выходных файлов, если она не существует | |
| output_file.parent.mkdir(parents=True, exist_ok=True) | |
| print(f"Парсинг файла: {input_file}") | |
| # Создаем экземпляр универсального парсера | |
| parser = UniversalParser() | |
| # Парсим документ | |
| parsed_document = parser.parse_by_path(str(input_file)) | |
| if parsed_document is None: | |
| print("Ошибка: Не удалось распарсить документ.") | |
| return 1 | |
| # Преобразуем документ в словарь | |
| document_dict = parsed_document.to_dict() | |
| # Сохраняем результат в JSON | |
| with open(output_file, "w", encoding="utf-8") as f: | |
| json.dump(document_dict, f, ensure_ascii=False, indent=2) | |
| print(f"Результат сохранен в файле: {output_file}") | |
| return 0 | |
| if __name__ == "__main__": | |
| time_start = datetime.now() | |
| for i in range(3): | |
| main() | |
| time_end = datetime.now() | |
| print(f"Время выполнения: {(time_end - time_start).total_seconds()} секунд") | |