Files
deerflow-code/offline-backend-20260512/backend/scripts/office_runtime_selfcheck.py
T

79 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Offline runtime smoke test for the LocalSandbox Office toolchain."""
from __future__ import annotations
import shutil
import subprocess
import tempfile
from pathlib import Path
def require_command(name: str) -> str:
path = shutil.which(name)
if path is None:
raise RuntimeError(f"missing command: {name}")
return path
def assert_contains(text: str, expected: str, label: str) -> None:
normalized = text.replace("\\_", "_")
if expected not in normalized:
raise RuntimeError(f"{label} conversion lost test text: {text[:300]!r}")
def main() -> int:
from docx import Document
from markitdown import MarkItDown
from openpyxl import Workbook
from pptx import Presentation
soffice = require_command("soffice")
require_command("pandoc")
require_command("pdftotext")
require_command("file")
subprocess.run(
[soffice, "--headless", "--version"],
check=True,
capture_output=True,
text=True,
timeout=30,
)
with tempfile.TemporaryDirectory(prefix="deerflow-office-selfcheck-") as temporary:
root = Path(temporary)
word_path = root / "word-test.docx"
document = Document()
document.add_heading("Word 自检", level=1)
document.add_paragraph("DEERFLOW_WORD_OK")
document.save(word_path)
excel_path = root / "excel-test.xlsx"
workbook = Workbook()
worksheet = workbook.active
worksheet.title = "自检"
worksheet.append(["状态", "DEERFLOW_EXCEL_OK"])
workbook.save(excel_path)
ppt_path = root / "ppt-test.pptx"
presentation = Presentation()
slide = presentation.slides.add_slide(presentation.slide_layouts[5])
slide.shapes.title.text = "DEERFLOW_PPT_OK"
presentation.save(ppt_path)
converter = MarkItDown()
assert_contains(converter.convert(word_path).text_content, "DEERFLOW_WORD_OK", "Word")
assert_contains(converter.convert(excel_path).text_content, "DEERFLOW_EXCEL_OK", "Excel")
assert_contains(converter.convert(ppt_path).text_content, "DEERFLOW_PPT_OK", "PowerPoint")
print("OFFICE_RUNTIME_SELF_CHECK=PASS")
print("支持:DOC/DOCX、XLS/XLSX、PPT/PPTX、ODT/ODS/ODP、PDF")
print("组件:MarkItDown、python-docx、openpyxl、python-pptx、LibreOffice、Pandoc、中文字体")
return 0
if __name__ == "__main__":
raise SystemExit(main())