'Python - pptx module - how to add image

I would like to add image to ppt using module pptx của python, below is my code:

from pptx import Presentation
#for picture must import pptx.util
from pptx.util import Inches
pr1 = Presentation()
#slide1
slide1_reg = pr1.slide_layouts[1]
sl1 = pr1.slides.add_slide(slide1_reg)




#add title
title1 = sl1.shapes.title
title1.text ="FEB-13 - LAB PPTX adding bullet point"
#adding bullet point 
bl = sl1.shapes 
bl_lv1 = bl.placeholders[1]
bl_lv1.text = "adding bullet 1"
#adding bullet point 2 is paragraph
#bl_lv2 = bl1.text_frame.add_paragraph()
bl_lv2 = bl_lv1.text_frame.add_paragraph()
bl_lv2.text= "to"
bl_lv2.level=1
bl_lv3 = bl_lv1.text_frame.add_paragraph()
bl_lv3.text= "test lv2"
bl_lv3.level=2
bl_lv4 = bl_lv1.text_frame.add_paragraph()
bl_lv4.text= "test lv3"
bl_lv4.level=3

slide2_reg = pr1.slide_layouts[5]
sl2 = pr1.slides.add_slide(slide2_reg)
title2 = sl2.shapes.title
title2.text = "Insert Picture to slide2"
#add image
img1 = "pic1.jpg"
from_left = Inches(3)
from_right = Inches(2)
add_picture = sl2.shapes.add_picture(img1,from_left,from_right)
pr1.save("Feb-13-pptx_lab.pptx")

pic1.jpg is located at the same location with this file , but i got the error below:

Traceback (most recent call last): File "c:\Users\xxx\Documents\GitHub\learning\python\pptx\Feb-13-pptx_lab.py", line 39, in
add_picture = sl2.shapes.add_picture(img1,from_left,from_right) File "C:\ProgramData\Anaconda3\lib\site-packages\pptx\shapes\shapetree.py", line 332, in add_picture
image_part, rId = self.part.get_or_add_image_part(image_file) File "C:\ProgramData\Anaconda3\lib\site-packages\pptx\parts\slide.py", line 39, in get_or_add_image_part image_part = self._package.get_or_add_image_part(image_file) File "C:\ProgramData\Anaconda3\lib\site-packages\pptx\package.py", line 36, in get_or_add_image_part
return self._image_parts.get_or_add_image_part(image_file) File "C:\ProgramData\Anaconda3\lib\site-packages\pptx\package.py", line 151, in get_or_add_image_part
image = Image.from_file(image_file) File "C:\ProgramData\Anaconda3\lib\site-packages\pptx\parts\image.py", line 162, in from_file with open(image_file, "rb") as f: FileNotFoundError: [Errno 2] No such file or directory: 'pic1.jpg' PS C:\Users\xxx\Documents\GitHub\learning\python>

could you please help assist on this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source