You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Linux系统下用Python将PPTX文件转换为PDF(无需依赖Windows)

在Linux上用Python将PPTX转PDF的免费方案

方案1:结合python-pptx与LibreOffice命令行(推荐)

该方案依赖开源免费的LibreOffice(Linux系统普遍支持安装),完全不涉及Windows或微软相关服务,是兼顾易用性和兼容性的最优选择:

  1. 安装依赖:

    # Debian/Ubuntu系列系统
    sudo apt install libreoffice-core
    # RHEL/CentOS系列系统
    sudo dnf install libreoffice-headless
    # 安装Python依赖包
    pip install python-pptx
    
  2. Python转换代码:

    import subprocess
    import os
    
    def pptx_to_pdf(input_pptx, output_pdf):
        # 调用LibreOffice无界面模式执行转换
        convert_cmd = [
            "libreoffice",
            "--headless",
            "--convert-to",
            "pdf",
            "--outdir",
            os.path.dirname(output_pdf),
            input_pptx
        ]
        try:
            subprocess.run(convert_cmd, check=True, capture_output=True)
            # 重命名转换后的文件(默认与原PPTX同名)
            default_pdf = os.path.join(os.path.dirname(output_pdf), f"{os.path.splitext(os.path.basename(input_pptx))[0]}.pdf")
            if default_pdf != output_pdf:
                os.rename(default_pdf, output_pdf)
            print("PPTX转PDF成功")
        except subprocess.CalledProcessError as e:
            print(f"转换失败:{e.stderr.decode()}")
    
    # 调用示例
    pptx_to_pdf("demo.pptx", "demo_output.pdf")
    

方案2:纯Python渲染(仅适用于简单PPTX)

如果完全不想依赖任何外部工具,可以通过python-pptx提取幻灯片内容,再用reportlab手动渲染成PDF。但该方式仅能处理基础文本、图片,复杂布局、动画、特殊字体等会丢失样式:

  1. 安装依赖:

    pip install python-pptx reportlab pillow
    
  2. 基础转换代码:

    from pptx import Presentation
    from reportlab.pdfgen import canvas
    from reportlab.lib.pagesizes import letter
    from PIL import Image
    import io
    
    def simple_pptx_to_pdf(input_pptx, output_pdf):
        prs = Presentation(input_pptx)
        pdf_canvas = canvas.Canvas(output_pdf, pagesize=letter)
        page_width, page_height = letter
    
        for slide in prs.slides:
            # 处理文本内容
            for shape in slide.shapes:
                if hasattr(shape, "text") and shape.text:
                    # 简单定位,实际需根据PPTX坐标调整
                    pdf_canvas.drawString(50, page_height - 100, shape.text)
            # 处理图片内容
            for shape in slide.shapes:
                if shape.shape_type == 13:  # 识别图片类型
                    img_bytes = shape.image.blob
                    img = Image.open(io.BytesIO(img_bytes))
                    # 按页面比例缩放图片
                    scale = min((page_width-100)/img.width, (page_height-200)/img.height)
                    pdf_canvas.drawImage(io.BytesIO(img_bytes), 50, page_height - 200, width=img.width*scale, height=img.height*scale)
            pdf_canvas.showPage()  # 新建PDF页
    
        pdf_canvas.save()
        print("简单PPTX转换完成")
    
    # 调用示例
    simple_pptx_to_pdf("simple_demo.pptx", "simple_output.pdf")
    

内容的提问来源于stack exchange,提问作者Lydia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 22:42:40