提取PPT中的⽂字(包括图⽚中的⽂字)python是⼀门很强⼤的语⾔,因为有着丰富的第三⽅库,所以可以说Python是⽆所不能的。
很多⼈都知道,Python可以操作Excel,PDF·还有PPT,这篇⽂章就围绕Python提取PPT中的⽂字来写,包括提取PPT中的艺术字,图⽚中的⽂字。
因为实现环境是linux,所以⽆法⽤win32com来实现这个需求,使⽤extract库也可以提取PDF,PPT等⽂件中的⽂字,但这⾥不⽤extract来实现,⽤python-pptx,如果熟悉extract库⼀点的也知
道,extract中也使⽤了python-pptx,实现过程也是调⽤了python-pptx。
presentation = pptx.Presentation(fp)
results = []
for slide in presentation.slides:
for shape in slide.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
part = []
for run in paragraph.runs:
part.append(run.text)
results.append(''.join(part))
elif isinstance(shape, Picture):
content = self.parsepic.request_api(shape.image.blob)
results.append(''.join(content))
results = [line for line in results if line.strip()]
代码分析:presentation = pptx.Presentation(fp)
实例化ppt对象,只有实例化Presentation对象才能操作ppt,通过此对象可以访问其他类的接⼝。