
# https://www.globalsino.com/ICs/
# Merge the pptx files with any words in a sentence.

import re
import os
from os.path import exists
#=====================
import pptx
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
import time
from pptx.util import Inches, Pt

theSentense = "import transparent win32com.client, PPTtestWorldJobDescription, outlook = win32com.client.Dispatch('Outlook.Application').GetNamespace('MAPI') PPTXtest gal = outlook.Session.GetGlobalAddressList() Slide2 screenCanvas entries = gal.AddressEntries for e in entries: Original Test theHeightFunc  14  test3   user = e.GetExchangeUser()     if user is not None:         print(user.Name)        print(user.FirstName)        print(user.LastName) hello<one!two>three.four!five'six"
# The file below is the one which the pptx files with the kewwords in the sentence above are merged into.
filename = r"C:\0Python\pptxTest\TheReport.pptx" # Target file here.


theFolder = r"C:\0Python\pptxTest"
MyNewSentense = re.split(r'[`!@#$%^&*()_+\-=\[\]{};\':"\\|,.<>\/?~]', theSentense)
newItem = " "
#=====================
# pres2 = r"C:\0Python\pptxTest\pptxTest2.pptx"
pres2FilePath = r"C:\0Python\pptxTest"
folderSavedImage = r"C:\GlobalSino2\ICs"

def MergeTwoFiles(pres2FileName):
    theDirectory = os.path.dirname(filename)
    print("theDirectory is:: ", theDirectory)
    pres2 = os.path.join(pres2FilePath, pres2FileName+".pptx")

    # 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)
        print("theImageFilePath is:: ", theImageFilePath)
        if exists(theImageFilePath) == True:
            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)
        else:
            print("Cannot find the image file.")
        time.sleep(6)

#==========================

AllTheItemsInNewList = MyNewSentense[0:1]
[AllTheItemsInNewList.extend((newItem, x)) for x in MyNewSentense[1:]]
# Merge all items
theNewList = newItem.join(AllTheItemsInNewList[:])

wordsInTheNewList = theNewList.split()
print("wordsInTheNewList is:")
print(wordsInTheNewList)
# For the code lines above, refer to https://www.globalsino.com/ICs/page4331.html

print("\n")
CombinedList =[]
for root, dirs, files in os.walk(theFolder):
    for file in files:
        filePath = os.path.join(root, file)
        if filePath.endswith(".pptx") == True:
            pptxElement = file.split(".")[0]
            # If a pptx file in the folder "theFolder", then print the file name.
            if pptxElement in wordsInTheNewList:
                print(pptxElement)
                CombinedList.append(pptxElement)
print(CombinedList)
for i in range(len(CombinedList)):
    print("CombinedList[0] is:: ", CombinedList[0])
    MergeTwoFiles(CombinedList[0])    
    del(CombinedList[0])
    print("CombinedList is:: ", CombinedList) # List is empty at final/this step
                

            
            
            

