在日常工作中我们经常需要把 PPT 转成图片用于博客、报告或者分享。Python 提供了一个非常方便的方式通过win32com自动化PowerPoint实现批量导出幻灯片。一、安装依赖pip install pywin32注意该方法仅在 Windows 系统上有效并且需要安装 MicrosoftPowerPoint二、完整代码importosfrompathlibimportPathimportpythoncomfromwin32com.clientimportDispatchdefppt_to_images(ppt_path,output_dirNone,image_formatPNG,width1920,height1080): 将 PPT 每一页导出为图片 参数: ppt_path: PPT 文件路径 output_dir: 输出文件夹默认在 PPT 同目录下创建同名文件夹 image_format: 图片格式可选 PNG / JPG width: 导出宽度 height: 导出高度 ppt_pathPath(ppt_path)ifnotppt_path.exists():raiseFileNotFoundError(f找不到文件:{ppt_path})ifoutput_dirisNone:output_dirppt_path.parent/f{ppt_path.stem}_imageselse:output_dirPath(output_dir)output_dir.mkdir(parentsTrue,exist_okTrue)pythoncom.CoInitialize()powerpointNonepresentationNonetry:powerpointDispatch(PowerPoint.Application)powerpoint.Visible1presentationpowerpoint.Presentations.Open(str(ppt_path),WithWindowFalse)# 导出全部幻灯片presentation.Export(str(output_dir),image_format,width,height)print(f转换完成图片已保存到:{output_dir})exceptExceptionase:print(f转换失败:{e})finally:ifpresentation:presentation.Close()ifpowerpoint:powerpoint.Quit()pythoncom.CoUninitialize()if__name____main__:ppt_filerD:\目标文件.pptppt_to_images(ppt_file)三、使用方法修改ppt_file为你的 PPT 路径。运行脚本会在同目录下生成PPT文件名_images文件夹里面是每一页的图片。3. 可通过参数调整图片格式和分辨率ppt_to_images(ppt_file, image_formatJPG, width2560, height1440)Windows 用户可以用 withWindowTrue 看到转换过程调试更方便。如果导出的图片有白边可以考虑在 PPT 里调整幻灯片尺寸