binary-husky/gpt_academic · error · RuntimeError

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

Error message

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

What it means

WordToPdfConverter.convert_to_pdf raises this RuntimeError when running on Linux and `which libreoffice` returns a non-zero exit code, i.e. the libreoffice executable is not on PATH. The Linux branch of the converter shells out to `libreoffice --headless --convert-to pdf`, so the binary is a hard runtime dependency on Linux (Windows/macOS use docx2pdf instead).

Source

Thrown at crazy_functions/review_fns/conversation_doc/word2pdf.py:49

            生成的PDF文件路径

        异常:
            如果转换失败,将抛出相应异常
        """
        try:
            word_path = Path(word_path)

            if pdf_path is None:
                # 创建新的pdf路径,同时替换文件名中的docx
                pdf_path = WordToPdfConverter._replace_docx_in_filename(word_path).with_suffix('.pdf')
            else:
                pdf_path = WordToPdfConverter._replace_docx_in_filename(Path(pdf_path))

            # 检查操作系统
            if platform.system() == 'Linux':
                # Linux系统需要安装libreoffice
                if not os.system('which libreoffice') == 0:
                    raise RuntimeError("请先安装LibreOffice: sudo apt-get install libreoffice")

                # 使用libreoffice进行转换
                os.system(f'libreoffice --headless --convert-to pdf "{word_path}" --outdir "{pdf_path.parent}"')

                # 如果输出路径与默认生成的不同,则重命名
                default_pdf = word_path.with_suffix('.pdf')
                if default_pdf != pdf_path:
                    os.rename(default_pdf, pdf_path)
            else:
                # Windows和MacOS使用 docx2pdf
                convert(word_path, pdf_path)

            return str(pdf_path)

        except Exception as e:
            raise Exception(f"转换PDF失败: {str(e)}")

    @staticmethod

View on GitHub (pinned to d6bde0fa54)

Solutions

  1. Install LibreOffice on the host: sudo apt-get install libreoffice (or the lighter libreoffice-writer) and verify with `which libreoffice`.
  2. If only `soffice` is available, symlink it: sudo ln -s $(which soffice) /usr/local/bin/libreoffice.
  3. For Docker images, add `RUN apt-get update && apt-get install -y libreoffice` to the Dockerfile.
  4. If the process runs with a restricted PATH (service/cron), set PATH to include /usr/bin or patch the check to use shutil.which('libreoffice').

Example fix

// before (environment): libreoffice missing on Linux
sudo apt-get install libreoffice

# after (code, more robust check):
import shutil
if platform.system() == 'Linux':
    if shutil.which('libreoffice') is None:
        raise RuntimeError("请先安装LibreOffice: sudo apt-get install libreoffice")
Defensive patterns

Strategy: validation

Validate before calling

import shutil, platform

def can_convert_on_linux() -> bool:
    return platform.system() != 'Linux' or shutil.which('libreoffice') is not None

# before calling WordToPdfConverter.convert_to_pdf:
if not can_convert_on_linux():
    raise SystemError('libreoffice missing — run: sudo apt-get install libreoffice')

Try / catch

try:
    WordToPdfConverter.convert_to_pdf(word_path, pdf_path)
except RuntimeError as e:
    if 'LibreOffice' in str(e):
        logger.error('dependency missing: install libreoffice on this host')
    raise

Prevention

When it happens

Trigger: Calling WordToPdfConverter.convert_to_pdf(word_path) (directly or via batch_convert / convert from markdown content) on any Linux host where libreoffice is not installed, is installed under a different name (e.g. only `soffice` exists), or PATH does not include /usr/bin in the process environment (common in systemd services, Docker, cron).

Common situations: Minimal Docker/CI images (python:slim) that never had LibreOffice installed; headless servers where the user only installed `libreoffice-writer-nogui` under the `soffice` symlink; deployment environments where the app runs with a sanitized PATH so `which libreoffice` fails even though the package exists.

Related errors


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