Python代码创建一个幻灯片中有两个图像的ppt



当我运行附加的代码时,没有打开任何演示文稿,因为我是python的新手,我不知道我做错了什么,我在各自的文件夹中有名称为eagle_1和hawk_1的图像

from pptx import Presentation
from pptx.util import Inches
from pptx.util import Inches
from glob import glob
import os
img_path = 'D:/Pyhtoncheck'
def iter_image_pairs():
prs = Presentation()
content_slide_layout = prs.slide_layouts[6]
eagles, hawks = [], []
for image_path in glob.glob(img_path + '/*.png'):
if "eagle" in image_path:
eagles.append(image_path)
elif "hawk" in image_path:
hawks.append(image_path)
for pair in zip(eagles, hawks):
yield pair
for eagle, hawk in iter_image_pairs():
slide = prs.slides.add_slide(content_slide_layout)   
slide.shapes.add_picture(eagle, left=Inches(0), top=Inches(0), width=Inches(3), height=Inches(3))
slide.shapes.add_picture(hawk, left=Inches(2), top=Inches(2), width=Inches(3), height=Inches(3))
prs.save("eagle_hawk.pptx") 
os.startfile("eagle_hawk.pptx")

我同意@carlo的评论。你好,pavankonchada,你最好把大象分解成小块,然后自己一步一步地调试,这样可以让你更好地理解代码。

试试下面这个:

from pptx import Presentation
from pptx.util import Inches
from pptx.util import Inches
from glob import glob
img_path = 'D:/Pyhtoncheck'
def iter_image_pairs():
eagles, hawks = [], []
for image_path in glob(img_path + '/*.png'):
if "eagle" in image_path:
eagles.append(image_path)
if "hawk" in image_path:
hawks.append(image_path)
for pair in zip(eagles, hawks):
yield pair
prs = Presentation()
content_slide_layout = prs.slide_layouts[6]
for eagles, hawks in iter_image_pairs():
slide = prs.slides.add_slide(content_slide_layout)
slide.shapes.add_picture(eagles, left=Inches(0), top=Inches(0), width=Inches(3), height=Inches(3))
slide.shapes.add_picture(hawks, left=Inches(2), top=Inches(2), width=Inches(3), height=Inches(3))
prs.save("eagle_hawk.pptx")

我在我的笔记本电脑上测试了一下,效果很好。希望能有所帮助。我去掉了你的最后一行,并根据错误做了一些调整。如果您对上面的代码有任何疑问,请告诉我。

最新更新