目的

掌握python对PPT的的简单操作

实现

代码

  from tkinter import filedialog
  import win32com
  import win32com.client
  import os
  import tkinter
  
  
  def init_powerpoint():
      #app = comtypes.client.CreateObject("PowerPoint.Application")  # 使用office的接口电脑要安装有office的软件
      app = win32com.client.Dispatch("PowerPoint.Application")  # 使用office的接口电脑要安装有office的软件
      # powerpoint1 = comtypes.client.CreateObject("Kwpp.Application")  # 使用wps的接口运行比较流畅。电脑要安装有wps的软件
      app.Visible = True
      return app
  
  
  def ppt_to_pdf(inputFileName, outputFileName):
      app = init_powerpoint()
      if outputFileName[-3:] != 'pdf':
          outputFileName = outputFileName[0:-4] + ".pdf"
      deck = app.Presentations.Open(inputFileName)
      deck.SaveAs(outputFileName, 32)  # 32   for ppt to pdf
      # deck.SaveAs(inputFileName.rsplit('.')[0] + '.jpg', 17)
      deck.Close()
      app.Quit()
  
  
  def ppt_to_jpg(inputFileName):
      app = init_powerpoint()
      deck = app.Presentations.Open(inputFileName)
      deck.SaveAs(inputFileName.rsplit('.')[0] + '.jpg', 17)
      deck.Close()
      app.Quit()
  
  
  def convert_jpg_in_folder(folder):
      files = os.listdir(folder)
      print(files)  # 输出可以了解程序的运行过程。
      pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))]
      print(pptfiles)
      for pptfile in pptfiles:
          fullpath = os.path.join(os.path.abspath(folder), pptfile)
          print(fullpath)
          ppt_to_jpg(fullpath)
  
  
  def convert_pdf_in_folder(folder):
      files = os.listdir(folder)
      pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))]
      for pptfile in pptfiles:
          fullpath = os.path.join(os.path.abspath(folder), pptfile)
          print(fullpath)
          ppt_to_pdf(fullpath, fullpath)
  
  
  def openfile():
      global dirpath
      dirpath = e_path.get()
      if dirpath == "":
          dirpath = filedialog.askdirectory()
      else:
          dirpath = filedialog.askdirectory(initialdir=dirpath)
      e_path.delete(0, tkinter.END)
      e_path.insert(0, dirpath)
  
  
  def openfile2():
      global dirpath1
      dirpath1 = e_path2.get()
      if dirpath1 == "":
          dirpath1 = filedialog.askdirectory()
      else:
          dirpath1 = filedialog.askdirectory(initialdir=dirpath1)
      e_path2.delete(0, tkinter.END)
      e_path2.insert(0, dirpath1)
  
  
  if __name__ == "__main__":
      dirpath = ""
      dirpath1 = ""
      # 1 新建窗口
      root = tkinter.Tk()
      # 2 给窗口起名字
      root.title("PPT文件转换器")
      # 3 设定窗口大小
      root.geometry("800x600")
      # 锁定大小
      root.resizable(False, True)
      # 4 frame 整体框架设计
      frame_Top = tkinter.Frame(root)
      frame_Top.pack(
          side=tkinter.TOP,
          fill=tkinter.X,
  
      )
      frame_Middle = tkinter.Frame(root)
      frame_Middle.pack(
          side=tkinter.TOP,
          fill=tkinter.X,
      )
  
      # 添加Label
      l_path = tkinter.Label(
          frame_Top,
          text="请选择路径:",
          font=("Arial", 15),
      )
      l_path.pack(
          side=tkinter.LEFT,
          padx=10,
          pady=10,
      )
  
      # 添加Entry,当前工作路径
      e_path = tkinter.Entry(frame_Top, )
      e_path.pack(
          side=tkinter.LEFT,
          expand=tkinter.YES,
          fill=tkinter.X,
          pady=10,
      )
  
      # 添加按钮,选择工作目录
      b_select_path = tkinter.Button(
          frame_Top,
          text="浏览",
          command=lambda: openfile(),
          font=("Arial", 12),
      )
      b_select_path.pack(
          side=tkinter.LEFT,
          padx=10,
          pady=10,
      )
      # 添加按钮,选择工作目录
      b_coverToPdf_path = tkinter.Button(
          frame_Top,
          text="转换为PDF",
          command=lambda: convert_pdf_in_folder(dirpath),
          font=("Arial", 12),
      )
      b_coverToPdf_path.pack(
          side=tkinter.LEFT,
          padx=10,
          pady=10,
      )
  
      # 添加Label
      l_path2 = tkinter.Label(
          frame_Middle,
          text="请选择路径:",
          font=("Arial", 15),
      )
      l_path2.pack(
          side=tkinter.LEFT,
          padx=10,
          pady=10,
      )
  
      # 添加Entry,当前工作路径
      e_path2 = tkinter.Entry(frame_Middle, )
      e_path2.pack(
          side=tkinter.LEFT,
          expand=tkinter.YES,
          fill=tkinter.X,
          pady=10,
      )
  
      # 添加按钮,选择工作目录
      b_select_path2 = tkinter.Button(
          frame_Middle,
          text="浏览",
          command=lambda: openfile2(),
          font=("Arial", 12),
      )
      b_select_path2.pack(
          side=tkinter.LEFT,
          padx=10,
          pady=10,
      )
      # 添加按钮,选择工作目录
      b_coverToPdf_path2 = tkinter.Button(
          frame_Middle,
          text="转换为JPG",
          command=lambda: convert_jpg_in_folder(dirpath1),
          font=("Arial", 12),
      )
      b_coverToPdf_path2.pack(
          side=tkinter.LEFT,
          padx=10,
          pady=10,
      )
      # 主窗口循环
      root.mainloop()