
# https://www.globalsino.com/ICs/
# Find image names in a pptx file.

import pptx
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
import os
import time

filename = r"C:\0Python\pptxTest\pptxTest2.pptx"
folderSavedImage = r"C:\GlobalSino2\ICs"

theDirectory = os.path.dirname(filename)
print("theDirectory is:: ", theDirectory)


def picture_shapes(prs):
    print("picture_shapes(prs) is :: ", picture_shapes(prs)) 
    for slide in prs.slides:
        for shape in slide.shapes:
            if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
                yield shape

for pic in picture_shapes(Presentation(filename)):
    image = pic.image
    # Get image "file" contents
    image_bytes = image.blob
    # Image is saved into the program folder automatically.
    image_filename = 'image.%s' % image.ext
    theImageFilePath = os.path.join(folderSavedImage, image_filename)
    print("image_filename is:: ", image_filename)
    print("theImageFilePath is:: ", theImageFilePath)
    print("The file size of the image is: ", os.path.getsize(theImageFilePath))
    # The following "with open" is needed to update the files in the image folder 
    with open(image_filename, 'wb') as f:
        f.write(image_bytes)
        # print(image_bytes) # Too many print lines
        print("f.write(image_bytes) is ", f.write(image_bytes))        
        # Here can get the correct image        
        print("\n")
    os.startfile(theImageFilePath)
    time.sleep(20)        
        
