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 on Linux when 'which libreoffice' returns non-zero, i.e. the LibreOffice binary is not on PATH. On Linux the converter shells out to 'libreoffice --headless --convert-to pdf' instead of using docx2pdf (which only works on Windows/macOS with MS Word installed).
Source
Thrown at crazy_functions/paper_fns/file2file_doc/word2pdf.py:40
异常:
如果转换失败,将抛出相应异常
"""
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
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 it: sudo apt-get install libreoffice (or the lighter libreoffice-writer package)
- In Docker: add 'RUN apt-get update && apt-get install -y libreoffice' to the image
- If installed but not found, ensure the soffice/libreoffice symlink is on PATH (e.g. /usr/bin)
- Alternatively run the workload on Windows/macOS where docx2pdf with MS Word is used instead
Example fix
# before: container without libreoffice FROM python:3.11-slim # after FROM python:3.11-slim RUN apt-get update && apt-get install -y --no-install-recommends libreoffice-writer && rm -rf /var/lib/apt/lists/*
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
if not can_convert_on_linux():
raise SystemExit("Install LibreOffice first: sudo apt-get install libreoffice") Try / catch
try:
WordToPdfConverter.convert_to_pdf(docx, pdf)
except (RuntimeError, Exception) as e:
if "LibreOffice" in str(e):
print("Skipping PDF export: install libreoffice to enable it")
else:
raise Prevention
- Check shutil.which('libreoffice') at app startup on Linux and surface a clear setup message
- Bake libreoffice-writer into Docker/CI images that do PDF conversion
- Gate PDF-export features on the dependency being present
When it happens
Trigger: Running convert_to_pdf on a Linux host (container, CI, server) where libreoffice is not installed or not on PATH; a minimal Docker image (python:slim) without LibreOffice.
Common situations: Deploying to a fresh Linux server or Docker container; CI pipelines building PDF conversion; libreoffice installed as a snap with a non-standard PATH.
Related errors
- 请先安装LibreOffice: sudo apt-get install libreoffice
- 请先安装LibreOffice: sudo apt-get install libreoffice
- LibreOffice转换失败: {error_msg}
- PDF生成失败或文件为空
- 转换PDF失败: {str(e)}
AI-assisted analysis of binary-husky/gpt_academic@d6bde0fa54 (2026-08-14).
Data as JSON: /api/errors/eac8a120866b8e78.
Report an issue: GitHub.