import os import comtypes.client def ppt_to_pdf_single(ppt_path, pdf_path=None): """ 单个 PPT 转 PDF :param ppt_path: PPT 文件路径 :param pdf_path: PDF 输出路径 (可选,默认同名) """ ppt_path = os.path.abspath(ppt_path) # COM 接口必须使用绝对路径 if pdf_path is None: pdf_path = os.path.splitext(ppt_path)[0] + ".pdf" pdf_path = os.path.abspath(pdf_path) if not os.path.exists(ppt_path): print(f"文件不存在: {ppt_path}") return False powerpoint = None try: # 启动 PowerPoint 应用 powerpoint = comtypes.client.CreateObject("PowerPoint.Application") powerpoint.Visible = 1 # 设为可见,否则某些版本会报错 # 打开演示文稿 deck = powerpoint.Presentations.Open(ppt_path) # 保存为 PDF (文件格式代码 32 代表 PDF) deck.SaveAs(pdf_path, 32) deck.Close() print(f"转换成功: {pdf_path}") return True except Exception as e: print(f"转换失败: {str(e)}") return False finally: if powerpoint: powerpoint.Quit()