1. 程式人生 > >python批量處理ppt

python批量處理ppt

#coding:utf-8
import win32com
from win32com.client import Dispatch, constants
import time
import os

def main():
    path = "C:\\Users\\Administrator\\Desktop\\ppt\\"
    dirs = os.listdir(path)
    for i in dirs:
        try:
            run(i)
        except:
            print i

def run(name):
    
    ppt = win32com.client.Dispatch('PowerPoint.Application')
    ppt.Visible = 1
    pptSel = ppt.Presentations.Open("C:\\Users\\Administrator\\Desktop\\ppt\\%s"%name)
    win32com.client.gencache.EnsureDispatch('PowerPoint.Application')
    f = file("./txt/%s.txt"%name,"w")
    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
                f.write(s.encode('utf-8') + "\n")    
    f.close()
    ppt.Quit()

main()