Python办公自动化【PPT增加图片、PPT增加流程图PPT增加图表、PPT设置图表样式、PPT绘制其它图表】(七)-全面详解(学习总结---从入门到深化)

文件目录

PPT增加图片

PPT增加流程图

PPT增加图表

PPT设置图表样式

PPT绘制其它图表


PPT增加图片

常用方法与属性

|-----------------------------------------|--------|
| 函数名&属性 | 含义 |
| slide.shapes.add_picture(path,left,top) | 增加图片信息 |

代码

python 复制代码
from pptx import Presentation
from pptx.util import Pt
def add_pic():
  # 创建一个ppt文档
  ppt = Presentation()
  # 建立一个幻灯片
  slide = ppt.slides.add_slide(ppt.slide_layouts[1])
  shapes = slide.shapes
  # 增加图片
 '''
 TypeError:
_BaseGroupShapes.add_picture() missing 2 required positional arguments: 'left' and
'top'
 '''
  num = Pt(30)

 shapes.add_picture('./base_data/backg.jpg', num,num)
  # 建立第2个幻灯片
  slide2 = ppt.slides.add_slide(ppt.slide_layouts[1])
  shapes2 = slide2.shapes

  shapes2.add_picture('./base_data/backg.jpg',num,num,Pt(300))
  # 保存ppt文档
  ppt.save('./create_data/03_增加图片.pptx')


if __name__ == '__main__':
  add_pic()

PPT增加流程图

代码

python 复制代码
from pptx import Presentation
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
from pptx.util import Pt

def create_shape():
  # 创建PPT文件
  ppt = Presentation()
  # 创建一个幻灯片
  slide = ppt.slides.add_slide(ppt.slide_layouts[5])
  # 获取形状对象
  shapes = slide.shapes
  shapes.title.text= '流程图'
  # 增加图形
  '''
 TypeError: _BaseGroupShapes.add_shape()
 missing 4 required positional arguments:
'left', 'top', 'width', and 'height'
 '''
  left = Pt(100)
  top = Pt(200)
  width = Pt(100)
  height = Pt(30)

  tmp_shape = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.PENTAGON,left,top,width,height)
  tmp_shape.text = '第1步'

  for i in range(2,5):
    left =  left + width -Pt(10)
    ts = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.CHEVRON,left,top,width,height)
    frame = ts.text_frame
    frame.text = f'第{i}步'
    frame.fit_text(max_size = 10,bold = True,italic = True)

  # 保存PPT文件
  ppt.save('./create_data/04_增加图形.pptx')

if __name__ == '__main__':
  create_shape()

PPT增加图表

常用方法与属性

|-------------------------------------|--------|
| 函数名&属性 | 含义 |
| pptx.chart.data.CategoryChartData() | 封装图表数据 |
| pptx.enum.chart.XL_CHART_TYPE | 图表类型 |
| CategoryChartData.categories | 设置分组 |
| CategoryChartData.add_series() | 设置图表数据 |
| slide.shapes.add_chart() | 增加图表 |

代码

python 复制代码
from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Pt

def use_chart():
  # 创建给我和PPT
  ppt = Presentation()
  # 增加一个页面
  slide = ppt.slides.add_slide(ppt.slide_layouts[6])
  # 设置图表
  # 设置图表的数据
  chart_data = CategoryChartData()
  # 设置分组
  chart_data.categories = ['第一季度','第二季度','第三季度','第四季度']
  # 设置数据
  chart_data.add_series('series', (19,21,16,30))
  # 将图表增到页面中
  '''
 TypeError: _BaseGroupShapes.add_chart() missing 5 required positional arguments:
'x', 'y', 'cx', 'cy', and 'chart_data'
 '''
 slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED,Pt(100),Pt(100),Pt(500),Pt(350),chart_data)
  # 保存PPT
  ppt.save('./create_data/05_增加图表.pptx')

def use_chart2():
  # 创建给我和PPT
  ppt = Presentation()
  # 增加一个页面
  slide = ppt.slides.add_slide(ppt.slide_layouts[6])
  # 设置图表
  # 设置图表的数据
  chart_data = CategoryChartData()
  # 设置分组
  chart_data.categories = ['第一季度','第二季度','第三季度','第四季度']
  # 设置数据
  chart_data.add_series('series', (19,21,16,30))
  chart_data.add_series('series', (22,23,15,25))
  chart_data.add_series('series', (20,19,19,28))
  # 将图表增到页面中
  '''
 TypeError: _BaseGroupShapes.add_chart() missing 5 required positional arguments:
'x', 'y', 'cx', 'cy', and 'chart_data'
 '''
 slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED,Pt(100),Pt(100),Pt(500),Pt(350),chart_data)
  # 保存PPT
  ppt.save('./create_data/05_增加图表.pptx')


if __name__ == "__main__":
  # use_chart()
  use_chart2()

PPT设置图表样式

常用方法与属性

|-------------------------------------------|--------------|
| 函数名&属性 | 含义 |
| chart.chart_style | 设置图表主题 |
| chart.font.size | 设置图表字体大小 |
| chart.category_axis.tick_labels.font.size | 设置分类轴字体大小 |
| chart.category_axis.has_major_gridlines | 设置分类轴是否有表示线 |
| chart.plots.has_data_labels | 设置是否显示图表标签 |
| plot.data_labels.position | 设置图表标签位置 |
| chart.has_legend | 设置是否显示图例 |
| chart.legend.font.size | 设置图例字体大小 |
| chart.legend.position | 设置图例位置 |
| chart.legend.include_in_layout | 设置图例布局是否在图表中 |

代码

python 复制代码
from pptx import Presentation
from pptx.chart.data import
CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.util import Pt
from pptx.enum.chart import XL_DATA_LABEL_POSITION,XL_LEGEND_POSITION

def use_chart():
  # 创建给我和PPT
  ppt = Presentation()
  # 增加一个页面
  slide = ppt.slides.add_slide(ppt.slide_layouts[6])
  # 设置图表
  # 设置图表的数据
  chart_data = CategoryChartData()

  # 设置分组
  chart_data.categories = ['第一季度','第二季度','第三季度','第四季度']
  # 设置数据
  chart_data.add_series('分组1',(19,21,16,30))
  chart_data.add_series('分组2',(22,23,15,25))
  chart_data.add_series('分组3',(20,19,19,28))
  # 将图表增到页面中
  chart = slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED,Pt(100),Pt(100),Pt(500),Pt(350),ch
art_data).chart

  # 设置图表的主题 1-48
  chart.chart_style= 10
  # 设置字体大小
  chart.font.size = Pt(10)

  # 获取分类轴的对象
  category =  chart.category_axis
  # 设置分类字体大小
  category.tick_labels.font.size = Pt(20)
  # 设置分类线
  category.has_major_gridlines = True

  # 设置标签对象
  plot = chart.plots[0]
  plot.has_data_labels = True

  plot.data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END

  # 增加图例
  chart.has_legend = True
  chart.legend.font.size = Pt(15)
  # 设置图例位置
  chart.legend.position =XL_LEGEND_POSITION.TOP
  chart.legend.include_in_layout = True

  # 保存PPT
  ppt.save('./create_data/06_设置图表.pptx')


if __name__ == "__main__":
  use_chart()

PPT绘制其它图表

代码

python 复制代码
from pptx import Presentation
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE,XL_DATA_LABEL_POSITION
from pptx.util import Pt


def create_line():

   # 创建新的ppt文档
  ppt = Presentation()
  # 增加新的幻灯片
  slide = ppt.slides.add_slide(ppt.slide_layouts[6])
  # 创建图表数据对象
  chart_data = CategoryChartData()
  # 设置图表分类
  chart_data.categories=['第一季度','第二季度','第三季度','第四季度']
  # 设置数据
  chart_data.add_series('销售1组', (15,20,16,30))
  chart_data.add_series('销售2组', (17,21,15,28))
  chart_data.add_series('销售3组', (16,24,12,25))


  # 增加图表
 slide.shapes.add_chart(XL_CHART_TYPE.LINE,Pt(50),Pt(100),Pt(500),Pt(350),chart_data)
  # 保存ppt
  ppt.save('./create_data/07_折线图.pptx')


def create_pie():
  # 创建新的ppt文档
  ppt = Presentation()
  # 增加新的幻灯片
  slide = ppt.slides.add_slide(ppt.slide_layouts[6])
  # 创建图表数据对象
  chart_data = CategoryChartData()
  # 设置图表分类


  chart_data.categories=['第一季度','第二季度','第三季度','第四季度']
  # 设置数据
  chart_data.add_series('季度销量比例', (0.27,0.25,0.31,0.19))
  # 增加图表
  chart = slide.shapes.add_chart(XL_CHART_TYPE.PIE,Pt(50),Pt(100),Pt(500),Pt(350),chart_data).chart
  

  # 显示图例
  chart.has_legend =True
  # 显示标签
  chart.plots[0].has_data_labels =True
  # 设置数字显示方式
  data_labes = chart.plots[0].data_labels
  data_labes.number_format =  '0%'
  data_labes.position = XL_DATA_LABEL_POSITION.OUTSIDE_END
  # 保存ppt
  ppt.save('./create_data/07_饼图.pptx')


if __name__ == '__main__':
  # create_line()
  create_pie()
相关推荐
代码匠心5 分钟前
AI 自动编程:一句话设计高颜值博客
前端·ai·ai编程·claude
_AaronWong1 小时前
Electron 实现仿豆包划词取词功能:从 AI 生成到落地踩坑记
前端·javascript·vue.js
cxxcode1 小时前
I/O 多路复用:从浏览器到 Linux 内核
前端
用户5433081441941 小时前
AI 时代,前端逆向的门槛已经低到离谱 — 以 Upwork 为例
前端
JarvanMo2 小时前
Flutter 版本的 material_ui 已经上架 pub.dev 啦!快来抢先体验吧。
前端
恋猫de小郭2 小时前
AI 可以让 WIFI 实现监控室内人体位置和姿态,无需摄像头?
前端·人工智能·ai编程
哀木2 小时前
给自己整一个 claude code,解锁编程新姿势
前端
程序员鱼皮2 小时前
GitHub 关注突破 2w,我总结了 10 个涨星涨粉技巧!
前端·后端·github
UrbanJazzerati2 小时前
Vue3 父子组件通信完全指南
前端·面试
是一碗螺丝粉2 小时前
5分钟上手LangChain.js:用DeepSeek给你的App加上AI能力
前端·人工智能·langchain