
# https://www.globalsino.com/ICs/
# Merge two ppt files (with images only by inputing images into a file from another file).

import pptx
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
import os
import time
from pptx.util import Inches, Pt

filename = r"C:\0Python\pptxTest\pptxTest1.pptx" # Target file here.
pres2 = r"C:\0Python\pptxTest\pptxTest2.pptx"
folderSavedImage = r"C:\GlobalSino2\ICs"

theDirectory = os.path.dirname(filename)
print("theDirectory is:: ", theDirectory)

# Refer to https://www.globalsino.com/ICs/page4329.html
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(pres2)):
    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)
    prs1 = Presentation(filename)
    fileSize = os.path.getsize(theImageFilePath)
    sl = prs1.slides.add_slide(prs1.slide_layouts[6])
    top = Inches(0.2)
    left = Inches(0.1)
    def theHeightFunc(fileSize):
        global height
        theHeight = 4.5*abs((fileSize-21718)/(838177-21718))
        print(theHeight)
        if theHeight <4:
            height = Inches(4)
            print(height)
        else:
            height = Inches(theHeight)
    theHeightFunc(fileSize)
    print(height)
    with open(theImageFilePath, '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")    
    pic = sl.shapes.add_picture(theImageFilePath,left,top, height=height)        
    prs1.save(filename)
    time.sleep(6)


