from win32com.client import Dispatch
path = "D:\PROGRAMMING\PythonCode\pypptx\image1.png"
ppt = Dispatch('PowerPoint.Application')
slide = ppt.Presentations.Open('D:\PROGRAMMING\PythonCode\pypptx\example.pptx')
slide.Slides.Add(1, 1)
slide.Slides(1).Shapes(1).TextFrame.TextRange.Text = 'Hello, World!'
slide.Slides(1).Shapes(2).TextFrame.TextRange.Text = 'This is a PowerPoint presentation.'
slide.Slides.Add(2, 1)
slide.Slides(2).Shapes(1).TextFrame.TextRange.Text = 'hrerted!'
slide.Slides.Add(3, 1)
slide.Slides(3).Shapes.AddPicture(FileName=path, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=-1, Height=-1)
slide.Save()
slide.Close()
ppt.Quit()
from pptx import Presentationfrom pptx.util import Inches
pptx_path = "D:\PROGRAMMING\PythonCode\pypptx\example.pptx"
presentation = Presentation(pptx_path)
slide = presentation.slides[-1]
image_path = "D:\PROGRAMMING\PythonCode\pypptx\image1.png"
left = Inches(1)
top = Inches(1)
width = Inches(5)
height = Inches(3)
slide.shapes.add_picture(image_path, left, top, width, height)
output_path = "D:\PROGRAMMING\PythonCode\pypptx\example.pptx"
presentation.save(output_path)
print("圖片已插入並保存簡報。")