Spaces:
Sleeping
Sleeping
#!/usr/bin/env python3 | |
""" | |
Скрипт для тестирования парсера PDF документов | |
и сохранения результатов в JSON формате. | |
Положите ваш файл test.pdf в директорию test_input и запустите скрипт. | |
Результат будет сохранен в файл test_output/test_pdf.json | |
""" | |
import json | |
import logging | |
import sys | |
from datetime import datetime | |
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.pdf | |
2. Парсит его с помощью UniversalParser | |
3. Сохраняет результат в test_output/test_pdf.json | |
""" | |
# Получаем абсолютные пути к файлам относительно корневой директории проекта | |
project_root = Path(__file__).parent.parent | |
input_file = project_root / "test_input" / "test.pdf" | |
output_file = project_root / "test_output" / "test_pdf.json" | |
# Проверяем существование входного файла | |
if not input_file.exists(): | |
print( | |
f"Ошибка: Файл {input_file} не найден. Пожалуйста, поместите файл test.pdf в директорию test_input." | |
) | |
return 1 | |
# Создаем директорию для выходных файлов, если она не существует | |
output_file.parent.mkdir(parents=True, exist_ok=True) | |
print(f"Парсинг PDF-файла: {input_file}") | |
# Создаем экземпляр универсального парсера | |
parser = UniversalParser() | |
# Парсим документ | |
parsed_document = parser.parse_by_path(str(input_file)) | |
if parsed_document is None: | |
print("Ошибка: Не удалось распарсить PDF-документ.") | |
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(1): | |
main() | |
time_end = datetime.now() | |
print(f"Время выполнения: {(time_end - time_start).total_seconds()} секунд") |