首页 > 编程语言 >python-pptx将多个ppt文件按照给定模板ppt格式整合

python-pptx将多个ppt文件按照给定模板ppt格式整合

时间:2024-09-04 16:06:29浏览次数:4  
标签:pptx name python text frame slide shape ppt image

from pptx import Presentation

from pptx.enum.shapes import MSO_SHAPE_TYPE

pr = Presentation('周彤.pptx')

for slide in pr.slides:

for shape in slide.shapes:

if shape.shape_type==MSO_SHAPE_TYPE.TEXT_BOX:

print(shape.text_frame.font.name,end='\n\n')

#shape.has_text_frame 判断是否为文本框形状

from pptx import Presentation
from pptx.util import Inches, Cm
from pptx.enum.shapes import MSO_SHAPE_TYPE
import io
from pptx.dml.color import RGBColor
from pptx.util import Pt

import re

def load_template(ppt_template_path):
""" 加载模板 PPT """
prs = Presentation(ppt_template_path)
return prs

def copy_slide_layout(prs, idx):
""" 复制模板中的幻灯片布局 """
slide_layout = prs.slide_layouts[idx]
return slide_layout

def create_slide_from_layout(prs, slide_layout, content, images):
""" 使用布局创建新幻灯片,填充内容和图片,并添加姓名文本框 """
slide = prs.slides.add_slide(slide_layout)
if 'title' in content and 'body' in content:
title = slide.shapes.title
body = slide.placeholders[1] if len(slide.placeholders) > 1 else None
if title:
title.text = content['title']
if body:
tf = body.text_frame
tf.text = content['body']

# 添加图片
left = top = Inches(1)
for image in images:
    slide.shapes.add_picture(image, left, top, width=Inches(2))
    left += Inches(2.5)  # 调整下一张图片的位置

# 移除姓名文本框的添加,因为这个功能在 adjust_text_format_and_position 中已经实现

return slide

def merge_ppts(template_path, single_page_ppts, output_path):
""" 合并多个单页 PPT 到模板中,包括图片,并输出结果 """
prs = load_template(template_path)

# 获取第一个布局作为标准格式
standard_layout = prs.slide_layouts[0]

for ppt in single_page_ppts:
    single_prs = Presentation(ppt)
    if len(single_prs.slides) > 0:
        content = {'title': '', 'body': '', 'name': ''}
        images = []
        slide = single_prs.slides[0]
        
        title_shape = slide.shapes.title
        if title_shape:
            content['title'] = title_shape.text
            # 假设标题就是姓名
            content['name'] = title_shape.text
        
        for shape in slide.shapes:
            if shape.has_text_frame:
                content['body'] += shape.text_frame.text + "\n"
            elif shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
                # 提取图片
                image = shape.image
                image_bytes = image.blob
                images.append(io.BytesIO(image_bytes))
        
        # 使用标准布局创建新幻灯片,包括图片
        new_slide = create_slide_from_layout(prs, standard_layout, content, images)
        
        # 调整文本框格式和位置
        extracted_name = adjust_text_format_and_position(new_slide)
        
        # 调整图片位置和大小
        image_shape = adjust_image_position_and_size(new_slide)
        
        # 如果有图片,再次调整名字文本框的位置
        if image_shape:
            adjust_name_position(new_slide, image_shape, extracted_name)

prs.save(output_path)
print(f"合并的幻灯片(包括图片)已保存到 {output_path}")

def adjust_text_format_and_position(slide):
""" 调整幻灯片文本框格式和位置,并提取姓名 """
max_text_shape = None
max_text_length = 0
extracted_name = "" # 默认为空字符串

for shape in slide.shapes:
    if shape.has_text_frame:
        text_length = len(shape.text_frame.text)
        if text_length > max_text_length:
            max_text_length = text_length
            max_text_shape = shape

if max_text_shape:
    # 尝试从文本中提取姓名
    text = max_text_shape.text_frame.text
    name_match = re.search(r'([\u4e00-\u9fa5]{2,4})', text)
    if name_match:
        extracted_name = name_match.group(1)

    # 设置最大文本框的格式和位置
    max_text_shape.width = Cm(23.26)
    max_text_shape.height = Cm(13.57)
    max_text_shape.left = Cm(9.81)
    max_text_shape.top = Cm(4.31)

    # 设置文本格式
    text_frame = max_text_shape.text_frame
    for paragraph in text_frame.paragraphs:
        paragraph.alignment = 1  # 1 表示左对齐
        for run in paragraph.runs:
            run.font.name = '微软雅黑'
            run.font.size = Pt(16)
            run.font.color.rgb = RGBColor(255, 255, 255)  # 白色

return extracted_name

def adjust_name_position(slide, image_shape, extracted_name):
""" 调整姓名文本框的位置和样式 """
name_box = None
for shape in slide.shapes:
if shape.has_text_frame and shape.text_frame.text == extracted_name:
name_box = shape
break

if not name_box and extracted_name:
    name_box = slide.shapes.add_textbox(Cm(2.04), Cm(12.61), Cm(5), Cm(1))

if name_box and extracted_name:
    # 使用 round() 函数将浮点数转换为整数
    name_box.left = round(image_shape.left + (image_shape.width - name_box.width) / 2)
    name_box.top = round(image_shape.top + image_shape.height + Cm(0.5))

    name_frame = name_box.text_frame
    name_frame.text = extracted_name
    name_frame.paragraphs[0].alignment = 1  # 居中对齐
    name_frame.paragraphs[0].font.name = '微软雅黑'
    name_frame.paragraphs[0].font.size = Pt(16)
    name_frame.paragraphs[0].font.bold = True
    name_frame.paragraphs[0].font.color.rgb = RGBColor(255, 255, 255)

    if len(extracted_name) == 2:
        name_frame.paragraphs[0].runs[0].font.char_spacing = Pt(16)

def adjust_image_position_and_size(slide):
""" 调整图片位置和大小 """
image_shape = None
for shape in slide.shapes:
if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
shape.left = Cm(3.23)
shape.top = Cm(4.46)
shape.width = Cm(5.54)
shape.height = Cm(7.79)
image_shape = shape
break # 只调整第一张图片
return image_shape

示例调用

template_path = '1.pptx'
single_page_ppts = ['simple.pptx', 'simple1.pptx']
output_path = 'merged_output.pptx'
merge_ppts(template_path, single_page_ppts, output_path)

标签:pptx,name,python,text,frame,slide,shape,ppt,image
From: https://www.cnblogs.com/yuanyongsheng/p/18396707

相关文章

  • python 注释符
    python注释符4、注释1、单行注释单行注释用于解释代码中的一行或一小段代码。在Python中,单行注释以#开头,后面的内容都是注释。单行注释可以放在代码的任何位置,但通常放在代码行的上方或旁边。单行注释不会影响代码的执行,Python解释器会忽略它们。单行注释是临时性的......
  • python操作kafka
    一、参考阿里云的官方链接:        使用PythonSDK接入Kafka收发消息_云消息队列Kafka版(Kafka)-阿里云帮助中心二、安装python环境  三、添加python依赖库pipinstallconfluent-kafka==1.9.2四、新建一个setting.py文件配置信息kafka_setting={'sas......
  • python利用深度学习(Keras)进行癫痫分类
    一、癫痫介绍        癫痫,即俗称“羊癫风”,是由多种病因引起的慢性脑功能障碍综合症,是仅次于脑血管病的第二大脑部疾病。癫痫发作的直接原因是脑部神经元反复地突发性过度放电所导致的间歇性中枢神经系统功能失调。临床上常表现为突然意识丧失、全身抽搐以及精神异常......
  • Python深度学习~生成车牌
    1.定义车牌数据所需字符        车牌中包括省份简称、大写英文字母和数字,我们首先定义需要的字符和字典,方便后面使用index={"京":0,"沪":1,"津":2,"渝":3,"冀":4,"晋":5,"蒙":6,"辽":7,"吉":8,"黑":9,&qu......
  • Python全网最全基础课程笔记(三)——所有运算符+运算符优先级
    本专栏系列为Pythong基础系列,每天都会更新新的内容,搜罗全网资源以及自己在学习和工作过程中的一些总结,可以说是非常详细和全面。以至于为什么要写的这么详细:自己也是学过Python的,很多新手只是简单的过一篇语法,其实对于一个知识点的底层逻辑和其他使用方法以及参数详情根本......
  • Python全网最全基础课程笔记(二)——变量
      本专栏系列为Pythong基础系列,每天都会更新新的内容,搜罗全网资源以及自己在学习和工作过程中的一些总结,可以说是非常详细和全面。以至于为什么要写的这么详细:自己也是学过Python的,很多新手只是简单的过一篇语法,其实对于一个知识点的底层逻辑和其他使用方法以及参数详情......
  • python-小理帮老师改错
    题目描述老师给小理发了一封电子邮件,任务如下。写一个程序,给你 n 个数,输出 X。X=num1^p1​​+num2^p2​​+⋯+numn^pn​​num1​,num2​,⋯⋯,numn​ 都是整数,p1​,p2​,⋯⋯,pn​ 都是一位数。但是出现了一些玄学错误,使得 X 变成了:X=q1​+q2​+...+qn​注:qi​=numi​......
  • python-甲流病人初筛
    题目描述目前正是甲流盛行时期,为了更好地进行分流治疗,医院在挂号时要求对病人的体温和咳嗽情况进行检查,对于体温超过 37.5 度(含等于 37.5 度)并且咳嗽的病人初步判定为甲流病人(初筛)。现需要统计某天前来挂号就诊的病人中有多少人被初筛为甲流病人。输入第一行是某天前来挂......
  • 乌鲁木齐市中考分数线预测系统:基于统信UOS的Python Flask Web应用
    系统概述“乌鲁木齐市中考分数线预测系统”是一个专为乌鲁木齐市初中生及其家长设计的Web应用系统,旨在通过历史数据和统计分析方法,提供中考分数线的预测服务。该系统基于统信UOS操作系统,采用Python的Flask框架开发,确保了系统的高效性和安全性。系统架构系统采用典型的B/S(浏......
  • Python程序:递归实现阶乘函数的优化与代码解读
    一、引言阶乘(Factorial)在数学和计算机科学中是一个常见的概念,它表示一个正整数的所有正整数的乘积。阶乘的定义如下:n!=n×(n−1)×(n−2)×…×1其中,0!定义为1。本文将以递归方式实现阶乘函数,并对代码进行优化与解释。二、原始代码首先来看一个简单的递归实现阶乘的P......