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='標(biāo)題1'
#?slide.shapes.placeholders[1].text='標(biāo)題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
#?#填充內(nèi)容
#?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='班級學(xué)生信息'
#?print(cell.is_merge_origin)#單元格是否合并
#?cell.split()#取消合并
#?print(cell.is_merge_origin)
#?#寫入圖表
#?chart_data=CategoryChartData()
#?3.保存
prs.save('newtest.pptx')
2020-06-19
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、問題產(chǎn)生的原因:具體可以訪問這里查看,有作者回復(fù)?https://github.com/scanny/python-pptx/issues/67