binary-husky/gpt_academic · error · RuntimeError

请先安装LibreOffice: sudo apt-get install libreoffice

Error message

请先安装LibreOffice: sudo apt-get install libreoffice

What it means

Raised as RuntimeError by WordToPdfConverter.convert_to_pdf on Linux when `which libreoffice` returns a non-zero exit code, i.e. the libreoffice executable is not installed or not on PATH. This is a hard precondition check before attempting any conversion — the Linux code path shells out to libreoffice --headless instead of using a Python library.

Source

Thrown at crazy_functions/doc_fns/conversation_doc/word2pdf.py:42

        异常:
            如果转换失败,将抛出相应异常
        """
        try:
            # 确保输入路径是Path对象
            word_path = Path(word_path)

            # 如果未指定pdf_path,则使用与word文档相同的名称
            if pdf_path is None:
                pdf_path = word_path.with_suffix('.pdf')
            else:
                pdf_path = Path(pdf_path)

            # 检查操作系统
            if platform.system() == 'Linux':
                # Linux系统需要安装libreoffice
                which_result = subprocess.run(['which', 'libreoffice'], capture_output=True, text=True)
                if which_result.returncode != 0:
                    raise RuntimeError("请先安装LibreOffice: sudo apt-get install libreoffice")

                print(f"开始转换Word文档: {word_path} 到 PDF")

                # 使用subprocess代替os.system
                result = subprocess.run(
                    ['libreoffice', '--headless', '--convert-to', 'pdf:writer_pdf_Export',
                     str(word_path), '--outdir', str(pdf_path.parent)],
                    capture_output=True, text=True
                )

                if result.returncode != 0:
                    error_msg = result.stderr or "未知错误"
                    print(f"LibreOffice转换失败,错误信息: {error_msg}")
                    raise RuntimeError(f"LibreOffice转换失败: {error_msg}")

                print(f"LibreOffice转换输出: {result.stdout}")

                # 如果输出路径与默认生成的不同,则重命名

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Install it as the message says: sudo apt-get install libreoffice (or libreoffice-writer for a smaller footprint)
  2. In Docker: RUN apt-get update && apt-get install -y libreoffice-writer
  3. If already installed but not found, ensure the binary dir is on PATH (which libreoffice) or symlink /usr/bin/libreoffice to soffice
  4. On non-Debian systems use the equivalent: dnf install libreoffice / apk add libreoffice / brew install --cask libreoffice
Defensive patterns

Strategy: validation

Validate before calling

import shutil, platform

def can_convert_on_this_os() -> bool:
    if platform.system() == 'Linux':
        return shutil.which('libreoffice') is not None
    return True  # docx2pdf branch (needs MS Word instead)

Try / catch

try:
    pdf = WordToPdfConverter.convert_to_pdf(docx)
except (RuntimeError, Exception) as e:
    if 'LibreOffice' in str(e):
        raise SystemExit('Install first: sudo apt-get install libreoffice') from e
    raise

Prevention

When it happens

Trigger: Calling convert_to_pdf() on Linux/Docker/WSL where LibreOffice was never installed, or where soffice exists under a different name/path not in PATH.

Common situations: Minimal Docker images (python:*-slim) that ship without libreoffice; CI runners; headless servers. macOS/Windows users never see this because they take the docx2pdf branch.

Related errors


AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14). Data as JSON: /api/errors/448dbeb7105a270d. Report an issue: GitHub.