当前位置:  首页>> 技术小册>> Python与办公-玩转PDF

针对将PPT文档转换成PDF文档还是使用win32com,思路也是一样的,打开一个文档然后保存为一个PDF文档,保存的时候PDF的格式编号是32。

再强调一下,在上文的Excel转PDF的代码中,我们想要在运行代码的时候不显示程序窗口,可以将应用对象的Visible属性设置为False。但这种方式在PowerPoint这里是行不通的,想要隐藏窗口的话可以在Open()文档时候指定参数WithWindow为False,代码如下:

  1. import os.path
  2. from win32com.client import DispatchEx
  3. app = DispatchEx("PowerPoint.Application")
  4. app.DisplayAlerts = 0
  5. abs_path = os.path.abspath(_ _ file_ _)
  6. base_dir = os.path.dirname(abs_path)
  7. file_path = os.path.join(base_dir,"convert_ files","转换测试文
  8. 档.pptx")
  9. save_path = os.path.join(base_dir,"convert_ files","test.pdf")
  10. ppt = app.Presentations.Open( file_path,WithWindow=False)
  11. ppt.SaveAs(save_path,32)
  12. ppt.Close()
  13. app.Quit()