{"record":{"id":"448dbeb7105a270d","repo":"binary-husky/gpt_academic","slug":"libreoffice-sudo-apt-get-install-libreoffice","errorCode":null,"errorMessage":"请先安装LibreOffice: sudo apt-get install libreoffice","messagePattern":"请先安装LibreOffice: sudo apt-get install libreoffice","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"crazy_functions/doc_fns/conversation_doc/word2pdf.py","lineNumber":42,"sourceCode":"        异常:\n            如果转换失败，将抛出相应异常\n        \"\"\"\n        try:\n            # 确保输入路径是Path对象\n            word_path = Path(word_path)\n\n            # 如果未指定pdf_path，则使用与word文档相同的名称\n            if pdf_path is None:\n                pdf_path = word_path.with_suffix('.pdf')\n            else:\n                pdf_path = Path(pdf_path)\n\n            # 检查操作系统\n            if platform.system() == 'Linux':\n                # Linux系统需要安装libreoffice\n                which_result = subprocess.run(['which', 'libreoffice'], capture_output=True, text=True)\n                if which_result.returncode != 0:\n                    raise RuntimeError(\"请先安装LibreOffice: sudo apt-get install libreoffice\")\n\n                print(f\"开始转换Word文档: {word_path} 到 PDF\")\n\n                # 使用subprocess代替os.system\n                result = subprocess.run(\n                    ['libreoffice', '--headless', '--convert-to', 'pdf:writer_pdf_Export',\n                     str(word_path), '--outdir', str(pdf_path.parent)],\n                    capture_output=True, text=True\n                )\n\n                if result.returncode != 0:\n                    error_msg = result.stderr or \"未知错误\"\n                    print(f\"LibreOffice转换失败，错误信息: {error_msg}\")\n                    raise RuntimeError(f\"LibreOffice转换失败: {error_msg}\")\n\n                print(f\"LibreOffice转换输出: {result.stdout}\")\n\n                # 如果输出路径与默认生成的不同，则重命名","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/crazy_functions/doc_fns/conversation_doc/word2pdf.py#L24-L60","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Install it as the message says: sudo apt-get install libreoffice (or libreoffice-writer for a smaller footprint)","In Docker: RUN apt-get update && apt-get install -y libreoffice-writer","If already installed but not found, ensure the binary dir is on PATH (which libreoffice) or symlink /usr/bin/libreoffice to soffice","On non-Debian systems use the equivalent: dnf install libreoffice / apk add libreoffice / brew install --cask libreoffice"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import shutil, platform\n\ndef can_convert_on_this_os() -> bool:\n    if platform.system() == 'Linux':\n        return shutil.which('libreoffice') is not None\n    return True  # docx2pdf branch (needs MS Word instead)","typeGuard":null,"tryCatchPattern":"try:\n    pdf = WordToPdfConverter.convert_to_pdf(docx)\nexcept (RuntimeError, Exception) as e:\n    if 'LibreOffice' in str(e):\n        raise SystemExit('Install first: sudo apt-get install libreoffice') from e\n    raise","preventionTips":["Bake libreoffice-writer into Docker/CI images (apt-get install -y libreoffice-writer)","Check shutil.which('libreoffice') at app startup on Linux and fail with instructions","Remember the platform split: Windows/macOS need MS Word, Linux needs LibreOffice"],"tags":["libreoffice","system-dependency","linux","docker","pdf-conversion"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}