手把手教你python自动化办公(三)---PPT批量修改
PPT批量修改
场景模拟:当公司让你制作10000个不同数据但背景相同的PPT时,你是干上三天,还是小手一挥,十秒搞定?
1.设计你想要的PPT模板
2.将需要批量更换的数据填入Excel
3.点击–PPT批量修改1.py
paths = sys.path[0]
prs = Presentation(paths+'\\test0.pptx')
text_runs = []
wb = load_workbook(paths+"\\testppt.xlsx")
ws = wb.active
data={}
print("你好")
slide0 = prs.slides[0]
for row in range(1,ws.max_row+1):
carnum = ws['a' + str(row)].value
swjnum = ws['b' + str(row)].value
data.setdefault(carnum,[])
data[carnum].append(swjnum)
print(data)
blank_slide_layout=prs.slide_layouts[0]
template = prs.slides[0]
for carnum in data:
for swjnum in data[carnum]:
copied_slide = prs.slides.add_slide(blank_slide_layout)
copied_slide.shapes.element.clear()
for shp in template.shapes:
el = shp.element
newel = copy.deepcopy(el) copied_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
a=template.shapes[7].text_frame
a.text = carnum
a.fit_text(font_family='Calibri', max_size=91.2, bold=True, italic=False, font_file=None)
b=template.shapes[8].text_frame
b.text= swjnum
b.fit_text(font_family='Calibri', max_size=31.9, bold=True, italic=False, font_file=None)
prs.save(paths+'\\test1.pptx')