kbzhishiku
This commit is contained in:
1 parent
7f542f54a5
commit
08a19ebe96
68 files changed
+16602
-229
No files matched your search
@@ -0,0 +1,83 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from edkb_system.contracts import Context
|
||||
from edkb_system.skills.word_bridge import convert_with_word
|
||||
|
||||
|
||||
def _find_soffice() -> str | None:
|
||||
candidates = [
|
||||
os.environ.get("EDKB_SOFFICE"),
|
||||
r"C:\Program Files\LibreOffice\program\soffice.exe",
|
||||
r"C:\Program Files (x86)\LibreOffice\program\soffice.exe",
|
||||
"soffice",
|
||||
]
|
||||
for c in candidates:
|
||||
if not c:
|
||||
continue
|
||||
if c == "soffice":
|
||||
return c
|
||||
if Path(c).exists():
|
||||
return c
|
||||
return None
|
||||
|
||||
|
||||
def _convert_with_word(path: Path, out_dir: Path) -> Path:
|
||||
output = out_dir / f"{path.stem}.pdf"
|
||||
return convert_with_word(path.resolve(), output.resolve(), 17)
|
||||
|
||||
|
||||
def _convert_with_soffice(path: Path, out_dir: Path, soffice: str) -> Path:
|
||||
output = out_dir / f"{path.stem}.pdf"
|
||||
cmd = [
|
||||
soffice,
|
||||
"--headless",
|
||||
"--convert-to",
|
||||
"pdf",
|
||||
str(path),
|
||||
"--outdir",
|
||||
str(out_dir),
|
||||
]
|
||||
proc = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if proc.returncode != 0:
|
||||
stderr = (proc.stderr or "").strip() or "no stderr"
|
||||
raise RuntimeError(
|
||||
f"soffice conversion failed (code={proc.returncode}): {stderr}"
|
||||
)
|
||||
if not output.exists():
|
||||
raise RuntimeError(f"soffice conversion produced no output: {output}")
|
||||
return output
|
||||
|
||||
|
||||
def run(input_data: dict, context: Context) -> dict:
|
||||
file_path = Path(input_data["file_path"])
|
||||
work_dir = Path(input_data["work_dir"])
|
||||
out_dir = work_dir / "flow_pdf"
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
errors: list[str] = []
|
||||
if os.name == "nt":
|
||||
try:
|
||||
pdf = _convert_with_word(file_path, out_dir)
|
||||
return {"pdf_path": str(pdf), "converter": "word"}
|
||||
except Exception as exc:
|
||||
errors.append(f"word: {exc}")
|
||||
|
||||
soffice = _find_soffice()
|
||||
if soffice:
|
||||
try:
|
||||
pdf = _convert_with_soffice(file_path, out_dir, soffice)
|
||||
return {"pdf_path": str(pdf), "converter": "soffice"}
|
||||
except Exception as exc:
|
||||
errors.append(f"soffice: {exc}")
|
||||
else:
|
||||
errors.append("soffice: not found")
|
||||
|
||||
context.warnings.append(
|
||||
"doc->pdf conversion unavailable; fallback to docx text parser"
|
||||
)
|
||||
context.warnings.append("doc->pdf details: " + "; ".join(errors))
|
||||
return {"pdf_path": None, "converter": None}
|
||||
Reference in new issue
Block a user