龙空技术网

实用干货分享:Python PPT的操作

传说天空有个洞 368

前言:

如今同学们对“python3ppt”都比较关注,朋友们都想要剖析一些“python3ppt”的相关资讯。那么小编在网络上搜集了一些有关“python3ppt””的相关内容,希望大家能喜欢,小伙伴们一起来了解一下吧!

新建PPT文档:

from pptx import Presentation

# 新建ppt

ppt = Presentation()

# 新建页面

slide = ppt.slides.add_slide(ppt.slide_layouts[6])

# 保存ppt

ppt.save('数据报表.pptx')

添加单行文字:

from pptx.util import Pt,Cm

# 添加单行内容

# 设置添加文字框的位置以及大小

left, top, width, height = Cm(8),Cm(4),Cm(20),Cm(4)

# 添加文字段落

paragraph1 = slide.shapes.add_textbox(left=left, top=top, width=width,

height=height).text_frame

# 设置段落内容

paragraph1.paragraphs[0].text = ‘第一章 Python基础’

# 设置文字大小

paragraph1.paragraphs[0].font.size = Pt(30)

添加多行文字:

paragraph1.paragraphs[0].text = '多行内容1\n多行内容2\n多行内容3'

添加表格:

from pptx.dml.color import RGBColor

from pptx.enum.text import MSO_VERTICAL_ANCHOR

from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT

slide=ppt.slides.add_slide(ppt.slide_layouts[6])

# 设置表格位置和大小

left, top, width, height = Cm(6), Cm(6), Cm(13.6), Cm(5)

# 表格行列数,和大小

shape = slide.shapes.add_table(3, 4, left, top, width, height)

# 获取table对象

table = shape.table

# 设置列宽

table.columns[0].width = Cm(3)

table.columns[1].width = Cm(2.3)

# 设置行高

table.rows[0].height = Cm(1)

# 合并首行

table.cell(0, 0).merge(table.cell(0, 3))

# 填写标题

table.cell(0,0).text='进度统计表'

table.cell(1,0).text='时间'

table.cell(1,1).text='阶段'

table.cell(1,2).text='阶段完成'

table.cell(1,3).text='总完成'

for rows in range(1, 3):

for cols in range(4):

# 设置文字大小

table.cell(rows, cols).text_frame.paragraphs[0].font.size = Pt(15)

# 设置字体

table.cell(rows, cols).text_frame.paragraphs[0].font.name = '微软雅黑'

# 设置文字颜色

table.cell(rows, cols).text_frame.paragraphs[0].font.color.rgb = RGBColor(255, 255, 255)

# 设置文字左右对齐

table.cell(rows, cols).text_frame.paragraphs[0].alignment =

PP_PARAGRAPH_ALIGNMENT.CENTER

# 设置文字上下对齐

table.cell(rows, cols).vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLE

# 设置背景为填充

table.cell(rows, cols).fill.solid()

# 设置背景颜色

table.cell(rows, cols).fill.fore_color.rgb = RGBColor(34, 134, 165)

添加图表:

from pptx.chart.data import ChartData

from pptx.enum.chart import XL_CHART_TYPE

# 获取slide对象

slide=ppt.slides.add_slide(ppt.slide_layouts[6])

# 初始化图表

chart_data = ChartData()

# 填充需要添加的内容

content_arr = [["4/30-5/14", "DVT1", "20", "12", "22", "25", "5"],

["5/15-5/21", "DVT1", "25", "32", "42", "30", "8"] ,

["5/22-6/28", "DVT1", "1", "27", "37", "56", "12"],

["5/22-6/28", "DVT1", "1", "27", "37", "56", "12"]]

# 填充图表

chart_data.categories = [content_arr[0][0], content_arr[1][0], content_arr[2][0], content_arr[3][0]]

chart_data.add_series("问题总数", (content_arr[0][4], content_arr[1][4], content_arr[2][4],

content_arr[3][4]))

chart_data.add_series("遗留问题总数", (content_arr[0][5], content_arr[1][5], content_arr[2][5],

content_arr[3][5]))

chart_data.add_series("遗留致命严重\n问题总数", (content_arr[0][6], content_arr[1][6],

content_arr[2][6], content_arr[3][6]))

# 设置位置

left, top, width, height = Cm(6), Cm(7), Cm(16.1), Cm(7.5)

# 添加图表

chart = slide.shapes.add_chart(

XL_CHART_TYPE.LINE, left, top, width, height, chart_data

).chart

chart.has_legend = True

chart.legend.include_in_layout = False

chart.series[0].smooth = True # 是否平滑

chart.series[1].smooth = True

chart.series[2].smooth = True

chart.font.size = Pt(10) # 文字大小

添加形状:

from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT

from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE

# 添加矩形

# 设置位置以及大小

left, top, width, height = Cm(2.5), Cm(4.5), Cm(20), Cm(0.5)

# 添加形状

rectangle = slide.shapes.add_shape(MSO_AUTO_SHAPE_TYPE.RECTANGLE, left, top, width,

height)

# 设置背景填充

rectangle.fill.solid()

# 设置背景颜色

rectangle.fill.fore_color.rgb = RGBColor(34, 134, 165)

# 设置边框颜色

rectangle.line.color.rgb = RGBColor(34, 134, 165)

# 添加正三角+文字

left, top, width, height = Cm(3), Cm(10), Cm(0.5), Cm(0.4)

slide.shapes.add_shape(MSO_AUTO_SHAPE_TYPE.FLOWCHART_EXTRACT, left, top, width,

height)

new_paragraph = slide.shapes.add_textbox(left=left - Cm(0.95), top=top + Cm(0.4),

width=Cm(2.4),height=Cm(1.1)).text_frame

content = '2021/05/21\n内容1'

new_paragraph.paragraphs[0].text = content

new_paragraph.paragraphs[0].font.size = Pt(10) # 文字大小

new_paragraph.paragraphs[0].alignment = PP_PARAGRAPH_ALIGNMENT.CENTER

# 添加向下箭头图形

left, top, width, height = Cm(7), Cm(4), Cm(0.5), Cm(0.4)

now = slide.shapes.add_shape(MSO_AUTO_SHAPE_TYPE.DOWN_ARROW, left, top, width,

height)

now.fill.solid()

now.fill.fore_color.rgb = RGBColor(254, 152, 47)

now.line.color.rgb = RGBColor(254, 152, 47)

插入图片:

from pptx import Presentation

from pptx.util import Pt,Cm

# 获取slide对象

slide=ppt.slides.add_slide(ppt.slide_layouts[6])

# 设置待添加的图片

img_name = 'my.jpg'

# 设置位置

left, top, width = Cm(8), Cm(4), Cm(9)

# 进行添加

slide.shapes.add_picture(image_file=img_name,left=left,top=top,width=width)

标签: #python3ppt