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

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

PPT增加图片



常用方法与属性

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


代码

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增加流程图



代码

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()  增加图表


代码

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 设置图例布局是否在图表中


代码

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绘制其它图表



代码

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()
目录
相关文章
|
3天前
|
运维 Shell Python
Shell和Python学习教程总结
Shell和Python学习教程总结
|
3天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
3天前
|
存储 索引 Python
Python从入门到精通——1.3.1练习编写简单程序
Python从入门到精通——1.3.1练习编写简单程序
|
4天前
|
开发框架 前端开发 数据库
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
Python从入门到精通:3.3.2 深入学习Python库和框架:Web开发框架的探索与实践
|
15天前
|
数据采集 存储 API
网络爬虫与数据采集:使用Python自动化获取网页数据
【4月更文挑战第12天】本文介绍了Python网络爬虫的基础知识,包括网络爬虫概念(请求网页、解析、存储数据和处理异常)和Python常用的爬虫库requests(发送HTTP请求)与BeautifulSoup(解析HTML)。通过基本流程示例展示了如何导入库、发送请求、解析网页、提取数据、存储数据及处理异常。还提到了Python爬虫的实际应用,如获取新闻数据和商品信息。
|
1月前
|
Web App开发 Python
在ModelScope中,你可以使用Python的浏览器自动化库
在ModelScope中,你可以使用Python的浏览器自动化库
17 2
|
1月前
|
存储 BI 数据处理
Python自动化 | 解锁高效办公利器,Python助您轻松驾驭Excel!
Python自动化 | 解锁高效办公利器,Python助您轻松驾驭Excel!
|
1月前
|
JavaScript 前端开发 Python
【python自动化】Playwright基础教程(三)定位操作
【python自动化】Playwright基础教程(三)定位操作
54 0
|
1月前
|
Python
【python自动化】Playwright基础教程(五)事件操作②悬停&输入&清除精讲
【python自动化】Playwright基础教程(五)事件操作②悬停&输入&清除精讲
53 0
|
4天前
|
人工智能 Python
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
【Python实用技能】建议收藏:自动化实现网页内容转PDF并保存的方法探索(含代码,亲测可用)
24 0

热门文章

最新文章