2025-04-11 10:29:53 +02:00
|
|
|
import logging
|
2024-11-19 12:21:17 +01:00
|
|
|
from pathlib import Path
|
|
|
|
|
2025-04-11 10:29:53 +02:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from docling.backend.msexcel_backend import MsExcelDocumentBackend
|
2024-11-19 12:21:17 +01:00
|
|
|
from docling.datamodel.base_models import InputFormat
|
2025-04-11 10:29:53 +02:00
|
|
|
from docling.datamodel.document import ConversionResult, DoclingDocument, InputDocument
|
2024-11-19 12:21:17 +01:00
|
|
|
from docling.document_converter import DocumentConverter
|
|
|
|
|
2025-03-18 10:38:19 +01:00
|
|
|
from .test_data_gen_flag import GEN_TEST_DATA
|
2025-02-24 07:10:40 +00:00
|
|
|
from .verify_utils import verify_document, verify_export
|
2025-02-14 17:47:53 +01:00
|
|
|
|
2025-04-11 10:29:53 +02:00
|
|
|
_log = logging.getLogger(__name__)
|
|
|
|
|
2025-03-18 10:38:19 +01:00
|
|
|
GENERATE = GEN_TEST_DATA
|
2024-11-19 12:21:17 +01:00
|
|
|
|
|
|
|
|
2025-06-10 20:25:59 +05:30
|
|
|
def get_excel_paths():
|
2024-11-19 12:21:17 +01:00
|
|
|
# Define the directory you want to search
|
|
|
|
directory = Path("./tests/data/xlsx/")
|
|
|
|
|
2025-06-10 20:25:59 +05:30
|
|
|
# List all Excel files in the directory and its subdirectories
|
|
|
|
excel_files = sorted(directory.rglob("*.xlsx")) + sorted(directory.rglob("*.xlsm"))
|
|
|
|
return excel_files
|
2024-11-19 12:21:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_converter():
|
|
|
|
converter = DocumentConverter(allowed_formats=[InputFormat.XLSX])
|
|
|
|
|
|
|
|
return converter
|
|
|
|
|
|
|
|
|
2025-04-11 10:29:53 +02:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def documents() -> list[tuple[Path, DoclingDocument]]:
|
|
|
|
documents: list[dict[Path, DoclingDocument]] = []
|
2024-11-19 12:21:17 +01:00
|
|
|
|
2025-06-10 20:25:59 +05:30
|
|
|
excel_paths = get_excel_paths()
|
2024-11-19 12:21:17 +01:00
|
|
|
converter = get_converter()
|
|
|
|
|
2025-06-10 20:25:59 +05:30
|
|
|
for excel_path in excel_paths:
|
|
|
|
_log.debug(f"converting {excel_path}")
|
2024-11-19 12:21:17 +01:00
|
|
|
|
|
|
|
gt_path = (
|
2025-06-10 20:25:59 +05:30
|
|
|
excel_path.parent.parent / "groundtruth" / "docling_v2" / excel_path.name
|
2024-11-19 12:21:17 +01:00
|
|
|
)
|
|
|
|
|
2025-06-10 20:25:59 +05:30
|
|
|
conv_result: ConversionResult = converter.convert(excel_path)
|
2024-11-19 12:21:17 +01:00
|
|
|
|
|
|
|
doc: DoclingDocument = conv_result.document
|
|
|
|
|
2025-04-11 10:29:53 +02:00
|
|
|
assert doc, f"Failed to convert document from file {gt_path}"
|
|
|
|
documents.append((gt_path, doc))
|
|
|
|
|
|
|
|
return documents
|
|
|
|
|
|
|
|
|
2025-06-10 20:25:59 +05:30
|
|
|
def test_e2e_excel_conversions(documents) -> None:
|
2025-04-11 10:29:53 +02:00
|
|
|
for gt_path, doc in documents:
|
2024-11-19 12:21:17 +01:00
|
|
|
pred_md: str = doc.export_to_markdown()
|
|
|
|
assert verify_export(pred_md, str(gt_path) + ".md"), "export to md"
|
|
|
|
|
|
|
|
pred_itxt: str = doc._export_to_indented_text(
|
|
|
|
max_text_len=70, explicit_tables=False
|
|
|
|
)
|
2025-04-14 18:01:26 +02:00
|
|
|
assert verify_export(pred_itxt, str(gt_path) + ".itxt"), (
|
|
|
|
"export to indented-text"
|
|
|
|
)
|
2024-11-19 12:21:17 +01:00
|
|
|
|
2025-04-14 18:01:26 +02:00
|
|
|
assert verify_document(doc, str(gt_path) + ".json", GENERATE), (
|
|
|
|
"document document"
|
|
|
|
)
|
2025-04-11 10:29:53 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_pages(documents) -> None:
|
|
|
|
"""Test the page count and page size of converted documents.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
documents: The paths and converted documents.
|
|
|
|
"""
|
|
|
|
# number of pages from the backend method
|
2025-06-10 20:25:59 +05:30
|
|
|
path = next(item for item in get_excel_paths() if item.stem == "test-01")
|
2025-04-11 10:29:53 +02:00
|
|
|
in_doc = InputDocument(
|
|
|
|
path_or_stream=path,
|
|
|
|
format=InputFormat.XLSX,
|
|
|
|
filename=path.stem,
|
|
|
|
backend=MsExcelDocumentBackend,
|
|
|
|
)
|
|
|
|
backend = MsExcelDocumentBackend(in_doc=in_doc, path_or_stream=path)
|
|
|
|
assert backend.page_count() == 3
|
|
|
|
|
|
|
|
# number of pages from the converted document
|
2025-04-14 18:01:26 +02:00
|
|
|
doc = next(item for path, item in documents if path.stem == "test-01")
|
2025-04-11 10:29:53 +02:00
|
|
|
assert len(doc.pages) == 3
|
|
|
|
|
|
|
|
# page sizes as number of cells
|
|
|
|
assert doc.pages.get(1).size.as_tuple() == (3.0, 7.0)
|
|
|
|
assert doc.pages.get(2).size.as_tuple() == (9.0, 18.0)
|
|
|
|
assert doc.pages.get(3).size.as_tuple() == (13.0, 36.0)
|