赞
踩
1、下图是ppt文件的基本结构:
2、获取ppt文件的整体流程:
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
return JsonResponse(data=all_ppt,safe = False) # 输出json
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。