当前位置:   article > 正文

python-pptx使用_ppt文件中的paragraph.runs:

ppt文件中的paragraph.runs:

如何获取ppt内元素并返回给前端(采用的python的Django)

1、下图是ppt文件的基本结构:
在这里插入图片描述
2、获取ppt文件的整体流程:

  • 通过Prsentation(filePath);获取ppt对象
  • 遍历.slides,拿到一页ppt对象slide(slide_width,slide_height)
  • 遍历slide拿到内部的每个容器shape(shape.width、shape.height)
  • 遍历容器元素shape中的图片image和文本框text_frame
  • 通过shape.image.blob拿到图片对象,注意如果容器中没有图片对象会报错,所以推荐使用try-except写法
  • 通过shape.text_frame.paragraphs 拿到文本,建立使用.paragraph.runs 获取每个字,再通过字符串拼接的方式获取文本;直接使用paragraphs获取可能造成获取文本不全;
  • 最后以json格式输出给前端
import base64
from django.http import JsonResponse
from django.shortcuts import render
from pptx import Presentatio# Create your views here.
def pptShows(request):
    if request.method == 'GET':
        ppt_name = request.GET.get('ppt_name')
        ppt_url_start = 'static'   # 在settings中注册静态资源文件夹位置
        ppt_path = ppt_url_start + '/' + ppt_name + '.pptx'# ppt文件地址
        print(ppt_path)
        ppt = Presentation(ppt_path) # 初始化打开ppt
        all_ppt = {}  # 整个ppt文件
        all_ppt['ppt_width'] = ppt.slide_width # 获取ppt的长度
        all_ppt['ppt_height'] = ppt.slide_height # 获取ppt的宽度
        all_ppt['elements'] = []
        for slide in ppt.slides:
            element = []
            for shape in slide.shapes:
                shape_left = shape.left
                shape_top = shape.top
                shape_height = shape.height
                shape_width = shape.width
                text = ''
                # shape 为图片时  t
                # ry/except 因为非图片元素没有Image方法会弹出异常.

                try:
                    image_base64 = str(base64.b64encode(shape.image.blob), encoding='utf-8')
                    picture = {
                        'type': 'picture',
                        'content': 'image_base64',
                        'width': shape_width,
                        'height': shape_height,
                        'top': shape_top,
                        'left': shape_left
                    }
                    element.append(picture)
                except:
                    pass
                # 如果容器含有文本框
                if shape.has_text_frame:
                    for paragraph in shape.text_frame.paragraphs:
                        for run in paragraph.runs:
                            text += run.text
                    text = {
                        'type': 'text',
                        'content': text,
                        'width': shape_width,
                        'height': shape_height,
                        'top': shape_top,
                        'left': shape_left
                    }
                    element.append(text)

            all_ppt['elements'].append(element)
    return JsonResponse(data=all_ppt,safe = False) # 输出json

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
return JsonResponse(data=all_ppt,safe = False) # 输出json
  • 1




  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/1001731
推荐阅读
相关标签
  

闽ICP备14008679号