python+pptx:(三)添加统计图、删除指定页

目录

统计图

删除PPT页


python 复制代码
from pptx import Presentation
from pptx.util import Cm, Inches, Mm, Pt
from pptx.dml.color import RGBColor
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LABEL_POSITION, XL_DATA_LABEL_POSITION

file_path = r'C:\Users\Administrator\Desktop\testfile\测试文件\test.pptx'
prs = Presentation(file_path)  # 创建PPT文件对象,没有参数为创建新的PPT,有参数表示打开已有PPT文件对象

统计图

python 复制代码
# 设置数据对象
ct = prs.slides[6].shapes
chart_data_x = ['Q1', 'Q2', 'Q3', 'Q4']
chart_data_y1 = [2010, 988, 1085, 2588]
chart_data_y2 = [2880, 699, 1011, 2623]
chart_data_y3 = [1334, 955, 2565, 3665]
chart_data = ChartData()
chart_data.categories = chart_data_x  # 设置横轴数据
chart_data.add_series(name='一厂', values=chart_data_y1)
chart_data.add_series(name='二厂', values=chart_data_y2)
chart_data.add_series(name='三厂', values=chart_data_y3)

# 添加统计图
left, top, width, height = Cm(3), Cm(5), Cm(25), Cm(12)
# new_chart = ct.add_chart(chart_type=XL_CHART_TYPE.LINE, x=left, y=top, cx=width, cy=height, chart_data=chart_data)  # chart_type图表样式,LINE为折线图
new_chart = ct.add_chart(chart_type=XL_CHART_TYPE.COLUMN_CLUSTERED, x=left, y=top, cx=width, cy=height,chart_data=chart_data) # COLUMN_CLUSTERED为柱状图

# 设置统计图标题样式和整体颜色风格
get_chart = new_chart.chart
get_chart.chart_style = 20  # 整体颜色风格(取值为1-48)
get_chart.has_title = True  # 设置标题
get_chart.chart_title.text_frame.clear()  # 清除原标题
new_tile = get_chart.chart_title.text_frame.add_paragraph()
new_tile.text = '2024季度账目汇总(单位:万元)'
new_tile.font.size = Pt(20)
new_tile.font.color.rgb = RGBColor(100, 200, 50)

# 设置数据节点样式
plot = get_chart.plots[0]  # 获取图表的plot
plot.has_data_labels = True  # 统计图表节点上是否显示数据
p = plot.data_labels  # 数据标签控制类
p.font.size = Pt(5)  # 测试没有用!!
p.font.color.rgb = RGBColor(80, 200, 100)  # 测试没有用!!
p.position = XL_DATA_LABEL_POSITION.CENTER  # 设置数据位置,INSIDE_END\OUTSIDE_END 等

删除PPT页

python 复制代码
page = list(prs.slides._sldIdLst)  # 获取ppt页列表,slides 对象的源码中有私有属性sldIdLst指向幻灯片元素集合,获取幻灯片页数
prs.slides._sldIdLst.remove(page[-2])  # 删除指定页
相关推荐
我是苏苏1 小时前
C#基础:Winform桌面开发中窗体之间的数据传递
开发语言·c#
斐波娜娜1 小时前
Maven详解
java·开发语言·maven
小码氓2 小时前
Java填充Word模板
java·开发语言·spring·word
暮鹤筠2 小时前
[C语言初阶]操作符
c语言·开发语言
chao_7893 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
烛阴4 小时前
Python装饰器解除:如何让被装饰的函数重获自由?
前端·python
Boilermaker19924 小时前
【Java EE】Mybatis-Plus
java·开发语言·java-ee
aramae4 小时前
C++ -- STL -- vector
开发语言·c++·笔记·后端·visual studio
Tony小周4 小时前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
noravinsc4 小时前
django 一个表中包括id和parentid,如何通过parentid找到全部父爷id
python·django·sqlite