{"record":{"id":"eac8a120866b8e78","repo":"binary-husky/gpt_academic","slug":"libreoffice-sudo-apt-get-install-libreoffice-eac8a1","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/paper_fns/file2file_doc/word2pdf.py","lineNumber":40,"sourceCode":"\n        异常:\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                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":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/binary-husky/gpt_academic/blob/d6bde0fa54373309bd05823a49bda8da019d2c77/crazy_functions/paper_fns/file2file_doc/word2pdf.py#L22-L58","documentation":"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).","triggerScenarios":"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.","commonSituations":"Deploying to a fresh Linux server or Docker container; CI pipelines building PDF conversion; libreoffice installed as a snap with a non-standard PATH.","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"],"exampleFix":"# before: container without libreoffice\nFROM python:3.11-slim\n\n# after\nFROM python:3.11-slim\nRUN apt-get update && apt-get install -y --no-install-recommends libreoffice-writer && rm -rf /var/lib/apt/lists/*","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\nif not can_convert_on_linux():\n    raise SystemExit(\"Install LibreOffice first: sudo apt-get install libreoffice\")","typeGuard":null,"tryCatchPattern":"try:\n    WordToPdfConverter.convert_to_pdf(docx, pdf)\nexcept (RuntimeError, Exception) as e:\n    if \"LibreOffice\" in str(e):\n        print(\"Skipping PDF export: install libreoffice to enable it\")\n    else:\n        raise","preventionTips":["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"],"tags":["environment","libreoffice","linux","pdf-conversion","missing-dependency"],"backgroundTag":null,"analyzedSha":"d6bde0fa54373309bd05823a49bda8da019d2c77","analyzedAt":"2026-08-14T22:48:35.038Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}