如何用Python输出PPT中的文字信息
在这里,会用到win32com模块
模块下载地址:http://sourceforge.net/projects/pywin32/files/pywin32/
代码如下:
import win32com from win32com.client import Dispatch, constants ppt = win32com.client.Dispatch('PowerPoint.Application') ppt.Visible = 1 pptSel = ppt.Presentations.Open(r"C:\Users\Victor\Desktop\1.ppt") win32com.client.gencache.EnsureDispatch('PowerPoint.Application') f = file(r"C:\Users\Victor\Desktop\1.txt","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()
PS:
1> 在打开文件的时候,刚开始写的是pptSel = ppt.Presentations.Open("C:\Users\Victor\Desktop\1.ppt"),报如下错误:
>>> pptSel=ppt.Presentations.Open("C:\Users\Victor\Desktop\1.ppt") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<COMObject <unknown>>", line 3, in Open pywintypes.com_error: (-2147352567, '\xb7\xa2\xc9\xfa\xd2\xe2\xcd\xe2\xa1\xa3', (0, None, None, None, 0, -2147024773), None)
错误的主要原因为路径中的反斜杠无法自动识别。
2> 该程序段来自 http://www.sharejs.com/codes/python/8145