如何用Python输出PPT中的文字信息

本文详细介绍了如何利用Python的win32com模块读取PPT文件,并将其中的文字内容提取出来保存为文本文件。解决路径中反斜杠无法识别的问题,并提供了一个完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里,会用到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

 

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值