{"record":{"id":"6234b5e325de932f","repo":"binary-husky/gpt_academic","slug":"libreoffice-sudo-apt-get-install-libreoffice-6234b5","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/review_fns/conversation_doc/word2pdf.py","lineNumber":49,"sourceCode":"            生成的PDF文件路径\n\n        异常:\n            如果转换失败，将抛出相应异常\n        \"\"\"\n        try:\n            word_path = Path(word_path)\n\n            if pdf_path is None:\n                # 创建新的pdf路径，同时替换文件名中的docx\n                pdf_path = WordToPdfConverter._replace_docx_in_filename(word_path).with_suffix('.pdf')\n            else:\n                pdf_path = WordToPdfConverter._replace_docx_in_filename(Path(pdf_path))\n\n            # 检查操作系统\n            if platform.system() == 'Linux':\n                # Linux系统需要安装libreoffice\n                if not os.system('which libreoffice') == 0:\n                    raise RuntimeError(\"请先安装LibreOffice: sudo apt-get install libreoffice\")\n\n                # 使用libreoffice进行转换\n                os.system(f'libreoffice --headless --convert-to pdf \"{word_path}\" --outdir \"{pdf_path.parent}\"')\n\n                # 如果输出路径与默认生成的不同，则重命名\n                default_pdf = word_path.with_suffix('.pdf')\n                if default_pdf != pdf_path:\n                    os.rename(default_pdf, pdf_path)\n            else:\n                # Windows和MacOS使用 docx2pdf\n                convert(word_path, pdf_path)\n\n            return str(pdf_path)\n\n        except Exception as e:\n            raise Exception(f\"转换PDF失败: {str(e)}\")\n\n    @staticmethod","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/crazy_functions/review_fns/conversation_doc/word2pdf.py#L31-L67","documentation":"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).","triggerScenarios":"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).","commonSituations":"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.","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')."],"exampleFix":"// before (environment): libreoffice missing on Linux\nsudo apt-get install libreoffice\n\n# after (code, more robust check):\nimport shutil\nif platform.system() == 'Linux':\n    if shutil.which('libreoffice') is None:\n        raise RuntimeError(\"请先安装LibreOffice: sudo apt-get install libreoffice\")","handlingStrategy":"validation","validationCode":"import shutil, platform\n\ndef can_convert_on_linux() -> bool:\n    return platform.system() != 'Linux' or shutil.which('libreoffice') is not None\n\n# before calling WordToPdfConverter.convert_to_pdf:\nif not can_convert_on_linux():\n    raise SystemError('libreoffice missing — run: sudo apt-get install libreoffice')","typeGuard":null,"tryCatchPattern":"try:\n    WordToPdfConverter.convert_to_pdf(word_path, pdf_path)\nexcept RuntimeError as e:\n    if 'LibreOffice' in str(e):\n        logger.error('dependency missing: install libreoffice on this host')\n    raise","preventionTips":["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."],"tags":["linux","libreoffice","pdf-conversion","environment","dependency"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}