# encoding=utf8 #-*-coding:utf-8 -*- #pip install python-pptx -i https://pypi.tuna.tsinghua.edu.cn/simple #pip install python-docx -i https://pypi.tuna.tsinghua.edu.cn/simple #pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple from pptx import Presentation #导入PPT库 from pptx.util import Cm,Pt #导入单位 Cm,Pt from docx import Document #导入word库 import pandas as pd #导入pandas from pptx import Presentation, util from pptx.util import Pt, Cm from pptx.shapes.picture import Picture from 制作PPT.util.operation_excel import OperationExcel class HandleCopyPPT(object): def __init__(self,ppt_name,tihuan_content_list,new_ppt_name): self.new_ppt_name = new_ppt_name self.tihuan_content_list = tihuan_content_list self.tihuan_content_xiabiao = 0 self.excel_hangshu = 1 #从第二行开始写,第一行写入标题 self.ppt_name = ppt_name self.ppt = Presentation(self.ppt_name) self.oe = OperationExcel(file_name="hh.xls", sheet_id=None) self.all_yuansu_list = [] #记录所有元素 self.shijiyuansu_list = [] #记录实际元素 self.youyilou_list = [] #记录有遗漏的元素 #写入表头 def write_excel_title(self): biaoti_list = ['第几张PPT',"元素名字", "元素原始文本内容", "元素原始文本长度", "元素新赋值文本内容"] for i in range(0, len(biaoti_list)): self.oe.write_value(row=0, col=i, value=biaoti_list[i]) def inOneListNotInTwoList(self,one_list,two_list): chayi_list = [] for one in one_list: if one not in two_list: chayi_list.append(one) print("在第一个列表中,不在第二个列表中的元素:") print(chayi_list) return chayi_list #处理一页幻灯片,传入替换内容列表 def rander_template_with_new(self,slide,zhangshu): #一页幻灯片中所有元素 shape_list_one_slide = slide.shapes # all_yuansu_list = [] yuansu_geshu = len(shape_list_one_slide) print("幻灯片中存在元素个数为:%s" % yuansu_geshu) for k in range(0,yuansu_geshu): one_shape = shape_list_one_slide[k] shape = one_shape print("处理幻灯片中第%s个元素" % str(k+1)) shape_name = shape.name print("幻灯片中第%s个元素 的名字是 %s" % (str(k + 1),str(shape_name))) one_yuansu_list = [] one_yuansu_list.append(zhangshu) one_yuansu_list.append(shape_name) self.shijiyuansu_list.append(shape_name) # try: # shape_text = shape.text # # except Exception as e: # shape_text = e # one_yuansu_list.append(shape_name) # self.all_yuansu_list.append(one_yuansu_list) try: if shape.has_text_frame == True: print("==========================文本框=============================") duanluo_list = shape.text_frame.paragraphs #段落列表 duanluo_shu = len(duanluo_list) #段落个数 print("段落长度:", duanluo_shu) for n in range(0,duanluo_shu): one_paragraph = duanluo_list[n] paragraph = one_paragraph print("处理幻灯片中第%s个元素的第%s段落" % (str(k + 1),str(n+1))) run_list = paragraph.runs #段落的run的列表 run_shu = len(run_list) for l in range(0,run_shu): one_run_list = [] one_run_list.append(zhangshu) #统计张数,第几张幻灯片 one_run_list.append(shape_name) # 元素名字 print("处理幻灯片中第%s个元素的第%s段落的第%s个run" % (str(k + 1), str(n + 1),str(l+1))) one_run = run_list[l] run = one_run yuan_run_text = run.text print("幻灯片中第%s个元素的第%s段落的第%s个run的原始文本内容为 %s" % (str(k + 1), str(n + 1), str(l + 1),yuan_run_text)) one_run_list.append(yuan_run_text) #统计元素原始文本 yuanshi_text_zijie_changdu = len(yuan_run_text) print("幻灯片中第%s个元素的第%s段落的第%s个run的原始文本内容长度 %s" % (str(k + 1), str(n + 1), str(l + 1),yuanshi_text_zijie_changdu)) one_run_list.append(yuanshi_text_zijie_changdu) # 统计元素原始文本的长度 xinfuzhi = self.tihuan_content_list[self.tihuan_content_xiabiao] self.tihuan_content_xiabiao = self.tihuan_content_xiabiao+1 #赋值之后就加1 run.text = str(xinfuzhi) print("幻灯片中第%s个元素的第%s段落的第%s个run的新文本内容为 %s" % (str(k + 1), str(n + 1), str(l + 1), xinfuzhi)) one_run_list.append(xinfuzhi) # 统计元素文本新内容 self.all_yuansu_list.append(one_run_list) print("符合条件添加成功") self.youyilou_list.append(shape_name) elif shape.has_table == True: print("==========================表格==============================") one_table_data = [] for row in shape.table.rows: # 读每行 row_data = [] for cell in row.cells: # 读一行中的所有单元格 cell.text = cell.text if cell.text != "" else "未填写" c = cell.text row_data.append(c) one_table_data.append(row_data) # 把每一行存入表 # 用二维列表输出表格行和列的数据 print(one_table_data) print("第一个单元格内容:", shape.table.rows[0].cells[0].text) # self.all_yuansu_list.append(one_yuansu_list) # self.youyilou_list.append(shape_name) elif isinstance(shape, Picture): print("==========================图片==============================") index = 0 with open(f'{index}.jpg', 'wb') as f: f.write(shape.image.blob) index += 1 # self.all_yuansu_list.append(one_yuansu_list) # self.youyilou_list.append(shape_name) else: # self.all_yuansu_list.append(one_yuansu_list) # self.youyilou_list.append(shape_name) print("不符合条件添加成功") except Exception as e: print("报错:%s" % e) # self.all_yuansu_list.append(one_yuansu_list) # self.youyilou_list.append(shape_name) print("异常添加成功") print("实际元素:") print(self.shijiyuansu_list) print("有遗漏的元素:") print(self.youyilou_list) self.inOneListNotInTwoList(one_list=self.shijiyuansu_list, two_list=self.youyilou_list) #插入图片 def insert_pic(self,slide): img_path = '1.jpg' # 图片路径 # 设置图片的位置和大小 left = util.Cm(8.04) top = util.Cm(9.93) width = util.Cm(15.07) height = util.Cm(4.06) # 在页面中插入图片 slide.shapes.add_picture(img_path, left, top, width, height) #处理多张幻灯片 def rander_many(self): self.write_excel_title() #写入标题 all_slide_list = self.ppt.slides #获取所有幻灯片 len_all_slide_list = len(all_slide_list) print("替换后的列表内容") print(self.tihuan_content_list) for i in range(0,len_all_slide_list): print("处理第%s张幻灯片"% str(i+1)) one_slide = all_slide_list[i] self.rander_template_with_new(slide=one_slide,zhangshu=i+1) #写入excel中 print("self.all_yuansu_list:") for one in self.all_yuansu_list: print(one) one_yuansu_list = one #写入excel中 for f in range(0,len(one_yuansu_list)): self.oe.write_value(row=self.excel_hangshu, col=f, value=one_yuansu_list[f]) self.excel_hangshu = self.excel_hangshu + 1 len_all_yuansu_list = len(self.all_yuansu_list) print("幻灯片总共个有%s个元素" % str(len_all_yuansu_list)) self.ppt.save(self.new_ppt_name) if __name__ == "__main__": all_data_list = [] excel_data = pd.read_excel("副本49过程定义作用时间.xlsx", header=None) # pandas 读取excel中的数据 ,header=None,表示不要读取表头 print(excel_data) print("excel_data类型:") print(type(excel_data)) print(excel_data.iloc[0, 0]) # excel_data.iloc[0,0],表示获取数据中的第0行第0列的数据 for i in range(1,50): one_list = [] for j in range(0,6): print(excel_data.iloc[i,j]) one_list.append(excel_data.iloc[i,j]) print(one_list) #一个开始 ppt_name = "一页模板.pptx" # # tihuan_content_list = [a for a in range(0,1000)] # # tihuan_content_list = [] # # data_excel_name = "gjjs.xls" # #读取excel中的数据作为替换字符列表 # mubiao_data = pd.read_excel(data_excel_name) # print(mubiao_data["赋值"]) #赋值 为表格第一行的一个表头 # print(type(mubiao_data["赋值"])) # print(mubiao_data) # for i in range(0,6): # print("第%s数据:"% str(i)) # tihuanshuju = mubiao_data.iloc[i,5] # print(tihuanshuju ) #用iloc读取表格中的行、列数据 # tihuan_content_list.append(tihuanshuju ) #添加到列表中 fangfa = str(i) tihuan_content_list =[] tihuan_content_list.append("49个管理过程") tihuan_content_list.append("49 management processes") tihuan_content_list.append("COMPANY INTRODUCE") tihuan_content_list.append(one_list[3]) tihuan_content_list.append(one_list[4]) tihuan_content_list.append("%s%s"%(one_list[1],one_list[2])) print(tihuan_content_list) new_ppt_name = "%s.pptx" % fangfa hcp = HandleCopyPPT(ppt_name,tihuan_content_list,new_ppt_name) hcp.rander_many() #一个结束
标签:name,python,self,list,shape,append,print,替换,幻灯片 From: https://www.cnblogs.com/jingzaixin/p/17290898.html