import pptx from pptx.util import Inches,Pt from pptx.enum.shapes import MSO_SHAPE from pptx.dml.color import RGBColor # 1.得到ppt prs = pptx.Presentation('newtest.pptx') # 2.写入 slide=prs.slides.add_slide(prs.slide_layouts[0]) # prs.slides.add_slide(prs.slide_layouts[1]) # prs.slides.add_slide(prs.slide_layouts[2]) # prs.slides.add_slide(prs.slide_layouts[3]) # prs.slides.add_slide(prs.slide_layouts[4]) # 删除 print(len(prs.slides)) del prs.slides._sldIdLst[1] print(len(prs.slides)) # text1 = slide.shapes.add_textbox(Inches(5), Inches(5), Inches(5), Inches(5)) # text1.text = "我是文本框" # p1 = text1.text_frame.add_paragraph() # p1.text = '我是段落1' # p1.add_run().text = 'end' # title_shape=slide.shapes.title # title_shape.text='标题1' # slide.shapes.placeholders[1].text='标题2' # #添加自选图形 # shape=slide.shapes.add_shape(MSO_SHAPE.RECTANGLE,Inches(2),Inches(2),Inches(5),Inches(3)) # #填充,边框 # fill=shape.fill # fill.solid() # fill.fore_color.rgb=RGBColor(255,0,0) # line=shape.line # line.color.rgb=RGBColor(55,3,5) # line.width=Pt(2) # #添加表格 # table=slide.shapes.add_table(2,3,Inches(2),Inches(2),Inches(4),Inches(2)).table # #填充内容 # table.cell(0,0).text='name' # table.cell(0,1).text='age' # table.cell(0,2).text='class' # table.cell(1,0).text='张三' # table.cell(1,1).text='19' # table.cell(1,2).text='一班' # #合并单元格 # cell=table.cell(0,0) # cell1=table.cell(0,2) # cell.merge(cell) # table.cell(0,0).text='班级学生信息' # print(cell.is_merge_origin)#单元格是否合并 # cell.split()#取消合并 # print(cell.is_merge_origin) # #写入图表 # chart_data=CategoryChartData() # 3.保存 prs.save('newtest.pptx')
1、解决方式,尝试下面这段代码
# 删除
print(len(prs.slides))
rId = prs.slides._sldIdLst[1].rId
prs.part.drop_rel(rId)
del prs.slides._sldIdLst[1]
print(len(prs.slides))
2、问题产生的原因:具体可以访问这里查看,有作者回复 https://github.com/scanny/python-pptx/issues/67