ppt2text.py 824 B

12345678910111213141516171819202122232425262728
  1. import win32com
  2. from win32com.client import Dispatch
  3. def ppt2text(filepath):
  4. '''support ppt and pptx'''
  5. PPT = win32com.client.Dispatch('PowerPoint.Application')
  6. PPT.Visible = 1
  7. res = []
  8. pptSel = PPT.Presentations.Open(filepath)
  9. win32com.client.gencache.EnsureDispatch('PowerPoint.Application')
  10. slide_count = pptSel.Slides.Count
  11. for i in range(1, slide_count + 1):
  12. shape_count = pptSel.Slides(i).Shapes.Count
  13. for j in range(1, shape_count + 1):
  14. if pptSel.Slides(i).Shapes(j).HasTextFrame:
  15. s = pptSel.Slides(i).Shapes(j).TextFrame.TextRange.Text
  16. res.append(s)
  17. PPT.Quit()
  18. return "\n".join(res)
  19. if __name__ == "__main__":
  20. # filepath = r'D:\yx\a.ppt'
  21. filepath = r'D:\yx\upload\2.pptx'
  22. print(ppt2text(filepath))