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
import pptx from 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.title title_shape.text = '标题1' slide.shapes.placeholders[1].text = '标题2' # 添加文本 p1.add_run().text = "end" # 步骤三:保存PPT文件 #prs.save('newtest.pptx') prs.save('test.pptx')
执行效果:
import pptx from pptx.enum.shapes import MSO_SHAPE from pptx.dml.color import RGBColor from 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.title title_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.line line.color.rgb = RGBColor(55, 3, 5) # 设置边线大小(宽度) line.width = Pt(2) # 步骤三:保存PPT文件 #prs.save('newtest.pptx') prs.save('test.pptx')
执行效果:
import pptx from pptx.enum.shapes import MSO_SHAPE from pptx.dml.color import RGBColor from 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.title title_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.line line.color.rgb = RGBColor(55, 3, 5) # 设置边线大小(宽度) line.width = Pt(2) # 添加表格 # rows, cols, left, top, width, height table = 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 pptx from pptx.util import Inches from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE from 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 = True chart.chart_title.text_frame.text = '第一季度销售额' # 设置图例 chart.has_legend = True # 设置图例位置 chart.legend.position = XL_LEGEND_POSITION.RIGHT # 步骤三:保存PPT文件 prs.save('test2.pptx')
执行效果:
import pptx from pptx.util import Inches, Pt from pptx.enum.shapes import MSO_SHAPE from pptx.dml.color import RGBColor from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE from 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 ).chart chart_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 = True chart1.legend.position = XL_LEGEND_POSITION.RIGHT # 第六页 slide6 = prs.slides.add_slide(prs.slide_layouts[0]) slide6.shapes.title.text = '谢谢!' prs.save('report.pptx')
执行效果: