PPT自动化处理,用程序快速排版
PPT自动生成,我去这也行?
1、用PYTHON处理PPT的文字
2、用PYTHON处理PPT的图片和表格
3、用PYTHON实现数据分析与图标
4、用PYTHON进行图标生成和展示
准备:
pip install python-pptx
开始
from pptx import Presentation ppt =Presentation() slide = ppt.slides.add_slide(ppt.slide_layouts[1]) #在ppt插入一个幻灯片 ppt.save('D:/autoOffice/test.pptx')
##使用占位方式添加内容 body_shage =slide.shapes.placeholders body_shage[0].text='这是占位符0' body_shage[1].text='这是占位符1'
# 利用主副标题 title_shape =slide.shapes.title title_shape.text='这是标题' subtitle = slide.shapes.placeholders[1] subtitle.text='这是文本框'
#添加段落及格式 new_paragraph=body_shage[1].text_frame.add_paragraph() new_paragraph.text='新段落' new_paragraph.font.bold = True #文字加粗 new_paragraph.font.italic =True #文字斜体 new_paragraph.font.size =Pt(35) #文字大小 new_paragraph.font.underline= True #文字下划线
#自定义文本框 left =Inches(2) right =Inches(2) width =Inches(2) height =Inches(2) textbox =slide.shapes.add_textbox(left,right,width,height) textbox.text='这是自定义文本框' new_para =textbox.text_frame.add_paragraph() new_para.text ='这是自定义文本框的段落'
段落格式需导入包
from pptx.util import Inches,Pt
根据实际要求增添以上内容
准备:一张图片(banner.jpg)
#插入图片 left =Inches(1) right =Inches(1) width =Inches(8) height =Inches(2) pic =slide.shapes.add_picture('banner.jpg',left,right,width,height)
#插入表格 rows =2 cols =2 left =Inches(2) right =Inches(1) width =Inches(6) height =Inches(4) table =slide.shapes.add_table(rows,cols,left,right,width,height).table table.columns[0].width=Inches(1) table.columns[1].width=Inches(3) table.cell(0,0).text = '1' table.cell(0,1).text = '2' table.cell(1,0).text = '3' table.cell(1,1).text = '4'
PPT实现可视化
准备:pip install matplotlib
import matplotlib.pyplot as plt plt.rcParams['font.sans-serif'] = ['SimHei'] #不用理解用就行了 用来正常显示中文标签
准备一些基础数据
date =['2018/7/21','2018/7/22','2018/7/23','2018/7/24','2018/7/25'] hebei = [69,93,65,66,70] shanxi =[36,37,41,38,36]
#折线图 # plt.plot(date,hebei,color='red',label ='河北') # plt.plot(date,shanxi,color='red',label ='山西') # plt.title('每日入库量对比') # plt.xlabel('日期') # plt.ylabel('车次') # plt.legend() #刻印 即画图 # plt.show()
#柱形图 plt.bar(date,hebei,color='red',label ='河北',width=0.2) plt.title('每日入库量对比') plt.xlabel('日期') plt.ylabel('车次') plt.legend() #刻印 即画图 plt.show()
#柱形图 plt.barh(date,hebei,color='red',label ='河北',width=0.2) plt.title('每日入库量对比') plt.xlabel('日期') plt.ylabel('车次') plt.legend() #刻印 即画图 plt.show()
#双柱形图
x =range(len(date)) plt.bar(date,hebei,color='red',label ='河北',width=0.2) # 向右移动0.2, 柱状条宽度为0.2 plt.bar([i + 0.2 for i in x], shanxi,label ='山西', width=0.2) # 底部汉字移动到两个柱状条中间(本来汉字是在左边蓝色柱状条下面, 向右移动0.1) plt.xticks([i + 0.1 for i in x], date) plt.title('每日入库量对比') plt.xlabel('日期') plt.ylabel('车次') plt.legend() #刻印 即画图 plt.show()
#饼状图 number =[666,334,888] province =['河北','山西','浙江'] color=['#999fff','#fff999','#999999'] plt.pie(x=number,labels=province,colors=color) plt.legend() #刻印 即画图 plt.show()
pic_path='D:/autoOffice/plt.png' plt.savefig(pic_path) #插入图片文档py文件中 slide.shapes.add_picture(pic_path, left, right, width, height)