安装
python操作pptx文件
from pptx import Presentation
创建ppt文件
prs = Presentation()
prs.save('texst01_ppt.pptx')
创建副件
prs1 = Presentation('texst01_ppt.pptx')
prs1.save('texst01_ppt副件.pptx')
操作幻灯片
from pptx import Presentation
prs = Presentation()
slide_layout = prs.slide_layouts[1]
slide_01 = prs.slides.add_slide(slide_layout)
shapes = slide_01.shapes
prs.save('test02_ppt.pptx')
形状学习和颜色
from pptx import Presentation
from pptx.util import Inches,Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor
from pptx.enum.dml import MSO_THEME_COLOR
prs = Presentation()
slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(slide_layout)
shapes = slide.shapes
left = top = width = height = Inches(1)
查看一英尺
length = Inches(1)
print(length,length.cm,length.pt)
length2 = Pt(72)
print(length2)
添加圆角矩形并设置长宽和在ppt的位置
shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height)
颜色填充
left = top = width = height = Inches(2)
shape_02 = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height)
fill = shape_02.fill
fill.solid()
fill.fore_color.rgb = RGBColor(255,0,0)
设置形状颜色填充
left = top =width = height = Inches(4)
shape_03 = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height)
fill_03 = shape_03.fill
fill_03.solid()
fill_03.fore_color.theme_color = MSO_THEME_COLOR.ACCENT_1
fill_03.fore_color.brightness = -0.25
设置背景色
left = top =width = height = Inches(8)
shape_04 = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height)
shape_04.fill.background()
设置轮廓颜色
slide_layout_02 = prs.slide_layouts[0]
slide_02 = prs.slides.add_slide(slide_layout_02)
shapes_02 = slide_02.shapes
设置图片轮廓
left = top =width = height = Inches(4)
shape_03 = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE,left,top,width,height)
设置边框
line = shape_02.line
line.color.rgb = RGBColor(255,0,0)
line.color.brithtness = 0.5
line.width = Pt(2.5)
line.color.theme_color = MSO_THEME_COLOR.ACCENT_1
line.fill.background()
保存
prs.save('test03_ppt.pptx')
占位符
from pptx import Presentation
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[8])
for shape in slide.placeholders:
print(shape.placeholder_format.idx,shape.name)
print(slide.palceholder[1])
for shape in slide.shapes:
if shape.is_placeholder:
phf = shape.placeholder_format
print(phf.index,phf.type)
slide_02 = prs.slides.add_slide(prs.slide_layouts[8])
place_holder = slide_02.placeholders[1]
picture = place_holder.insert_picture('壁纸.jpg')
prs.save('test04_ppt.pptx')
文本框
from pptx import Presentation
from pptx.enum.text import MSO_VERTICAL_ANCHOR,MSO_AUTO_SIZE
from pptx.util import Inches,Pt
from pptx.enum.dml import MSO_THEME_COLOR
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])
shapes = slide.shapes
for shape in shapes:
if not shape.has_text_frame:
continue
text_frame = shape.text_frame
print(text_frame)
访问段落
para_strs = [
'Egg,bacon,sausage and spam',
'spam,bacon,sausage and spam',
'spam,egg,spam,spam,bacon and spam'
]
for shape in shapes:
if not shape.has_text_frame:
continue
text_frame = shape.text_frame
text_frame.clear()
para = text_frame.paragraphs[0]
print(para)
para.text = para_strs[0]
run = para.add_run()
run.text = 'hello world'
for para_str in para_strs[1:]:
p = text_frame.add_paragraph()
p.text = para_str
文本框格式
slide_02 = prs.slides.add_slide(prs.slide_layouts[1])
shapes = slide_02.shapes
shape = shapes[0]
text_frame = shape.text_frame
text_frame.text = 'hello world bababbababbababa'
text_frame.margin_bottom = Inches(0.08)
text_frame.vertical_anchor = MSO_VERTICAL_ANCHOR.TOP
text_frame.word_wrap = False
text_frame.auto_size = MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT
text_frame_02 = shapes[1].text_frame
text_frame_02.clear()
p = text_frame_02.paragraphs[0]
run = p.add_run()
run.text = 'how are you,I am study python'
设置颜色
font = run.font
font.name = 'Calibri'
font.size = Pt(15)
font.blod = True
font.italic = None
font.color.theme_color = MSO_THEME_COLOR.ACCENT_1
prs.save('text05_ppt.pptx')
操作图表
导入库
from pptx import Presentation
from pptx.util import Inches,Pt
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LABEL_POSITION
插入柱状图
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
char_data = CategoryChartData()
char_data.categories = ['Apple','banana','cherry']
char_data.add_series('sales',(20,30,10))
left = Inches(2)
top = Inches(2)
width = Inches(6)
height = Inches(5)
slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED,
x=left, y=top,
cx=width, cy=height,
chart_data=char_data
)
prs.save('text06-chart01.pptx')
创建折线图
prs_02 = Presentation()
slide_02 = prs_02.slides.add_slide(prs_02.slide_layouts[5])
chart_data02 = CategoryChartData()
chart_data02.categories = ['Q1 sales','Q2 sales','Q3 sales']
chart_data02.add_series('Apple',(30,25,40))
chart_data02.add_series('banana',(25,30,40))
chart_data02.add_series('cherry',(40,25,30))
chart = slide_02.shapes.add_chart(
XL_CHART_TYPE.LINE,left,top,width,height,chart_data02
)
chart.has_legend = True
prs_02.save('test06_chart02.pptx')
扇形图
prs_03 = Presentation()
slide_03 = prs_03.slides.add_slide(prs_03.slide_layouts[5])
char_data03 = CategoryChartData()
char_data03.categories = ['Q1', 'Q2', 'Q3']
char_data03.add_series('Q1 sales',[0.5,0.3,0.2])
chart_03 = slide_03.shapes.add_chart(
XL_CHART_TYPE.PIE,left,top,width,height,char_data03
)
chart = chart_03.chart
plot = chart.plots[0]
plot.has_data_labels = True
data_labels = plot.data_labels
data_labels.show_category_name = True
data_labels.show_value = False
data_labels.show_percentage = True
data_labels.number_format = '0.0%'
data_labels.position = XL_LABEL_POSITION.INSIDE_END
data_labels.font.name = 'Arial'
data_labels.font.size = Pt(14)
chart.has_title = True
para = chart.chart_title.text_frame.add_paragraph()
para.text = '销量占比'
para.font.size = Pt(16)
prs_03.save('test06_chart03.pptx')
添加表格
from pptx import Presentation
from pptx.util import Inches
prs_04 = Presentation()
slide_04 = prs_04.slides.add_slide(prs_04.slide_layouts[5])
x,y,cx,cy = Inches(2),Inches(2),Inches(4),Inches(1.5)
shape = slide_04.shapes.add_table(3,3,x,y,cx,cy)
print(shape.has_table)
table = shape.table
cell = table.cell(0,0)
cell.text = 'Apple'
prs_04.save('test07_tb.pptx')
练习
导入库
from pptx import Presentation
练习01
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[0])
title = slide.shapes.title
title.text = 'Hello World'
subtitle = slide.placeholders[1]
subtitle.text = 'Welcome python-pptx'
prs.save('test1.pptx')
练习02
slide_02 = prs.slides.add_slide(prs.slide_layouts[1])
shapes = slide_02.shapes
title_shape = shapes.title
body_shape = shapes.placeholders[1]
title_shape.text = '添加bullet shape'
tf = body_shape.text_frame
tf.text = '定位到bullet 幻灯片'
p = tf.add_paragraph()
p.text = '使用文本框'
p.level = 1
p = tf.add_paragraph()
p.text = '使用文本框的二级'
p.level = 2
prs.save('text1.pptx')
插入图片
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
blank_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_layout)
left = top = Inches(1)
img_path = '壁纸.jpg'
slide.shapes.add_picture(
img_path,left,top
)
prs.save('text2.pptx')
添加形状(五边形)
from pptx import Presentation
from pptx.util import Inches
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])
shapes = slide.shapes
shapes.title.text = '添加形状'
left = Inches(0.93)
top = Inches(3)
width = Inches(1.75)
height = Inches(1)
shape = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.PENTAGON,left,top,width,height)
shape.text = 'step'
'''
如何在第一个五边形后边添加V型
- 添加第一个
- 通过for循环改变其内容以及位置
'''
left = left + width - Inches(0.4)
width = Inches(2)
for n in range(2,6):
shape = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.CHEVRON,left,top,width,height)
shape.text = 'step %d '%n
left = left + width - Inches(0.4)
prs.save('test3_shape.pptx')
读取幻灯片内容
from pptx import Presentation
prs = Presentation(r'python-ppt\text1.pptx')
text_runs = []
for slide in prs.slides:
for shape in slide.shapes:
if not shape.has_text_frame:
continue
for para in shape.text_frame.paragraphs:
for run in para.runs:
text_runs.append(run.text)
print(text_runs)