领导要求能够将数据分析同事的结果进行自动生成ppt的操作,所以果断问度娘:
此时主要用到的包是pptx
主要用到的方法:
导入ppt模板:
这个模板必须是提前存在的
prs = Presentation(path + "\\model\\*****.pptx")
elbtslide = prs.slides.add_slide(prs.slide_layouts[6]) # 在幻灯片中加入一页6号风格(空白)幻灯片
title_only_slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(title_only_slide_layout) shapes = slide.shapes subtitle = slide.placeholders[1] #设计标题 shapes.title.text = '板桥河下坝闸站点'.center(10) #添加副标题 subtitle.text = (now.strftime("%Y-%m-%d %H:%M:00") + '\r\n' + 'author:quqinran').center(10) #添加图片 slide.shapes.add_picture(r'F:\PycharmProjects\test\PPT\标题图片.png', Inches(0), Inches(0),Inches(13))
添加文本框,并给文本框附文字
textbox = wtslide.shapes.add_textbox(Cm(0.8), Cm(0.8), Cm(4), Cm(1.03)) # left,top为相对位置,width,height为文本框大小
textbox.text = '水温,PH'.center(10) # 文本框中文字
添加表格 table = wtslide.shapes.add_table(rows, cols, left, top, width, height).table
# 设置单元格宽度 table.columns[0].width = Cm(2) # Inches(2.0) table.columns[1].width = Cm(1.8) table.columns[2].width = Cm(2.3) # Inches(2.0) table.columns[3].width = Cm(3.3) table.columns[4].width = Cm(2.7)
# 填充数据 table.cell(1, 0).text = str(val_AI1[0]) table.cell(1, 1).text = str(val_AI1[1]) table.cell(1, 2).text = str(val_AI1[2]) table.cell(1, 3).text = str(val_AI1[3]) table.cell(1, 4).text = str(val_AI1[4])
建议是:这个是有中文文档的,上网可搜,我一般是在搞不出来的格式,如果是固定格式的话,直接通过ppt创建好然后截图,之后直接添加图片就行,效果和自己通过代码编辑的差不多,逻辑其实不难,难就难在各种格式还有位置啊,大小之类的,需要自已一个一个调整才能到理想的位置,所以比较麻烦吧。