python中使用PPTX库实现幻灯片里面表格对象的所有操作,单元格内容,字体大小,颜色,字体、填充背景颜色等等

本文介绍了如何使用Python的pptx库,自动化操作PowerPoint中的表格,根据内容改变样式,提升工作效率。通过示例展示了如何修改单元格内容、字体、颜色和布局,旨在提供模板和代码片段以便快速复制到实际项目中。
摘要由CSDN通过智能技术生成

前言

这是自己自行对python中的自动化PPT感兴趣,主要参考官方文档pptx库的官方文档,(要好好学英语)

解决的问题

我要将ppt中某一页的表格内容,随意根据需要调成我想要的样子,包括每个单元长什么样子

达到效果

以后我碰到需要自动化ppt里面的table对象,我直接过里啊拷贝代码,快速完成需要,完成工作

代码

from pptx import Presentation
from pptx.util import Inches
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Inches, Pt 
from pptx.enum.text import PP_ALIGN
from pptx.dml.color import RGBColor

import time

# 读入已存在的PPT文件
ppt = Presentation('../input/NIVEA+VOC月报_202112_jc.pptx')

#  第一页幻灯片
slide0 = ppt.slides[25]
# 获取第一页幻灯片的所有占位符
placeholder = slide0.shapes#.placeholders
# 在第二个占位符对象中添加新段落
new_paragraph = placeholder[5].text_frame.add_paragraph()
# 修改表格数据
for i in range(len(placeholder)):
    if placeholder[i].has_table:
        if "Positive" in placeholder[i].table.cell(0, 0).text:
            R,G,B = 0, 176, 48
            P_R,P_G,P_B = 217, 244, 245
        if "Negative" in placeholder[i].table.cell(0, 0).text:
            R,G,B = 237, 69, 87
            N_R,N_G,N_B =  250,212,232
        for row in range(len(placeholder[i].table.rows)):
            for col in range(len(placeholder[i].table.columns)):  
                if row == 0: 
                    placeholder[i].table.cell(row, col).text = "第一行"
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].font.size=Pt(11) 
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].font.name="微软雅黑"
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER  # 居中
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].font.blod = True
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].font.color.rgb = RGBColor(R, G, B)
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].font.bold = True  # 是否加粗
                    placeholder[i].table.cell(row, col).fill.solid()  #填充
                    placeholder[i].table.cell(row, col).fill.fore_color.rgb=RGBColor(255, 255, 255)  # 背景颜色
                #Write rest of table entries 
                else: 
                    placeholder[i].table.cell(row, col).text = "其他行"
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].font.size=Pt(11) 
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].font.name="微软雅黑"
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].alignment = PP_ALIGN.CENTER  # 居中
                    placeholder[i].table.cell(row, col).text_frame.paragraphs[0].font.color.rgb = RGBColor(R, G, B) 
                    placeholder[i].table.cell(row, col).fill.solid() 
                    if row%2==1 and R==0:
                        placeholder[i].table.cell(row, col).fill.fore_color.rgb=RGBColor(P_R,P_G,P_B) 
                    elif row%2==1 and R==237:
                        placeholder[i].table.cell(row, col).fill.fore_color.rgb=RGBColor(N_R,N_G,N_B) 
                    else:
                        placeholder[i].table.cell(row, col).fill.fore_color.rgb=RGBColor(255,255,255)
                        placeholder[i].table.cell(row, col).fill.transparency=200
# # 追加新文字
# new_paragraph.text = '追加的新文字'
# placeholder[4].text  = "%%file盖覆盖!!!"
ppt.save('../output/test_file_'+time.strftime("%Y-%m-%d %H点%M分%S秒")+'.pptx')

总结

沉淀好python自动化(以后可以摸鱼 ),剩下研究一下
1 数据工程方面的
2 数据业务的一些书,多看报告
3 毕设
4 研究一下python爬虫项目
目标:熟悉并能实现数据链路下的一个项目,后续计划把python开发安排上

在这里插入图片描述

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据闲逛人

谢谢大嘎喔~ 开心就好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值