zoukankan      html  css  js  c++  java
  • [python]提取PPT中的文字(包括图片中的文字)

    python是一门很强大的语言,因为有着丰富的第三方库,所以可以说Python是无所不能的。

    很多人都知道,Python可以操作Excel,PDF·还有PPT,这篇文章就围绕Python提取PPT中的文字来写,包括提取PPT中的艺术字,图片中的文字。

    因为实现环境是linux,所以无法用win32com来实现这个需求,使用extract库也可以提取PDF,PPT等文件中的文字,但这里不用extract来实现,用python-pptx,如果熟悉extract库一点的也知道,extract中也使用了python-pptx,实现过程也是调用了python-pptx。

    1.  
      presentation = pptx.Presentation(fp)
    2.  
      results = []
    3.  
      for slide in presentation.slides:
    4.  
      for shape in slide.shapes:
    5.  
      if shape.has_text_frame:
    6.  
      for paragraph in shape.text_frame.paragraphs:
    7.  
      part = []
    8.  
      for run in paragraph.runs:
    9.  
      part.append(run.text)
    10.  
      results.append(''.join(part))
    11.  
      elif isinstance(shape, Picture):
    12.  
      content = self.parsepic.request_api(shape.image.blob)
    13.  
      results.append(''.join(content))
    14.  
      results = [line for line in results if line.strip()]

    代码分析:

    presentation = pptx.Presentation(fp)

    实例化ppt对象,只有实例化Presentation对象才能操作ppt,通过此对象可以访问其他类的接口。

    通过presentation访问幻灯片presentation.slides,slides是由每一个幻灯片对象组成的列表。

    通过幻灯片对象slide访问包含在此幻灯片上的形状对象shape,如文本框,表格,图片等

    if shape.has_text_frame

    判断形状对象是否为文本对象

    1.  
      for paragraph in shape.text_frame.paragraphs:
    2.  
      part = []
    3.  
      for run in paragraph.runs:
    4.  
      part.append(run.text)
    5.  
      results.append(''.join(part))

    获取文本框的文本,文本框中也会出现多个段落内容

    1.  
      elif isinstance(shape, Picture):
    2.  
      content = self.parsepic.request_api(shape.image.blob)
    3.  
      results.append(''.join(content))

    判断是否为图片对象或者艺术字对象,这里是调用百度图片提取文字的接口来提取PPT中包含在图片中的文字。

    1.  
      def request_api(self, img_data):
    2.  
      url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
    3.  
      # 高进度版
    4.  
      # url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic'
    5.  
      postdata = {
    6.  
      'language_type': 'CHN_ENG', # 中英文混合
    7.  
      'detect_direction': 'true',
    8.  
      'image': base64.b64encode(img_data)
    9.  
      }
    10.  
      headers = {
    11.  
      'Content-Type': 'application/x-www-form-urlencoded',
    12.  
      'User-Agent': random.choice(USER_AGENT['pc'])
    13.  
      }
    14.  
      r = requests.post(url, params={'access_token': self.get_token()}, data=postdata, headers=headers).json()
    15.  
      if r.get('error_code'):
    16.  
      raise Exception(r['error_msg'])
    17.  
      return [item['words'] for item in r['words_result']]

    需要注册一个账号,并且开通图片提取文字的服务,获取APPKEY, APPSECRETKEY,从而获取到token,具体接口详情看百度开发者文档。

    python-pptx官方文档:https://python-pptx.readthedocs.io/en/latest/api/slides.html#pptx.slide.Slides

  • 相关阅读:
    PHP xml_get_current_column_number() 函数
    PHP xml_get_current_byte_index() 函数
    PHP xml_error_string() 函数
    PHP utf8_encode() 函数
    PHP utf8_decode() 函数
    MySQL 资料库概论与MySQL 安装
    SEO之网站关键词的优化 :首页,内页关键字,长尾关键字
    前端开发chrome console的使用 :评估表达式 – Break易站
    chrome console的使用 : 异常和错误的处理 – Break易站
    SEO的基本概念 和 提交SITEMAP到搜索引擎
  • 原文地址:https://www.cnblogs.com/taosiyu/p/14115862.html
Copyright © 2011-2022 走看看