Python3操作ppt

import win32com
from win32com.client import Dispatch, constants
import os
path=r'C:\Users\mzj\Desktop\ppt'
def ppt_text(p):
    ppt = win32com.client.Dispatch('PowerPoint.Application')
    ppt.Visible = 1
    pptSel = ppt.Presentations.Open(p)
    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(get_filename_ext(p)[0]+os.sep+get_filename_ext(p)[1]+'.txt', "a+",encoding='utf-8')as f:
                    f.write(s + "\n")
    ppt.Quit()
def get_filename_ext(filename):
    file_path, temp_file_name = os.path.split(filename)
    shot_name, extension = os.path.splitext(temp_file_name)
    return file_path, shot_name, extension
if __name__ == '__main__':
    for i in os.listdir(path):
        if 'ppt' in i:
            ppt_text(os.path.join(path,i))

 

你可能感兴趣的:(python)