1.安装
#pip install python-pptx -i https://pypi.tuna.tsinghua.edu.cn/simple
#pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple
2.代码
复制幻灯片文字.py
# 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 = [] #记录所有元素 #写入表头 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 rander_template_with_new(self,slide,zhangshu): #一页幻灯片中所有元素 shape_list_one_slide = slide.shapes # all_yuansu_list = [] for k in range(0,len(shape_list_one_slide)): 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) 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) 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) 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) except Exception as e: print("报错:%s" % e) self.all_yuansu_list.append(one_yuansu_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) 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__": ppt_name = "muban.pptx" # tihuan_content_list = [a for a in range(0,1000)] tihuan_content_list = [] #读取excel中的数据作为替换字符列表 mubiao_data = pd.read_excel("shuju.xls") print(mubiao_data["赋值"]) #赋值 为表格第一行的一个表头 print(type(mubiao_data["赋值"])) for i in range(0,151): print(mubiao_data.iloc[i,6]) #用iloc读取表格中的行、列数据 tihuan_content_list.append(mubiao_data.iloc[i,6]) #添加到列表中 print(tihuan_content_list) new_ppt_name = "new.pptx" hcp = HandleCopyPPT(ppt_name,tihuan_content_list,new_ppt_name) hcp.rander_many()
operation_excel.py
import xlrd #导入xlrd from xlutils.copy import copy #导入excel 复制函数 class OperationExcel: def __init__(self,file_name=None,sheet_id=None): if file_name: self.file_name = file_name else: self.file_name = '../data/exceldata/测试用例模板.xls' if sheet_id: self.sheet_id = sheet_id else: self.sheet_id = 0 self.data = self.get_data() #获取sheet表 #获取sheet的内容 def get_data(self): data = xlrd.open_workbook(self.file_name) # 打开excel文件 print("打开[%s]文件"% self.file_name) tables = data.sheets()[self.sheet_id] #sheet_id从0开始 print("遍历第%s个sheet表" % self.sheet_id) return tables #获取单元格的行数 def get_lines(self): tables = self.data print("获取到单元格的行数为%s"% tables.nrows) return tables.nrows #获取某一个单元格的内容 def get_cell_value(self,row,col): print("获取【%s】行【%s】列的内容" % (row+1,col+1)) return self.data.cell_value(row,col) #写入数据 def write_value(self,row,col,value): """ 写入excel数据 """ read_data = xlrd.open_workbook(self.file_name) #读到excel write_data = copy(read_data) #复制excel sheet_data = write_data.get_sheet(0) #得到excel中的sheet表中的第一个sheet表 sheet_data.write(row,col,value) #写入数据 write_data.save(self.file_name) #保存表 #根据对应的caseid找到对应行的内容 def get_rows_data(self,case_id): row_num = self.get_row_num(case_id) #先根据case_id拿到行号 rows_data = self.get_row_values(row_num) #再根据行号获取该行的内容 return rows_data #根据对应的caseid找到对应的行号 def get_row_num(self,case_id): num = 0 #默认行号等于0 clols_data = self.get_cols_data() #获取某一列的内容 for col_data in clols_data: #循环 if case_id in col_data: #如果case_id等于某一列的数据,则返回该列的行数 return num num = num + 1 #如果没有找到,行号自增1 #根据行号找到该行的内容 def get_row_values(self,row): tables = self.data row_data = tables.row_values(row) return row_data #获取某一列的内容 def get_cols_data(self,col_id=None): #col_id=None,将col_id弄成一个可选参数 if col_id !=None: cols = self.data.col_values(col_id) #如果col_id 不为空,则返回col_id 的内容 else: cols = self.data.col_values(0) #否则默认返回第一行的内容 return cols if __name__ == '__main__': opers = OperationExcel() #实例化 print(opers.get_cell_value(1,1)) #打印表的行数
3.替换数据的excel样式
标签:run,name,python,self,list,复制,print,data,幻灯片 From: https://www.cnblogs.com/jingzaixin/p/17254782.html