File size: 2,990 Bytes
86c402d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/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()} секунд")