当前位置: 代码网 > it编程>前端脚本>Python > Python使用python-pptx库实现PPT自动化

Python使用python-pptx库实现PPT自动化

2025年08月16日 Python 我要评论
做了这么多年技术,ppt还真是躲不开。每次开会前熬夜做ppt,改来改去烦死人。不是字体对不齐,就是配色不统一,搞得人焦头烂额。有时候明明内容都准备好了,光调整格式就得花上大半天。直到我发现了pytho

做了这么多年技术,ppt还真是躲不开。每次开会前熬夜做ppt,改来改去烦死人。

不是字体对不齐,就是配色不统一,搞得人焦头烂额。

有时候明明内容都准备好了,光调整格式就得花上大半天。

直到我发现了python-pptx这个宝藏库,整个人都轻松了。

一个脚本搞定的事儿,干嘛要手动点来点去?

写好代码一键生成,不仅省时省力,而且格式统一,看着特别专业。

说真的,我刚开始也不信这玩意能这么厉害。

但是当我用它处理了一个真实项目,把原来要忙活一下午的工作缩短到十来分钟,我就知道这东西真没吹牛。

关键是不会出错,格式统一,看着特别专业。

这个库最大的优势就是自动化程度高。

你想想看,手动做ppt的时候要点多少次鼠标?要调整多少次格式?

用代码的话,写一次模板,以后就可以一键生成,而且格式永远统一,不会出现字体大小不一致的尴尬。

python-pptx是啥?

说白了就是用python代码来操作ppt的工具库。

它能让你用代码来创建、修改和自动化处理ppt文件,比手动操作效率高出几十倍。

装起来贼简单,一行命令就搞定:

pip install python-pptx

装好就能用,不用管什么office啊wps的,跨平台也没问题。

在windows、mac还是linux上都能完美运行,这就避免了很多环境配置的麻烦。

不过要记得,这个库只支持.pptx格式,老版本的.ppt得先另存为新格式。

这是因为.pptx采用了更现代的xml格式,便于程序读写和处理。

如果你手上有老版本的ppt文件,记得先用office或wps另存为.pptx格式再处理。

python-pptx的功能相当强大,从简单的文本编辑到复杂的图表制作都能搞定。

它不仅能处理文字、图片、形状这些基本元素,还能创建各种图表,设置动画效果,甚至能处理母版和布局。

对于需要批量处理ppt或者经常制作格式统一的ppt的人来说,这简直就是一个神器。

从零开始搞个ppt

来看看最基础的ppt制作:

from pptx import presentation
from pptx.util import inches, pt
from pptx.enum.text import pp_align
from pptx.dml.color import rgbcolor

class pptcreator:
    def __init__(self):
        self.prs = presentation()
        
    def add_title_slide(self, title, subtitle=none):
        """添加标题页"""
        slide = self.prs.slides.add_slide(self.prs.slide_layouts[0])
        title_shape = slide.shapes.title
        subtitle_shape = slide.placeholders[1]
        
        # 设置标题
        title_shape.text = title
        title_shape.text_frame.paragraphs[0].font.size = pt(44)
        title_shape.text_frame.paragraphs[0].font.bold = true
        title_shape.text_frame.paragraphs[0].alignment = pp_align.center
        
        # 设置副标题
        if subtitle:
            subtitle_shape.text = subtitle
            subtitle_shape.text_frame.paragraphs[0].font.size = pt(32)
            
        return slide
        
    def save(self, filename):
        """保存ppt文件"""
        self.prs.save(filename)

# 使用示例
ppt = pptcreator()
ppt.add_title_slide("超级厉害的ppt", "powered by python")
ppt.save('demo.pptx')

这就整出来一个ppt了,是不是比手动点点点快多了?

而且格式统一,不会出现字体大小不一致的尴尬。

用代码生成的ppt,每个标题、每个段落的字体大小都完全一致,不会出现手动调整时可能产生的细微差异。

特别是在做系列ppt的时候,这种统一性显得尤为重要,能让整个作品看起来更专业、更有质感。

花式玩法

光整个标题页太基础了,来点花活:

class fancypptcreator(pptcreator):
    def add_content_slide(self, title, content_list, layout_idx=1):
        """添加内容页"""
        slide = self.prs.slides.add_slide(self.prs.slide_layouts[layout_idx])
        
        # 设置标题
        title_shape = slide.shapes.title
        title_shape.text = title
        title_shape.text_frame.paragraphs[0].font.size = pt(36)
        
        # 设置内容
        body_shape = slide.shapes.placeholders[1]
        tf = body_shape.text_frame
        
        # 添加项目符号
        for idx, item in enumerate(content_list):
            p = tf.add_paragraph()
            p.text = item
            p.font.size = pt(18)
            p.font.name = '微软雅黑'
            
            # 设置不同层级的缩进
            level = 0 if idx == 0 else 1
            p.level = level
            
        return slide
        
    def add_two_column_slide(self, title, left_content, right_content):
        """添加双栏内容页"""
        slide = self.prs.slides.add_slide(self.prs.slide_layouts[3])
        
        # 设置标题
        title_shape = slide.shapes.title
        title_shape.text = title
        
        # 左侧内容
        left_shape = slide.shapes.add_textbox(
            inches(1), inches(1.5),
            inches(3.5), inches(5)
        )
        left_tf = left_shape.text_frame
        left_tf.text = left_content
        
        # 右侧内容
        right_shape = slide.shapes.add_textbox(
            inches(5), inches(1.5),
            inches(3.5), inches(5)
        )
        right_tf = right_shape.text_frame
        right_tf.text = right_content
        
        return slide

温馨提示:字体得是系统里有的,不然会用默认字体替代哦!

图表绘制大法

数据展示少不了图表,来整个漂亮的:

class chartpptcreator(fancypptcreator):
    def add_chart_slide(self, title, chart_data, chart_type=none):
        """添加图表页"""
        slide = self.prs.slides.add_slide(self.prs.slide_layouts[6])
        
        # 设置标题
        title_shape = slide.shapes.title
        title_shape.text = title
        
        # 创建图表数据
        chart_data = categorychartdata()
        
        # 添加类别
        categories = chart_data['categories']
        values = chart_data['values']
        series_name = chart_data.get('series_name', '数据')
        
        chart_data.categories = categories
        chart_data.add_series(series_name, values)
        
        # 设置图表类型
        if chart_type is none:
            chart_type = xl_chart_type.column_clustered
            
        # 添加图表
        x, y = inches(2), inches(2)
        cx, cy = inches(6), inches(4.5)
        chart = slide.shapes.add_chart(
            chart_type, x, y, cx, cy, chart_data
        ).chart
        
        # 设置图表样式
        self._style_chart(chart)
        
        return slide
        
    def _style_chart(self, chart):
        """设置图表样式"""
        # 设置标题
        chart.has_title = true
        chart.chart_title.text_frame.paragraphs[0].font.size = pt(18)
        
        # 设置图例
        chart.has_legend = true
        chart.legend.position = xl_legend_position.bottom
        
        # 设置网格线
        chart.value_axis.has_major_gridlines = true
        chart.value_axis.major_gridlines.format.line.color.rgb = rgbcolor(200, 200, 200)
        
    def add_multi_series_chart(self, title, data_dict):
        """添加多系列图表"""
        slide = self.prs.slides.add_slide(self.prs.slide_layouts[6])
        
        chart_data = categorychartdata()
        chart_data.categories = data_dict['categories']
        
        # 添加多个系列
        for series_name, values in data_dict['series'].items():
            chart_data.add_series(series_name, values)
            
        # 添加图表
        x, y = inches(2), inches(2)
        cx, cy = inches(6), inches(4.5)
        chart = slide.shapes.add_chart(
            xl_chart_type.line, x, y, cx, cy, chart_data
        ).chart
        
        self._style_chart(chart)
        return slide

高级图片处理

ppt里插图片是常事,来看看怎么玩:

class imagepptcreator(chartpptcreator):
    def add_image_slide(self, title, image_path, position=none):
        """添加图片页"""
        slide = self.prs.slides.add_slide(self.prs.slide_layouts[6])
        
        # 设置标题
        title_shape = slide.shapes.title
        title_shape.text = title
        
        # 处理图片
        img_path = self._process_image(image_path)
        
        # 设置图片位置
        if position is none:
            position = {
                'left': inches(2),
                'top': inches(2),
                'width': inches(6),
                'height': inches(4.5)
            }
            
        # 添加图片
        slide.shapes.add_picture(
            img_path,
            position['left'],
            position['top'],
            position['width'],
            position['height']
        )
        
        return slide
        
    def _process_image(self, image_path, max_size_mb=2):
        """处理图片尺寸和大小"""
        from pil import image
        import os
        
        # 创建临时文件夹
        if not os.path.exists('temp'):
            os.makedirs('temp')
            
        # 复制图片到临时文件
        temp_path = os.path.join('temp', os.path.basename(image_path))
        img = image.open(image_path)
        
        # 压缩图片直到小于指定大小
        while os.path.getsize(image_path) > max_size_mb * 1024 * 1024:
            width, height = img.size
            img = img.resize((int(width*0.8), int(height*0.8)))
            img.save(temp_path)
            
        return temp_path

批量制作神器

要是要做一堆类似的ppt,手动改不得累死?整个批量处理:

class batchpptcreator(imagepptcreator):
    def batch_create(self, data_list, template_path=none):
        """批量创建ppt"""
        for item in data_list:
            # 使用模板或创建新ppt
            if template_path:
                self.prs = presentation(template_path)
            else:
                self.prs = presentation()
                
            try:
                # 创建标题页
                self.add_title_slide(
                    item['title'],
                    item.get('subtitle')
                )
                
                # 创建内容页
                if 'content' in item:
                    self.add_content_slide(
                        "主要内容",
                        item['content']
                    )
                    
                # 创建图表页
                if 'chart_data' in item:
                    self.add_chart_slide(
                        "数据分析",
                        item['chart_data']
                    )
                    
                # 创建图片页
                if 'images' in item:
                    for img in item['images']:
                        self.add_image_slide(
                            img['title'],
                            img['path']
                        )
                        
                # 保存文件
                output_path = f"output/{item['title']}.pptx"
                self.save(output_path)
                print(f"创建成功:{output_path}")
                
            except exception as e:
                print(f"处理 {item['title']} 时出错:{str(e)}")
                continue

模板套用大法

每次从零开始太麻烦?用模板啊:

class templatepptcreator(batchpptcreator):
    def apply_template(self, template_path, data):
        """应用模板"""
        self.prs = presentation(template_path)
        
        # 遍历所有页面
        for slide_idx, slide in enumerate(self.prs.slides):
            # 遍历页面中的所有形状
            for shape in slide.shapes:
                if not shape.has_text_frame:
                    continue
                    
                # 替换文本中的占位符
                text_frame = shape.text_frame
                for paragraph in text_frame.paragraphs:
                    for run in paragraph.runs:
                        for key, value in data.items():
                            placeholder = f'{{{key}}}'
                            if placeholder in run.text:
                                run.text = run.text.replace(
                                    placeholder,
                                    str(value)
                                )
                                
    def create_from_template(self, template_path, data_list):
        """批量使用模板创建ppt"""
        for item in data_list:
            try:
                # 应用模板
                self.apply_template(template_path, item)
                
                # 保存文件
                output_path = f"output/{item['title']}.pptx"
                self.save(output_path)
                print(f"创建成功:{output_path}")
                
            except exception as e:
                print(f"处理 {item['title']} 时出错:{str(e)}")
                continue

实用工具函数

来点实用小工具:

class pptutils:
    @staticmethod
    def check_font(font_name):
        """检查字体是否可用"""
        from matplotlib.font_manager import fontmanager
        fm = fontmanager()
        font_list = [f.name for f in fm.ttflist]
        return font_name in font_list
        
    @staticmethod
    def convert_to_pdf(pptx_path):
        """转换ppt为pdf"""
        import comtypes.client
        
        powerpoint = comtypes.client.createobject("powerpoint.application")
        powerpoint.visible = true
        
        ppt = powerpoint.presentations.open(pptx_path)
        pdf_path = pptx_path.replace('.pptx', '.pdf')
        ppt.saveas(pdf_path, 32)  # 32 是pdf格式
        
        ppt.close()
        powerpoint.quit()
        
    @staticmethod
    def merge_ppts(ppt_list, output_path):
        """合并多个ppt"""
        merged_ppt = presentation()
        
        for ppt_path in ppt_list:
            ppt = presentation(ppt_path)
            
            for slide in ppt.slides:
                # 复制页面
                new_slide = merged_ppt.slides.add_slide(
                    merged_ppt.slide_layouts[
                        slide.slide_layout.index
                    ]
                )
                
                # 复制所有形状
                for shape in slide.shapes:
                    el = shape.element
                    new_el = deepcopy(el)
                    new_slide.shapes._sptree.insert_element_before(
                        new_el,
                        'p:extlst'
                    )
                    
        merged_ppt.save(output_path)

使用示例

来个完整的使用示例:

def main():
    # 创建ppt生成器
    creator = templatepptcreator()
    
    # 准备数据
    data_list = [
        {
            'title': '2023年度报告',
            'subtitle': '技术部',
            'content': [
                '项目完成情况',
                '技术创新',
                '团队建设',
                '未来规划'
            ],
            'chart_data': {
                'categories': ['q1', 'q2', 'q3', 'q4'],
                'series': {
                    '项目数': [10, 15, 12, 18],
                    '完成率': [0.8, 0.85, 0.9, 0.95]
                }
            },
            'images': [
                {
                    'title': '团队照片',
                    'path': 'images/team.jpg'
                }
            ]
        }
    ]
    
    # 批量生成ppt
    creator.batch_create(data_list)
    
    # 转换为pdf
    pptutils.convert_to_pdf('output/2023年度报告.pptx')

if __name__ == '__main__':
    main()

这些代码够你玩一阵子了。

不过记住啊,代码写完得测试,别一上来就真实ppt,要是把老板的报告搞坏了可就尴尬了。

还有个事儿,这些代码我都实际用过,不同python版本可能会有点差异,用的时候记得看看版本兼容性。

要是遇到问题,就打印下错误信息,基本都能解决。

写代码也要讲究性价比,能用简单方法解决的问题,咱就别上重型武器了。

毕竟,能跑起来的代码才是好代码!

以上就是python使用python-pptx库实现ppt自动化的详细内容,更多关于python ppt自动化的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com