1. 程式人生 > >Python3操作ppt

Python3操作ppt

import win32com
from win32com.client import Dispatch, constants

ppt = win32com.client.Dispatch('PowerPoint.Application')
ppt.Visible = 1
pptSel = ppt.Presentations.Open(r"C:\Users\mzj\Desktop\ppt\14 雲群BP 11月26.pptx")
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
    print(shape_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
            with open(r"C:\Users\mzj\Desktop\ppt\14 雲群BP 11月26.txt", "a+",encoding='utf-8')as f:
                f.write(s + "\n")
ppt.Quit()