python-pptx为Python第三方模块,用于自动化生成和更新PowerPoint(.pptx)文件
安装:pip install python-pptx(python-pptx依赖的python版本为:2.6、2.7、3.3、3.4、3.6)
查看:pip List
导入:import pptx
2. python-pptx模块的使用2.1 写入文本到PPTimport pptxfrom pptx.util import Inches# 步骤一:得到演示文稿的对象prs = pptx.Presentation('test.pptx') # 以test.pptx为模板# 步骤二:写入 *** 作# 设置幻灯片布局slIDe = prs.slIDes.add_slIDe(prs.slIDe_layouts[0])# prs.slIDes.add_slIDe(prs.slIDe_layouts[1])# prs.slIDes.add_slIDe(prs.slIDe_layouts[2])# 删除幻灯片# print(len(prs.slIDes))# del prs.slIDes._sldIDLst[1] # 根据索引删除,删除第2张幻灯片# print(len(prs.slIDes))# 添加文本框text1 = slIDe.shapes.add_textBox(Inches(5), Inches(5), Inches(5), Inches(5))text1.text = "这是文本框"# 添加段落p1 = text1.text_frame.add_paragraph()p1.text = "我是段落1"Title_shape = slIDe.shapes.TitleTitle_shape.text = '标题1'slIDe.shapes.placeholders[1].text = '标题2'# 添加文本p1.add_run().text = "end"# 步骤三:保存PPT文件#prs.save('newtest.pptx')prs.save('test.pptx')
执行效果:
import pptxfrom pptx.enum.shapes import MSO_SHAPEfrom pptx.dml.color import RGBcolorfrom pptx.util import Inches, Pt# 步骤一:得到演示文稿的对象prs = pptx.Presentation('test.pptx') # 以test.pptx为模板# 步骤二:写入 *** 作# 设置幻灯片布局slIDe = prs.slIDes.add_slIDe(prs.slIDe_layouts[0])# prs.slIDes.add_slIDe(prs.slIDe_layouts[1])# prs.slIDes.add_slIDe(prs.slIDe_layouts[2])# 删除幻灯片# print(len(prs.slIDes))# del prs.slIDes._sldIDLst[1] # 根据索引删除,删除第2张幻灯片# print(len(prs.slIDes))# 添加文本框text1 = slIDe.shapes.add_textBox(Inches(5), Inches(5), Inches(5), Inches(5))text1.text = "这是文本框"# 添加段落p1 = text1.text_frame.add_paragraph()p1.text = "我是段落1"# 添加文本p1.add_run().text = "end"Title_shape = slIDe.shapes.TitleTitle_shape.text = '标题1'slIDe.shapes.placeholders[1].text = '标题2'# 添加自选图形# 写入矩形,设置位置及大小#slIDe.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(2), Inches(2), Inches(5), Inches(3))shape = slIDe.shapes.add_shape(MSO_SHAPE.HEXAGON, Inches(2), Inches(2), Inches(5), Inches(3))# 填充、边框fill = shape.fill # 纯色填充fill.solID()# 设置填充色fill.fore_color.rgb = RGBcolor(255, 0, 0)# 设置边线line = shape.lineline.color.rgb = RGBcolor(55, 3, 5)# 设置边线大小(宽度)line.wIDth = Pt(2)# 步骤三:保存PPT文件#prs.save('newtest.pptx')prs.save('test.pptx')
执行效果:
import pptxfrom pptx.enum.shapes import MSO_SHAPEfrom pptx.dml.color import RGBcolorfrom pptx.util import Inches, Pt# 步骤一:得到演示文稿的对象prs = pptx.Presentation('test.pptx') # 以test.pptx为模板# 步骤二:写入 *** 作# 设置幻灯片布局slIDe = prs.slIDes.add_slIDe(prs.slIDe_layouts[0])# prs.slIDes.add_slIDe(prs.slIDe_layouts[1])# prs.slIDes.add_slIDe(prs.slIDe_layouts[2])# 删除幻灯片# print(len(prs.slIDes))# del prs.slIDes._sldIDLst[1] # 根据索引删除,删除第2张幻灯片# print(len(prs.slIDes))# 添加文本框text1 = slIDe.shapes.add_textBox(Inches(5), Inches(5), Inches(5), Inches(5))text1.text = "这是文本框"# 添加段落p1 = text1.text_frame.add_paragraph()p1.text = "我是段落1"# 添加文本p1.add_run().text = "end"Title_shape = slIDe.shapes.TitleTitle_shape.text = '标题1'slIDe.shapes.placeholders[1].text = '标题2'# 添加自选图形# 写入矩形,设置位置及大小#slIDe.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(2), Inches(2), Inches(5), Inches(3))shape = slIDe.shapes.add_shape(MSO_SHAPE.HEXAGON, Inches(2), Inches(2), Inches(5), Inches(3))# 填充、边框fill = shape.fill # 纯色填充fill.solID()# 设置填充色fill.fore_color.rgb = RGBcolor(255, 0, 0)# 设置边线line = shape.lineline.color.rgb = RGBcolor(55, 3, 5)# 设置边线大小(宽度)line.wIDth = Pt(2)# 添加表格# rows, cols, left, top, wIDth, heighttable = slIDe.shapes.add_table(3, 3, Inches(2), Inches(2), Inches(4), Inches(2)).table# 填充内容table.cell(1, 0).text = 'name'table.cell(1, 1).text = 'age'table.cell(1, 2).text = 'hobby'table.cell(2, 0).text = '张三'table.cell(2, 1).text = '18'table.cell(2, 2).text = 'music'# 合并单元格cell = table.cell(0, 0)cell1 = table.cell(0, 2)cell.merge(cell1)table.cell(0, 0).text = '兴趣爱好信息' # 第一行# 取消合并#print(cell.is_merge_origin) # 单元格是否合并#cell.split()# 步骤三:保存PPT文件#prs.save('newtest.pptx')prs.save('test.pptx')
执行效果:
import pptxfrom pptx.util import Inchesfrom pptx.chart.data import categoryChartDatafrom pptx.enum.chart import XL_CHART_TYPEfrom pptx.enum.chart import XL_LEGEND_position# 步骤一:得到演示文稿的对象prs = pptx.Presentation()# 步骤二:写入 *** 作# 设置幻灯片布局slIDe = prs.slIDes.add_slIDe(prs.slIDe_layouts[0])# 写入图表chart_data = categoryChartData()chart_data.categorIEs = ['一月份', '二月份', '三月份'] # X轴# 具体数据chart_data.add_serIEs('Y2019', (250, 350, 500))chart_data.add_serIEs('Y2020', (270, 380, 600))chart_data.add_serIEs('Y2021', (300, 450, 650))'''chart_type:图表类型, x, y, cx, cy, chart_data:图表数据'''chart = slIDe.shapes.add_chart(XL_CHART_TYPE.ColUMN_CLUSTERED, Inches(2), Inches(2), Inches(6), Inches(4), chart_data).chart# 设置图表标题chart.has_Title = Truechart.chart_Title.text_frame.text = '第一季度销售额'# 设置图例chart.has_legend = True# 设置图例位置chart.legend.position = XL_LEGEND_position.RIGHT# 步骤三:保存PPT文件prs.save('test2.pptx')
执行效果:
import pptxfrom pptx.util import Inches, Ptfrom pptx.enum.shapes import MSO_SHAPEfrom pptx.dml.color import RGBcolorfrom pptx.chart.data import categoryChartDatafrom pptx.enum.chart import XL_CHART_TYPEfrom pptx.enum.chart import XL_LEGEND_position# 构建对象prs = pptx.Presentation()# 第一页slIDe1 = prs.slIDes.add_slIDe(prs.slIDe_layouts[0])# 标题slIDe1.shapes.Title.text = '4S店汽车消费者洞察报告'# 占位符slIDe1.shapes.placeholders[1].text = '2021年第一季度'# 第二页slIDe2 = prs.slIDes.add_slIDe(prs.slIDe_layouts[1]) # 布局slIDe2.shapes.Title.text = '目录'# 通过占位符的方式获取content = slIDe2.shapes.placeholders[1].text_frame# 添加段落content.add_paragraph().text = '4S店第一季度汽车销售榜单'content.add_paragraph().text = '汽车用户消费偏好趋势'content.add_paragraph().text = '汽车消费者用户画像'# 第三页slIDe3 = prs.slIDes.add_slIDe(prs.slIDe_layouts[1]) # 布局slIDe3.shapes.Title.text = '4S店第一季度汽车销售榜单''''行数:11,列数:4,距离左侧边缘距离:0,距离顶部边缘距离:1.4,宽度:10,高度:6'''# 获取表格table = slIDe3.shapes.add_table(11, 4, Inches(0), Inches(1.4), Inches(10), Inches(6)).table# 写入内容table.cell(0, 0).text = '排名'table.cell(0, 1).text = '车型'table.cell(0, 2).text = '所属厂商'table.cell(0, 3).text = '一季度销售额'data = [ {'type': '日产轩逸', 'base': '东风日产', 'total': 50000}, {'type': '大众宝来', 'base': '一汽大众', 'total': 46401}, {'type': '吉利帝豪', 'base': '吉利汽车', 'total': 43000}, {'type': '奥迪A6', 'base': '一汽大众', 'total': 41000}, {'type': '大众速腾', 'base': '一汽大众', 'total': 40000}, {'type': '本田雅阁', 'base': '广汽本田', 'total': 39000}, {'type': '奔驰C级', 'base': '北京奔驰', 'total': 35000}, {'type': '长安逸动', 'base': '长安汽车', 'total': 34000}, {'type': '宝马5系', 'base': '华晨宝马', 'total': 25000}, {'type': '奥迪A4L', 'base': '一汽大众', 'total': 20000}, ]for i in range(11): if i>0: table.cell(i, 0).text = str(i) # 排名 table.cell(i, 1).text = data[i-1]['type'] # 车型 table.cell(i, 2).text = data[i-1]['base'] # 所属厂家 table.cell(i, 3).text = str(data[i-1]['total']) # 销量# 第四页slIDe4 = prs.slIDes.add_slIDe(prs.slIDe_layouts[1]) # 布局slIDe4.shapes.Title.text = '汽车用户消费偏好趋势'shape1 = slIDe4.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(1), Inches(1.5), Inches(3), Inches(2.5))fill = shape1.fill # 纯色填充fill.solID()# 设置填充色fill.fore_color.rgb = RGBcolor(51, 102, 255)shape1.text_frame.add_paragraph().text = '价位偏好:'p1 = shape1.text_frame.add_paragraph()p1.text = '经济入门型'p1.Font.size = Pt(35)shape2 = slIDe4.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(4), Inches(1.5), Inches(5), Inches(2.5))shape2.text_frame.add_paragraph().text = '国别偏好:'p2 = shape2.text_frame.add_paragraph()p2.text = '国产自主品牌'p2.Font.size = Pt(35)shape3 = slIDe4.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(1), Inches(4), Inches(8), Inches(2.5))fill = shape1.fill # 纯色填充fill.solID()fill.fore_color.rgb = RGBcolor(112, 219, 255)shape3.text_frame.add_paragraph().text = '车型偏好:'p3 = shape3.text_frame.add_paragraph()p3.text = 'SUV车型仍为主力'p3.Font.size = Pt(35)# 第五页slIDe5 = prs.slIDes.add_slIDe(prs.slIDe_layouts[1]) # 布局slIDe5.shapes.Title.text = '汽车消费者用户画像'chart_data = categoryChartData()chart_data.categorIEs = ['70后', '80后', '90后']chart_data.add_serIEs('各年龄段平均购车花费', (40, 45, 15))# 柱状图chart = slIDe5.shapes.add_chart( XL_CHART_TYPE.ColUMN_CLUSTERED, Inches(1), Inches(2.5), Inches(4), Inches(3), chart_data).chartchart_data1 = categoryChartData()chart_data1.categorIEs = ['男', '女']chart_data1.add_serIEs('性别分布', (65, 35))# 饼图chart1 = slIDe5.shapes.add_chart( XL_CHART_TYPE.PIE, Inches(6), Inches(2.5), Inches(3), Inches(3), chart_data1).chart# 显示标题chart.has_Title = True# 显示标题chart1.has_Title = True# 显示图例chart1.has_legend = Truechart1.legend.position = XL_LEGEND_position.RIGHT# 第六页slIDe6 = prs.slIDes.add_slIDe(prs.slIDe_layouts[0])slIDe6.shapes.Title.text = '谢谢!'prs.save('report.pptx')
执行效果:
以上是内存溢出为你收集整理的Python——玩转PPT自动化全部内容,希望文章能够帮你解决Python——玩转PPT自动化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)