博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 操作PPT练习
阅读量:6242 次
发布时间:2019-06-22

本文共 6970 字,大约阅读时间需要 23 分钟。

from pptx import Presentationfrom pptx.util import Pt, Inchesprs = Presentation()slide = prs.slides.add_slide(prs.slide_layouts[1])# 添加空白页PPTbody_shape = slide.shapes.placeholdersbody_shape[0].text = 'this is placeholders[0]'  # 在第一个文本框中文字框架内添加文字body_shape[1].text = 'this is placeholders[1]'  # 在第二个文本框中文字框架内添加文字print(len(slide.shapes.placeholders))new_paragraph = body_shape[1].text_frame.add_paragraph()  # 在第二个shape中的文本框架中添加新段落new_paragraph.text = 'add_paragraph'  # 新段落中文字new_paragraph.font.bold = True  # 文字加粗new_paragraph.font.italic = True  # 文字斜体new_paragraph.font.size = Pt(15)  # 文字大小new_paragraph.font.underline = True  # 文字下划线new_paragraph.level = 1  # 新段落的级别left = top = width = height = Inches(5)  # 预设位置及大小textbox = slide.shapes.add_textbox(left, top, width, height)  # left,top为相对位置,width,height为文本框大小textbox.text = 'this is a new textbox'  # 文本框中文字new_para = textbox.text_frame.add_paragraph()  # 在新文本框中添加段落new_para.text = 'this is second para in textbox'  # 段落文字img_path = 'dog.png'  # 文件路径left, top, width, height = Inches(1), Inches(4.5), Inches(2), Inches(2)  # 预设位置及大小pic = slide.shapes.add_picture(img_path, left, top, width, height)  # 在指定位置按预设值添加图片from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPEleft, top, width, height = Inches(1), Inches(3), Inches(1.8), Inches(1)  # 预设位置及大小shape = slide.shapes.add_shape(MSO_AUTO_SHAPE_TYPE.PENTAGON, left, top, width, height)  # 在指定位置按预设值添加类型为PENTAGON的形状shape.text = 'Step 1'for n in range(2, 6):    left = left + width - Inches(0.3)    shape = slide.shapes.add_shape(MSO_AUTO_SHAPE_TYPE.CHEVRON, left, top, width, height)    shape.text = 'Step{}'.format(n)rows, cols, left, top, width, height = 2, 2, Inches(3.5), Inches(4.5), Inches(6), Inches(0.8)table = slide.shapes.add_table(rows, cols, left, top, width, height).table  # 添加表格,并取表格类table.columns[0].width = Inches(2.0)  # 第一纵列宽度table.columns[1].width = Inches(4.0)  # 第二纵列宽度table.cell(0, 0).text = 'text00'  # 指定位置写入文本table.cell(0, 1).text = 'text01'table.cell(1, 0).text = 'text10'table.cell(1, 1).text = 'text11'from pptx.chart.data import ChartDatafrom pptx.enum.chart import XL_CHART_TYPEfrom pptx.enum.chart import XL_TICK_MARKfrom pptx.dml.color import RGBColorfrom pptx.enum.chart import XL_DATA_LABEL_POSITIONfrom pptx.enum.chart import XL_LEGEND_POSITIONslide = prs.slides.add_slide(prs.slide_layouts[6])  # 在幻灯片中加入一页6号风格(空白)幻灯片# chart1 左上方图x, y, cx, cy = Inches(0.5), Inches(0.5), Inches(4), Inches(3)  # 按英尺标准指定x,y值chart_data = ChartData()  # 图表data类chart_data.categories = [u'A班级得分率', u'B班级得分率']  # 图表加入两栏chart_data.add_series(u'得分率对比', (80.5, 60.5))  # 在两栏分别填入数据graphic_frame = slide.shapes.add_chart(    XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data)  # add_chart(图表类型,xy表示图表位置,cx cy表示图表宽高,并且插入chart_data中规定好的数据)chart = graphic_frame.chart  # 从生成的图表中取出图表类chart.chart_style = 21  # 图表整体颜色风格chart.has_title = True  # 图表是否含有标题,默认为Falsechart.chart_title.text_frame.clear()  # 清除原标题new_paragraph = chart.chart_title.text_frame.add_paragraph()  # 添加一行新标题new_paragraph.text = '得分率对比'  # 新标题new_paragraph.font.size = Pt(15)  # 新标题字体大小category_axis = chart.category_axis  # category_axis 为chart的category控制类category_axis.has_major_gridlines = True  # 是否显示纵轴线category_axis.tick_labels.font.italic = True  # tick_labels为图表下标签,置为斜体category_axis.tick_labels.font.size = Pt(15)  # 下标签字体大小category_axis.tick_labels.font.color.rgb = RGBColor(255, 0, 0)  # 标签字体颜色value_axis = chart.value_axis  # value_axis 为chart的value控制类value_axis.maximum_scale = 100.0  # 纵坐标最大值value_axis.minimum_scale = 0.0  # 纵坐标最小值value_axis.minor_tick_mark = XL_TICK_MARK.CROSSvalue_axis.has_minor_gridlines = Truetick_labels = value_axis.tick_labels  # tick_labels 为chart的纵轴标签控制类tick_labels.number_format = '0%'  # 标签显示样式tick_labels.font.bold = True  # 字体加粗tick_labels.font.size = Pt(14)  # 字体大小tick_labels.font.color.rgb = RGBColor(0, 255, 0)  # 标签颜色plot = chart.plots[0]  # 取图表中第一个plotplot.has_data_labels = True  # 是否显示数据标签data_labels = plot.data_labels  # 数据标签控制类data_labels.font.size = Pt(13)  # 字体大小data_labels.font.color.rgb = RGBColor(0, 0, 255)  # 字体颜色data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END  # 字体位置# chart 2 左下方图x, y, cx, cy = Inches(0.5), Inches(3.5), Inches(4), Inches(3)  # 按英尺标准指定x,y值chart_data = ChartData()chart_data.categories = ['A', 'B', 'C', 'D']chart_data.add_series(u'A班级选项占比', (80, 10, 9, 10))chart = slide.shapes.add_chart(    XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data).chart  # PIE为饼状图chart.has_legend = True  # 是否含有下方的说明chart.legend.position = XL_LEGEND_POSITION.BOTTOMchart.legend.horz_offset = 0  # 说明位移量 [-1, 1] 默认为0chart.plots[0].has_data_labels = True  # 饼中是否写入数值data_labels = chart.plots[0].data_labelsdata_labels.number_format = '0%'  # 数值显示格式data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_END  # 数值布局方式chart.has_title = Truechart.chart_title.text_frame.clear()  # 清除原标题new_paragraph = chart.chart_title.text_frame.add_paragraph()  # 添加一行新标题new_paragraph.text = 'A班级选项占比'  # 新标题new_paragraph.font.size = Pt(13)  # 新标题字体大小# chart 3 右下方图x, y, cx, cy = Inches(5.5), Inches(4), Inches(4), Inches(3)  # 按英尺标准指定x,y值chart_data = ChartData()chart_data.categories = ['A', 'B', 'C', 'D']chart_data.add_series(u'B班级选项占比', (0.1, 0.2, 0.3, 0.4))chart = slide.shapes.add_chart(    XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data).chartchart.has_legend = Truechart.legend.position = XL_LEGEND_POSITION.BOTTOMchart.plots[0].has_data_labels = Truedata_labels = chart.plots[0].data_labelsdata_labels.number_format = '0%'data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_ENDchart.has_title = Truechart.chart_title.text_frame.clear()  # 清除原标题new_paragraph = chart.chart_title.text_frame.add_paragraph()  # 添加一行新标题new_paragraph.text = 'B班级选项占比'  # 新标题new_paragraph.font.size = Pt(13)  # 新标题字体大小# chart 4 右上方图x, y, cx, cy = Inches(5.5), Inches(0.5), Inches(4), Inches(3)chart_data = ChartData()chart_data.categories = ['0', '1-3', '4-6', '7-9']chart_data.add_series('', (50, 18, 30, 34))chart = slide.shapes.add_chart(    XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data).chartchart.has_legend = Truechart.legend.position = XL_LEGEND_POSITION.BOTTOMchart.legend.font.size = Pt(13)chart.plots[0].has_data_labels = Truedata_labels = chart.plots[0].data_labelsdata_labels.number_format = '0%'data_labels.position = XL_DATA_LABEL_POSITION.INSIDE_ENDchart.has_title = Truechart.chart_title.text_frame.clear()new_title = chart.chart_title.text_frame.add_paragraph()new_title.text = '得分占比'new_title.font.size = Pt(13)prs.save('python-pptx.pptx')从演示文稿中的幻灯片中提取所有文本from pptx import Presentationprs = Presentation('教育行业通案模板 1.pptx')# text_runs will be populated with a list of strings,# one for each text run in presentationtext_runs = []for slide in prs.slides:    for shape in slide.shapes:        if not shape.has_text_frame:            continue        for paragraph in shape.text_frame.paragraphs:            for run in paragraph.runs:                text_runs.append(run.text)print(text_runs)

 

转载于:https://www.cnblogs.com/Erick-L/p/9390281.html

你可能感兴趣的文章
spice在桌面虚拟化中的应用系列之三(USB映射实现,SSL加密,密码认证,多客户端支持)...
查看>>
Loading project 91606170 of 1: Project FooBar 问题如何解决?
查看>>
C# yeild使用
查看>>
MapReduce-Hadoop分布式计算模型
查看>>
StrokePlus
查看>>
joisino's travel
查看>>
组合游戏-博弈论中经典模型题目
查看>>
浅谈HTTP的GET和POST
查看>>
点灯笼
查看>>
try{}catch{}
查看>>
[Aaronyang] 写给自己的WPF4.5 笔记11[自定义控件-AyImageButton的过程 1/4]
查看>>
Linux VMware新添加网络适配器找不到配置文件问题
查看>>
Javascript百学不厌 - this
查看>>
机器学习中的数学(1)-回归(regression)、梯度下降(gradient descent)
查看>>
实用算法实现-第 14 篇 启发式搜索
查看>>
c#常用的排序算法
查看>>
论文阅读——Visual inertial odometry using coupled nonlinear optimization
查看>>
Office插件编程[转]
查看>>
读代码还是读文档,来自知乎
查看>>
Linux 常见编译错误
查看>>