12345678910111213141516171819202122232425262728 |
- import win32com
- from win32com.client import Dispatch
- def ppt2text(filepath):
- '''support ppt and pptx'''
- PPT = win32com.client.Dispatch('PowerPoint.Application')
- PPT.Visible = 1
- res = []
- pptSel = PPT.Presentations.Open(filepath)
- win32com.client.gencache.EnsureDispatch('PowerPoint.Application')
- slide_count = pptSel.Slides.Count
- for i in range(1, slide_count + 1):
- shape_count = pptSel.Slides(i).Shapes.Count
- for j in range(1, shape_count + 1):
- if pptSel.Slides(i).Shapes(j).HasTextFrame:
- s = pptSel.Slides(i).Shapes(j).TextFrame.TextRange.Text
- res.append(s)
- PPT.Quit()
- return "\n".join(res)
- if __name__ == "__main__":
- # filepath = r'D:\yx\a.ppt'
- filepath = r'D:\yx\upload\2.pptx'
- print(ppt2text(filepath))
|