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)}")
@staticmethodView on GitHub (pinned to d6bde0fa54)
Solutions
- Install LibreOffice on the host: sudo apt-get install libreoffice (or the lighter libreoffice-writer) and verify with `which libreoffice`.
- If only `soffice` is available, symlink it: sudo ln -s $(which soffice) /usr/local/bin/libreoffice.
- For Docker images, add `RUN apt-get update && apt-get install -y libreoffice` to the Dockerfile.
- 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
- Bake libreoffice into the deployment image/Dockerfile rather than relying on the host.
- Use shutil.which('libreoffice') in a startup health check and fail fast with an actionable message.
- On Windows/macOS deployments, verify MS Word is installed for the docx2pdf path.
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
- 请先安装LibreOffice: sudo apt-get install libreoffice
- 请先安装LibreOffice: sudo apt-get install libreoffice
- LibreOffice转换失败: {error_msg}
- PDF生成失败或文件为空
- markitdown 库未安装,无法进行转换
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/6234b5e325de932f.
Report an issue: GitHub.