
# https://www.globalsino.com/ICs/indexC.html
# Create a pptx file

from pptx import Presentation
# "Inches" helps the layout of pictures where you want.
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
import os
import cv2
from os.path import exists
import time

Path0 = r"C:\0Python\images"

def SlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup, Rotation):
    ImageForSizeMeasurement = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg") # ==================== Needed to be replaced
    #ImageForSizeMeasurement = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
    print("File (FileListToSendToPPTpreparation[0]) used for size measure is ", FileListToSendToPPTpreparation[0])
    im = cv2.imread(ImageForSizeMeasurement)
    
    h = im.shape[0]
    w = im.shape[1]
    im_real_height = h*1
    im_real_width = w*1 # ============ Temporary factor is used    
    print(" im_real_height and im_real_width are ", im_real_height, im_real_width)
    height_or_width = 2 # =================== Does this need to be changed?
    half_w = im_real_width*height_or_width/im_real_height/2

    if im_real_width < 70 and im_real_height < 70:
        print("Rotation, im_real_width, im_real_height", Rotation, im_real_width, im_real_height) 
        def SixImagesWithoutScallingSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup, Rotation):
            if Rotation == 0:
                print("Rotation is", Rotation)
                def NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                    
                    pptFilePath = os.path.join(Path0, f"{JobNumber}_{JobSubNumber}_{JobDescription}.pptx")
                    print("The pptFilePath is ", pptFilePath)
                    if exists(pptFilePath) == True:
                        MyPresentation = Presentation(pptx = pptFilePath)
                    else:
                        MyPresentation = Presentation()

                    def NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                        ImageNoneRotatonOffsetX1 = 3.4
                        ImageNoneRotatonOffsetX2 = 5.5
                        ImageNoneRotatonOffsetX3 = 7.7
                        ImageNoneRotatonOffsetY1 = 1.1
                        ImageNoneRotatonOffsetY2 = 3.2
                        ImageNoneRotatonOffsetY3 =  5.3

                        TEXTsetNoneRotatonX1 = 3.4
                        TEXTsetNoneRotatonX2 = 5.6
                        TEXTsetNoneRotatonX3 = 7.7
                        TEXTsetNoneRotatonY1 = 1.1
                        TEXTsetNoneRotatonY2 = 3.2
                        TEXTsetNoneRotatonY3 = 5.3   

                        print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        print("thePathOfTheCurrentImagesGroup is ", thePathOfTheCurrentImagesGroup)
                        print("FileListToSendToPPTpreparation[0]", FileListToSendToPPTpreparation[0])
                        # Check if the ppt exist or not.
                    
                        # Add slide 3 and add pictures in the slides
                        Slide3_Register = MyPresentation.slide_layouts[5]
                        Slide3 = MyPresentation.slides.add_slide(Slide3_Register)
                        title3  = Slide3.shapes.title
                        title3.text = thePathOfTheCurrentImagesGroup.split("\\")[-1]

                        # Add a GPS image
                        # img0 = r"C:\0Python\images\TheJob\JobB\Word.jpg"
                        img0 = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
                        from_left_0 = Inches(0.12)
                        from_top_0 = Inches(2)
                        add_picture_0 = Slide3.shapes.add_picture(img0, from_left_0, from_top_0, width =Inches(3.3), height =None)

                        # Image 1 x 1
                        # First row of images
                        img1 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")        
                        print(img1, "in ppt3")
                        from_left_1 = Inches(ImageNoneRotatonOffsetX1)
                        from_top_1 = Inches(ImageNoneRotatonOffsetY1)
                        add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =Inches(2), height =None)
                        # text box 2
                        left_text_box2 = Inches(TEXTsetNoneRotatonX1)
                        top_text_box2 = Inches(TEXTsetNoneRotatonY1)
                        width_text_box2 = Inches(1.7)
                        height_text_box2 = Inches(0.4)
                        tb2 = Slide3.shapes.add_textbox(left_text_box2, top_text_box2, width_text_box2, height_text_box2)
                        tf2 = tb2.text_frame
                        p_0_2 = tf2.paragraphs[0]
                        run_p_0_2 = p_0_2.add_run()
                        run_p_0_2.text = FileListToSendToPPTpreparation[0]
                        font_p_0_2 = run_p_0_2.font
                        font_p_0_2.size = Pt(20)
                        font_p_0_2.name = 'arial'
                        font_p_0_2.bold = True
                        font_p_0_2.color.rgb = RGBColor (0, 255, 0)    

                        # delete used image
                        del FileListToSendToPPTpreparation[0]
                        print("FileListToSendToPPTpreparation after 1 x 1 is ", FileListToSendToPPTpreparation)
                        if FileListToSendToPPTpreparation:
                            
                            # Image 1 x 2
                            img2 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                            from_left_2 = Inches(ImageNoneRotatonOffsetX2)
                            from_top_2 = Inches(ImageNoneRotatonOffsetY1)
                            add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =Inches(2), height =None)
                            # tex box 1
                            left_text_box1 = Inches(TEXTsetNoneRotatonX2)
                            top_text_box1 = Inches(TEXTsetNoneRotatonY1)
                            width_text_box1 = Inches(1.7)
                            height_text_box1 = Inches(0.4)
                            tb1 = Slide3.shapes.add_textbox(left_text_box1, top_text_box1, width_text_box1, height_text_box1)
                            tf1 = tb1.text_frame
                            p_0_1 = tf1.paragraphs[0]
                            run_p_0_1 = p_0_1.add_run()
                            run_p_0_1.text = FileListToSendToPPTpreparation[0]
                            font_p_0_1 = run_p_0_1.font
                            font_p_0_1.size = Pt(20)
                            font_p_0_1.name = 'arial'
                            font_p_0_1.bold = True
                            font_p_0_1.color.rgb = RGBColor (0, 255, 0)        

                            # delete used image
                            del FileListToSendToPPTpreparation[0]
                            print("FileListToSendToPPTpreparation after 1 x 2 is ", FileListToSendToPPTpreparation)
                            if FileListToSendToPPTpreparation:

                                # Image 1 x 3
                                img3 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                from_left_3 = Inches(ImageNoneRotatonOffsetX3)
                                from_top_3 = Inches(ImageNoneRotatonOffsetY1)
                                add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =Inches(2), height =None)
                                 # text box 3
                                left_text_box3 = Inches(TEXTsetNoneRotatonX3)
                                top_text_box3 = Inches(TEXTsetNoneRotatonY1)
                                width_text_box3 = Inches(1.7)
                                height_text_box3 = Inches(0.4)
                                tb3 = Slide3.shapes.add_textbox(left_text_box3, top_text_box3, width_text_box3, height_text_box3)
                                tf3 = tb3.text_frame
                                p_0_3 = tf3.paragraphs[0]
                                run_p_0_3 = p_0_3.add_run()
                                run_p_0_3.text = FileListToSendToPPTpreparation[0]
                                font_p_0_3 = run_p_0_3.font
                                font_p_0_3.size = Pt(20)
                                font_p_0_3.name = 'arial'
                                font_p_0_3.bold = True
                                font_p_0_3.color.rgb = RGBColor (0, 255, 0)

                                # delete used image
                                del FileListToSendToPPTpreparation[0]
                                print("FileListToSendToPPTpreparation after 1 x 3 is ", FileListToSendToPPTpreparation)
                                if FileListToSendToPPTpreparation:
                               
                                    # Image 2 x 1
                                    img4 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                    from_left_4 = Inches(ImageNoneRotatonOffsetX1)
                                    from_top_4 = Inches(ImageNoneRotatonOffsetY2)
                                    add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =Inches(2), height =None)
                                    # text box 4
                                    left_text_box4 = Inches(TEXTsetNoneRotatonX1)
                                    top_text_box4 = Inches(TEXTsetNoneRotatonY2)
                                    width_text_box4 = Inches(1.7)
                                    height_text_box4 = Inches(0.4)
                                    tb4 = Slide3.shapes.add_textbox(left_text_box4, top_text_box4, width_text_box4, height_text_box4)
                                    tf4 = tb4.text_frame
                                    p_0_4 = tf4.paragraphs[0]
                                    run_p_0_4 = p_0_4.add_run()
                                    run_p_0_4.text = FileListToSendToPPTpreparation[0]
                                    font_p_0_4 = run_p_0_4.font
                                    font_p_0_4.size = Pt(20)
                                    font_p_0_4.name = 'arial'
                                    font_p_0_4.bold = True
                                    font_p_0_4.color.rgb = RGBColor (0, 255, 0)

                                    # delete used image
                                    del FileListToSendToPPTpreparation[0]
                                    print("FileListToSendToPPTpreparation after 2 x 1 is ", FileListToSendToPPTpreparation)
                                    if FileListToSendToPPTpreparation:
                                    
                                        # Image 2 x 2
                                        img5 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                        from_left_5 = Inches(ImageNoneRotatonOffsetX2)
                                        from_top_5 = Inches(ImageNoneRotatonOffsetY2)
                                        add_picture_5 = Slide3.shapes.add_picture(img5, from_left_5, from_top_5, width =Inches(2), height =None)
                                        # text box 5
                                        left_text_box5 = Inches(TEXTsetNoneRotatonX2)
                                        top_text_box5 = Inches(TEXTsetNoneRotatonY2)
                                        width_text_box5 = Inches(1.7)
                                        height_text_box5 = Inches(0.4)
                                        tb5 = Slide3.shapes.add_textbox(left_text_box5, top_text_box5, width_text_box5, height_text_box5)
                                        tf5 = tb5.text_frame
                                        p_0_5 = tf5.paragraphs[0]
                                        run_p_0_5 = p_0_5.add_run()
                                        run_p_0_5.text = FileListToSendToPPTpreparation[0]
                                        font_p_0_5 = run_p_0_5.font
                                        font_p_0_5.size = Pt(20)
                                        font_p_0_5.name = 'arial'
                                        font_p_0_5.bold = True
                                        font_p_0_5.color.rgb = RGBColor (0, 255, 0)

                                        # delete used image
                                        del FileListToSendToPPTpreparation[0]
                                        print("FileListToSendToPPTpreparation after 2 x 2 is ", FileListToSendToPPTpreparation)
                                        if FileListToSendToPPTpreparation:
                                        
                                            # Image 2 x 3
                                            img6 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                            from_left_6 = Inches(ImageNoneRotatonOffsetX3)
                                            from_top_6 = Inches(ImageNoneRotatonOffsetY2)
                                            add_picture_6 = Slide3.shapes.add_picture(img6, from_left_6, from_top_6, width =Inches(2), height =None)
                                            # text box 6
                                            left_text_box6 = Inches(TEXTsetNoneRotatonX3)
                                            top_text_box6 = Inches(TEXTsetNoneRotatonY2)
                                            width_text_box6 = Inches(1.7)
                                            height_text_box6 = Inches(0.4)
                                            tb6 = Slide3.shapes.add_textbox(left_text_box6, top_text_box6, width_text_box6, height_text_box6)
                                            tf6 = tb6.text_frame
                                            p_0_6 = tf6.paragraphs[0]
                                            run_p_0_6 = p_0_6.add_run()
                                            run_p_0_6.text = FileListToSendToPPTpreparation[0]
                                            font_p_0_6 = run_p_0_6.font
                                            font_p_0_6.size = Pt(20)
                                            font_p_0_6.name = 'arial'
                                            font_p_0_6.bold = True
                                            font_p_0_6.color.rgb = RGBColor (0, 255, 0)

                                            # delete used image
                                            del FileListToSendToPPTpreparation[0]
                                            print("FileListToSendToPPTpreparation after 2 x 3 is ", FileListToSendToPPTpreparation)
                                            if FileListToSendToPPTpreparation:
                                           
                                                # Image 3 x 1
                                                img7 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                                from_left_7 = Inches(ImageNoneRotatonOffsetX1)
                                                from_top_7 = Inches(ImageNoneRotatonOffsetY3)
                                                add_picture_7 = Slide3.shapes.add_picture(img7, from_left_7, from_top_7, width =Inches(2), height =None)
                                                # text box 7
                                                left_text_box7 = Inches(TEXTsetNoneRotatonX1)
                                                top_text_box7 = Inches(TEXTsetNoneRotatonY3)
                                                width_text_box7 = Inches(1.7)
                                                height_text_box7 = Inches(0.4)
                                                tb7 = Slide3.shapes.add_textbox(left_text_box7, top_text_box7, width_text_box7, height_text_box7)
                                                tf7 = tb7.text_frame
                                                p_0_7 = tf7.paragraphs[0]
                                                run_p_0_7 = p_0_7.add_run()
                                                run_p_0_7.text = FileListToSendToPPTpreparation[0]
                                                font_p_0_7 = run_p_0_7.font
                                                font_p_0_7.size = Pt(20)
                                                font_p_0_7.name = 'arial'
                                                font_p_0_7.bold = True
                                                font_p_0_7.color.rgb = RGBColor (0, 255, 0)

                                                # delete used image
                                                del FileListToSendToPPTpreparation[0]
                                                print("FileListToSendToPPTpreparation after 3 x 1 is ", FileListToSendToPPTpreparation)
                                                if FileListToSendToPPTpreparation:
                                                
                                                    # Image 3 x 2
                                                    img8 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                                    from_left_8 = Inches(ImageNoneRotatonOffsetX2)
                                                    from_top_8 = Inches(ImageNoneRotatonOffsetY3)
                                                    add_picture_8 = Slide3.shapes.add_picture(img8, from_left_8, from_top_8, width =Inches(2), height =None)
                                                    # text box 8
                                                    left_text_box8 = Inches(TEXTsetNoneRotatonX2)
                                                    top_text_box8 = Inches(TEXTsetNoneRotatonY3)
                                                    width_text_box8 = Inches(1.7)
                                                    height_text_box8 = Inches(0.4)
                                                    tb8 = Slide3.shapes.add_textbox(left_text_box8, top_text_box8, width_text_box8, height_text_box8)
                                                    tf8 = tb8.text_frame
                                                    p_0_8 = tf8.paragraphs[0]
                                                    run_p_0_8 = p_0_8.add_run()
                                                    run_p_0_8.text = FileListToSendToPPTpreparation[0]
                                                    font_p_0_8 = run_p_0_8.font
                                                    font_p_0_8.size = Pt(20)
                                                    font_p_0_8.name = 'arial'
                                                    font_p_0_8.bold = True
                                                    font_p_0_8.color.rgb = RGBColor (0, 255, 0)

                                                    # delete used image
                                                    del FileListToSendToPPTpreparation[0]
                                                    print("FileListToSendToPPTpreparation after 3 x 2 is ", FileListToSendToPPTpreparation)
                                                    if FileListToSendToPPTpreparation:
                                                        
                                                        # Image 3 x 3
                                                        img9 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                                        from_left_9 = Inches(ImageNoneRotatonOffsetX3)
                                                        from_top_9 = Inches(ImageNoneRotatonOffsetY3)
                                                        add_picture_9 = Slide3.shapes.add_picture(img9, from_left_9, from_top_9, width =Inches(2), height =None)
                                                        # text box 9
                                                        left_text_box9 = Inches(TEXTsetNoneRotatonX3)
                                                        top_text_box9 = Inches(TEXTsetNoneRotatonY3)
                                                        width_text_box9 = Inches(1.7)
                                                        height_text_box9 = Inches(0.4)
                                                        tb9 = Slide3.shapes.add_textbox(left_text_box9, top_text_box9, width_text_box9, height_text_box9)
                                                        tf9 = tb9.text_frame
                                                        p_0_9 = tf9.paragraphs[0]
                                                        run_p_0_9 = p_0_9.add_run()
                                                        run_p_0_9.text = FileListToSendToPPTpreparation[0]
                                                        font_p_0_9 = run_p_0_9.font
                                                        font_p_0_9.size = Pt(20)
                                                        font_p_0_9.name = 'arial'
                                                        font_p_0_9.bold = True
                                                        font_p_0_9.color.rgb = RGBColor (0, 255, 0)

                                                        del FileListToSendToPPTpreparation[0]
                                                        print("Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        
                                                        if FileListToSendToPPTpreparation:
                                                            print("============== Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                                                            NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                                                           
                    NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                    MyPresentation.save(pptFilePath)
                NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
            
            if Rotation == 90:
                print("Rotation is", Rotation)
                def NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                    ImageOffsetX1 = 3.4
                    ImageOffsetX2 = 5.5
                    ImageOffsetX3 = 7.7
                    ImageOffsetY1 = 1.1
                    ImageOffsetY2 = 3.2
                    ImageOffsetY3 =  5.3

                    TEXTsetX1 = 3.4
                    TEXTsetX2 = 5.6
                    TEXTsetX3 = 7.7
                    TEXTsetY1 = 1.2
                    TEXTsetY2 = 3.3
                    TEXTsetY3 = 5.4                 
                    
                    
                    pptFilePath = os.path.join(Path0, f"{JobNumber}_{JobSubNumber}_{JobDescription}.pptx")
                    print("The pptFilePath is ", pptFilePath)
                    if exists(pptFilePath) == True:
                        MyPresentation = Presentation(pptx = pptFilePath)
                    else:
                        MyPresentation = Presentation()

                    def NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                        print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        print("thePathOfTheCurrentImagesGroup is ", thePathOfTheCurrentImagesGroup)
                        print("FileListToSendToPPTpreparation[0]", FileListToSendToPPTpreparation[0])
                        # Check if the ppt exist or not.
                    
                        # Add slide 3 and add pictures in the slides
                        Slide3_Register = MyPresentation.slide_layouts[5]
                        Slide3 = MyPresentation.slides.add_slide(Slide3_Register)
                        title3  = Slide3.shapes.title
                        title3.text = thePathOfTheCurrentImagesGroup.split("\\")[-1]

                        # Add a GPS image
                        # img0 = r"C:\0Python\images\TheJob\JobB\Word.jpg"
                        img0 = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
                        from_left_0 = Inches(0.12)
                        from_top_0 = Inches(2)
                        add_picture_0 = Slide3.shapes.add_picture(img0, from_left_0, from_top_0, width =Inches(3.3), height =None)

                        # Image 1 x 1
                        # First row of images
                        img1 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")        
                        from_left_1 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                        from_top_1 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                        add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =None, height =Inches(height_or_width))
                        add_picture_1.rotation = 90


                  
                        # text box 1
                        left_text_box2 = Inches(TEXTsetX1)
                        top_text_box2 = Inches(TEXTsetY1)
                        width_text_box2 = Inches(1.7)
                        height_text_box2 = Inches(0.4)
                        tb2 = Slide3.shapes.add_textbox(left_text_box2, top_text_box2, width_text_box2, height_text_box2)
                        tf2 = tb2.text_frame
                        p_0_2 = tf2.paragraphs[0]
                        run_p_0_2 = p_0_2.add_run()
                        run_p_0_2.text = FileListToSendToPPTpreparation[0]
                        font_p_0_2 = run_p_0_2.font
                        font_p_0_2.size = Pt(20)
                        font_p_0_2.name = 'arial'
                        font_p_0_2.bold = True
                        font_p_0_2.color.rgb = RGBColor (0, 255, 0)    

                        # delete used image
                        del FileListToSendToPPTpreparation[0]
                        print("FileListToSendToPPTpreparation after 1 x 1 is ", FileListToSendToPPTpreparation)
                        if FileListToSendToPPTpreparation:
                            
                            # Image 1 x 2
                            img2 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                            #from_left_2 = Inches(5.7)
                            #from_top_2 = Inches(1.4)
                            #add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =Inches(2), height =None)
                            from_left_2 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                            from_top_2 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                            add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =None, height =Inches(height_or_width))
                            add_picture_2.rotation = 90
  
                            # tex box 2
                            left_text_box1 = Inches(TEXTsetX2)
                            top_text_box1 = Inches(TEXTsetY1)
                            width_text_box1 = Inches(1.7)
                            height_text_box1 = Inches(0.4)
                            tb1 = Slide3.shapes.add_textbox(left_text_box1, top_text_box1, width_text_box1, height_text_box1)
                            tf1 = tb1.text_frame
                            p_0_1 = tf1.paragraphs[0]
                            run_p_0_1 = p_0_1.add_run()
                            run_p_0_1.text = FileListToSendToPPTpreparation[0]
                            font_p_0_1 = run_p_0_1.font
                            font_p_0_1.size = Pt(20)
                            font_p_0_1.name = 'arial'
                            font_p_0_1.bold = True
                            font_p_0_1.color.rgb = RGBColor (0, 255, 0)        

                            # delete used image
                            del FileListToSendToPPTpreparation[0]
                            print("FileListToSendToPPTpreparation after 1 x 2 is ", FileListToSendToPPTpreparation)
                            if FileListToSendToPPTpreparation:

                                # Image 1 x 3
                                img3 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                #from_left_3 = Inches(7.8)
                                #from_top_3 = Inches(1.4)
                                #add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =Inches(2), height =None)
                                from_left_3 = Inches(ImageOffsetX3+(height_or_width/2)-half_w*0.9) ###
                                from_top_3 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                                add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =None, height =Inches(height_or_width))
                                add_picture_3.rotation = 90


                                 # text box 3
                                left_text_box3 = Inches(TEXTsetX3)
                                top_text_box3 = Inches(TEXTsetY1)
                                width_text_box3 = Inches(1.7)
                                height_text_box3 = Inches(0.4)
                                tb3 = Slide3.shapes.add_textbox(left_text_box3, top_text_box3, width_text_box3, height_text_box3)
                                tf3 = tb3.text_frame
                                p_0_3 = tf3.paragraphs[0]
                                run_p_0_3 = p_0_3.add_run()
                                run_p_0_3.text = FileListToSendToPPTpreparation[0]
                                font_p_0_3 = run_p_0_3.font
                                font_p_0_3.size = Pt(20)
                                font_p_0_3.name = 'arial'
                                font_p_0_3.bold = True
                                font_p_0_3.color.rgb = RGBColor (0, 255, 0)

                                # delete used image
                                del FileListToSendToPPTpreparation[0]
                                print("FileListToSendToPPTpreparation after 1 x 3 is ", FileListToSendToPPTpreparation)
                                if FileListToSendToPPTpreparation:
                               
                                    # Image 2 x 1
                                    img4 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                    #from_left_4 = Inches(3.5)
                                    #from_top_4 = Inches(3.5)
                                    #add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =Inches(2), height =None)
                                    from_left_4 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                                    from_top_4 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                    add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =None, height =Inches(height_or_width))
                                    add_picture_4.rotation = 90
                     
                                    # text box 4
                                    left_text_box4 = Inches(TEXTsetX1)
                                    top_text_box4 = Inches(TEXTsetY2)
                                    width_text_box4 = Inches(1.7)
                                    height_text_box4 = Inches(0.4)
                                    tb4 = Slide3.shapes.add_textbox(left_text_box4, top_text_box4, width_text_box4, height_text_box4)
                                    tf4 = tb4.text_frame
                                    p_0_4 = tf4.paragraphs[0]
                                    run_p_0_4 = p_0_4.add_run()
                                    run_p_0_4.text = FileListToSendToPPTpreparation[0]
                                    font_p_0_4 = run_p_0_4.font
                                    font_p_0_4.size = Pt(20)
                                    font_p_0_4.name = 'arial'
                                    font_p_0_4.bold = True
                                    font_p_0_4.color.rgb = RGBColor (0, 255, 0)

                                    # delete used image
                                    del FileListToSendToPPTpreparation[0]
                                    print("FileListToSendToPPTpreparation after 2 x 1 is ", FileListToSendToPPTpreparation)
                                    if FileListToSendToPPTpreparation:
                                    
                                        # Image 2 x 2
                                        img5 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                        from_left_5 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                        from_top_5 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                        add_picture_5 = Slide3.shapes.add_picture(img5, from_left_5, from_top_5, width =None, height =Inches(height_or_width))
                                        add_picture_5.rotation = 90  

                                        # text box 5
                                        left_text_box5 = Inches(TEXTsetX2)
                                        top_text_box5 = Inches(TEXTsetY2)
                                        width_text_box5 = Inches(1.7)
                                        height_text_box5 = Inches(0.4)
                                        tb5 = Slide3.shapes.add_textbox(left_text_box5, top_text_box5, width_text_box5, height_text_box5)
                                        tf5 = tb5.text_frame
                                        p_0_5 = tf5.paragraphs[0]
                                        run_p_0_5 = p_0_5.add_run()
                                        run_p_0_5.text = FileListToSendToPPTpreparation[0]
                                        font_p_0_5 = run_p_0_5.font
                                        font_p_0_5.size = Pt(20)
                                        font_p_0_5.name = 'arial'
                                        font_p_0_5.bold = True
                                        font_p_0_5.color.rgb = RGBColor (0, 255, 0)

                                        # delete used image
                                        del FileListToSendToPPTpreparation[0]
                                        print("FileListToSendToPPTpreparation after 2 x 2 is ", FileListToSendToPPTpreparation)
                                        if FileListToSendToPPTpreparation:
                                        
                                            # Image 2 x 3
                                            img6 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                            from_left_6 = Inches(ImageOffsetX3+(height_or_width/2)-half_w*0.9) ###
                                            from_top_6 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                            add_picture_6 = Slide3.shapes.add_picture(img6, from_left_6, from_top_6, width =None, height =Inches(height_or_width))
                                            add_picture_6.rotation = 90  

                                            # text box 6
                                            left_text_box6 = Inches(TEXTsetX3)
                                            top_text_box6 = Inches(TEXTsetY2)
                                            width_text_box6 = Inches(1.7)
                                            height_text_box6 = Inches(0.4)
                                            tb6 = Slide3.shapes.add_textbox(left_text_box6, top_text_box6, width_text_box6, height_text_box6)
                                            tf6 = tb6.text_frame
                                            p_0_6 = tf6.paragraphs[0]
                                            run_p_0_6 = p_0_6.add_run()
                                            run_p_0_6.text = FileListToSendToPPTpreparation[0]
                                            font_p_0_6 = run_p_0_6.font
                                            font_p_0_6.size = Pt(20)
                                            font_p_0_6.name = 'arial'
                                            font_p_0_6.bold = True
                                            font_p_0_6.color.rgb = RGBColor (0, 255, 0)

                                            # delete used image
                                            del FileListToSendToPPTpreparation[0]
                                            print("FileListToSendToPPTpreparation after 2 x 3 is ", FileListToSendToPPTpreparation)
                                            if FileListToSendToPPTpreparation:
                                           
                                                # Image 3 x 1
                                                img7 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                                from_left_7 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                                                from_top_7 = Inches(ImageOffsetY3-(height_or_width/2)+half_w*1.1) ####
                                                add_picture_7 = Slide3.shapes.add_picture(img7, from_left_7, from_top_7, width =None, height =Inches(height_or_width))
                                                add_picture_7.rotation = 90  



                                                # text box 7
                                                left_text_box7 = Inches(TEXTsetX1)
                                                top_text_box7 = Inches(TEXTsetY3)
                                                width_text_box7 = Inches(1.7)
                                                height_text_box7 = Inches(0.4)
                                                tb7 = Slide3.shapes.add_textbox(left_text_box7, top_text_box7, width_text_box7, height_text_box7)
                                                tf7 = tb7.text_frame
                                                p_0_7 = tf7.paragraphs[0]
                                                run_p_0_7 = p_0_7.add_run()
                                                run_p_0_7.text = FileListToSendToPPTpreparation[0]
                                                font_p_0_7 = run_p_0_7.font
                                                font_p_0_7.size = Pt(20)
                                                font_p_0_7.name = 'arial'
                                                font_p_0_7.bold = True
                                                font_p_0_7.color.rgb = RGBColor (0, 255, 0)

                                                # delete used image
                                                del FileListToSendToPPTpreparation[0]
                                                print("FileListToSendToPPTpreparation after 3 x 1 is ", FileListToSendToPPTpreparation)
                                                if FileListToSendToPPTpreparation:
                                                
                                                    # Image 3 x 2
                                                    img8 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                                    from_left_8 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                                    from_top_8 = Inches(ImageOffsetY3-(height_or_width/2)+half_w*1.1) ####
                                                    add_picture_8 = Slide3.shapes.add_picture(img8, from_left_8, from_top_8, width =None, height =Inches(height_or_width))
                                                    add_picture_8.rotation = 90  

                                                    # text box 8
                                                    left_text_box8 = Inches(TEXTsetX2)
                                                    top_text_box8 = Inches(TEXTsetY3)
                                                    width_text_box8 = Inches(1.7)
                                                    height_text_box8 = Inches(0.4)
                                                    tb8 = Slide3.shapes.add_textbox(left_text_box8, top_text_box8, width_text_box8, height_text_box8)
                                                    tf8 = tb8.text_frame
                                                    p_0_8 = tf8.paragraphs[0]
                                                    run_p_0_8 = p_0_8.add_run()
                                                    run_p_0_8.text = FileListToSendToPPTpreparation[0]
                                                    font_p_0_8 = run_p_0_8.font
                                                    font_p_0_8.size = Pt(20)
                                                    font_p_0_8.name = 'arial'
                                                    font_p_0_8.bold = True
                                                    font_p_0_8.color.rgb = RGBColor (0, 255, 0)

                                                    # delete used image
                                                    del FileListToSendToPPTpreparation[0]
                                                    print("FileListToSendToPPTpreparation after 3 x 2 is ", FileListToSendToPPTpreparation)
                                                    if FileListToSendToPPTpreparation:
                                                        
                                                        # Image 3 x 3
                                                        img9 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                                        from_left_9 = Inches(ImageOffsetX3+(height_or_width/2)-half_w*0.9) ###
                                                        from_top_9 = Inches(ImageOffsetY3-(height_or_width/2)+half_w*1.1) ####
                                                        add_picture_9 = Slide3.shapes.add_picture(img9, from_left_9, from_top_9, width =None, height =Inches(height_or_width))
                                                        add_picture_9.rotation = 90  

                                                        # text box 9
                                                        left_text_box9 = Inches(TEXTsetX3)
                                                        top_text_box9 = Inches(TEXTsetY3)
                                                        width_text_box9 = Inches(1.7)
                                                        height_text_box9 = Inches(0.4)
                                                        tb9 = Slide3.shapes.add_textbox(left_text_box9, top_text_box9, width_text_box9, height_text_box9)
                                                        tf9 = tb9.text_frame
                                                        p_0_9 = tf9.paragraphs[0]
                                                        run_p_0_9 = p_0_9.add_run()
                                                        run_p_0_9.text = FileListToSendToPPTpreparation[0]
                                                        font_p_0_9 = run_p_0_9.font
                                                        font_p_0_9.size = Pt(20)
                                                        font_p_0_9.name = 'arial'
                                                        font_p_0_9.bold = True
                                                        font_p_0_9.color.rgb = RGBColor (0, 255, 0)

                                                        del FileListToSendToPPTpreparation[0]
                                                        print("Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        
                                                        if FileListToSendToPPTpreparation:
                                                            print("============== Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                                                            NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                                                           
                    NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                    MyPresentation.save(pptFilePath)
                NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)

            if Rotation == -90:
                print("Rotation is", Rotation)
                def NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                    ImageOffsetX1 = 3.4
                    ImageOffsetX2 = 5.55
                    ImageOffsetX3 = 7.7
                    ImageOffsetY1 = 1.1
                    ImageOffsetY2 = 3.2
                    ImageOffsetY3 =  5.3

                    TEXTsetX1 = 3.4
                    TEXTsetX2 = 5.6
                    TEXTsetX3 = 7.7
                    TEXTsetY1 = 1.2
                    TEXTsetY2 = 3.3
                    TEXTsetY3 = 5.4                 
                    
                    
                    pptFilePath = os.path.join(Path0, f"{JobNumber}_{JobSubNumber}_{JobDescription}.pptx")
                    print("The pptFilePath is ", pptFilePath)
                    if exists(pptFilePath) == True:
                        MyPresentation = Presentation(pptx = pptFilePath)
                    else:
                        MyPresentation = Presentation()

                    def NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                        print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        print("thePathOfTheCurrentImagesGroup is ", thePathOfTheCurrentImagesGroup)
                        print("FileListToSendToPPTpreparation[0]", FileListToSendToPPTpreparation[0])
                        # Check if the ppt exist or not.
                    
                        # Add slide 3 and add pictures in the slides
                        Slide3_Register = MyPresentation.slide_layouts[5]
                        Slide3 = MyPresentation.slides.add_slide(Slide3_Register)
                        title3  = Slide3.shapes.title
                        title3.text = thePathOfTheCurrentImagesGroup.split("\\")[-1]

                        # Add a GPS image
                        # img0 = r"C:\0Python\images\TheJob\JobB\Word.jpg"
                        img0 = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
                        from_left_0 = Inches(0.12)
                        from_top_0 = Inches(2)
                        add_picture_0 = Slide3.shapes.add_picture(img0, from_left_0, from_top_0, width =Inches(3.3), height =None)

                        # Image 1 x 1
                        # First row of images
                        img1 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")        
                        from_left_1 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                        from_top_1 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                        add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =None, height =Inches(height_or_width))
                        add_picture_1.rotation = -90


                  
                        # text box 1
                        left_text_box2 = Inches(TEXTsetX1)
                        top_text_box2 = Inches(TEXTsetY1)
                        width_text_box2 = Inches(1.7)
                        height_text_box2 = Inches(0.4)
                        tb2 = Slide3.shapes.add_textbox(left_text_box2, top_text_box2, width_text_box2, height_text_box2)
                        tf2 = tb2.text_frame
                        p_0_2 = tf2.paragraphs[0]
                        run_p_0_2 = p_0_2.add_run()
                        run_p_0_2.text = FileListToSendToPPTpreparation[0]
                        font_p_0_2 = run_p_0_2.font
                        font_p_0_2.size = Pt(20)
                        font_p_0_2.name = 'arial'
                        font_p_0_2.bold = True
                        font_p_0_2.color.rgb = RGBColor (0, 255, 0)    

                        # delete used image
                        del FileListToSendToPPTpreparation[0]
                        print("FileListToSendToPPTpreparation after 1 x 1 is ", FileListToSendToPPTpreparation)
                        if FileListToSendToPPTpreparation:
                            
                            # Image 1 x 2
                            img2 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                            #from_left_2 = Inches(5.7)
                            #from_top_2 = Inches(1.4)
                            #add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =Inches(2), height =None)
                            from_left_2 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                            from_top_2 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                            add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =None, height =Inches(height_or_width))
                            add_picture_2.rotation = -90
  
                            # tex box 2
                            left_text_box1 = Inches(TEXTsetX2)
                            top_text_box1 = Inches(TEXTsetY1)
                            width_text_box1 = Inches(1.7)
                            height_text_box1 = Inches(0.4)
                            tb1 = Slide3.shapes.add_textbox(left_text_box1, top_text_box1, width_text_box1, height_text_box1)
                            tf1 = tb1.text_frame
                            p_0_1 = tf1.paragraphs[0]
                            run_p_0_1 = p_0_1.add_run()
                            run_p_0_1.text = FileListToSendToPPTpreparation[0]
                            font_p_0_1 = run_p_0_1.font
                            font_p_0_1.size = Pt(20)
                            font_p_0_1.name = 'arial'
                            font_p_0_1.bold = True
                            font_p_0_1.color.rgb = RGBColor (0, 255, 0)        

                            # delete used image
                            del FileListToSendToPPTpreparation[0]
                            print("FileListToSendToPPTpreparation after 1 x 2 is ", FileListToSendToPPTpreparation)
                            if FileListToSendToPPTpreparation:

                                # Image 1 x 3
                                img3 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                #from_left_3 = Inches(7.8)
                                #from_top_3 = Inches(1.4)
                                #add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =Inches(2), height =None)
                                from_left_3 = Inches(ImageOffsetX3+(height_or_width/2)-half_w*0.9) ###
                                from_top_3 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                                add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =None, height =Inches(height_or_width))
                                add_picture_3.rotation = -90


                                 # text box 3
                                left_text_box3 = Inches(TEXTsetX3)
                                top_text_box3 = Inches(TEXTsetY1)
                                width_text_box3 = Inches(1.7)
                                height_text_box3 = Inches(0.4)
                                tb3 = Slide3.shapes.add_textbox(left_text_box3, top_text_box3, width_text_box3, height_text_box3)
                                tf3 = tb3.text_frame
                                p_0_3 = tf3.paragraphs[0]
                                run_p_0_3 = p_0_3.add_run()
                                run_p_0_3.text = FileListToSendToPPTpreparation[0]
                                font_p_0_3 = run_p_0_3.font
                                font_p_0_3.size = Pt(20)
                                font_p_0_3.name = 'arial'
                                font_p_0_3.bold = True
                                font_p_0_3.color.rgb = RGBColor (0, 255, 0)

                                # delete used image
                                del FileListToSendToPPTpreparation[0]
                                print("FileListToSendToPPTpreparation after 1 x 3 is ", FileListToSendToPPTpreparation)
                                if FileListToSendToPPTpreparation:
                               
                                    # Image 2 x 1
                                    img4 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                    #from_left_4 = Inches(3.5)
                                    #from_top_4 = Inches(3.5)
                                    #add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =Inches(2), height =None)
                                    from_left_4 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                                    from_top_4 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                    add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =None, height =Inches(height_or_width))
                                    add_picture_4.rotation = -90
                     
                                    # text box 4
                                    left_text_box4 = Inches(TEXTsetX1)
                                    top_text_box4 = Inches(TEXTsetY2)
                                    width_text_box4 = Inches(1.7)
                                    height_text_box4 = Inches(0.4)
                                    tb4 = Slide3.shapes.add_textbox(left_text_box4, top_text_box4, width_text_box4, height_text_box4)
                                    tf4 = tb4.text_frame
                                    p_0_4 = tf4.paragraphs[0]
                                    run_p_0_4 = p_0_4.add_run()
                                    run_p_0_4.text = FileListToSendToPPTpreparation[0]
                                    font_p_0_4 = run_p_0_4.font
                                    font_p_0_4.size = Pt(20)
                                    font_p_0_4.name = 'arial'
                                    font_p_0_4.bold = True
                                    font_p_0_4.color.rgb = RGBColor (0, 255, 0)

                                    # delete used image
                                    del FileListToSendToPPTpreparation[0]
                                    print("FileListToSendToPPTpreparation after 2 x 1 is ", FileListToSendToPPTpreparation)
                                    if FileListToSendToPPTpreparation:
                                    
                                        # Image 2 x 2
                                        img5 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                        from_left_5 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                        from_top_5 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                        add_picture_5 = Slide3.shapes.add_picture(img5, from_left_5, from_top_5, width =None, height =Inches(height_or_width))
                                        add_picture_5.rotation = -90  

                                        # text box 5
                                        left_text_box5 = Inches(TEXTsetX2)
                                        top_text_box5 = Inches(TEXTsetY2)
                                        width_text_box5 = Inches(1.7)
                                        height_text_box5 = Inches(0.4)
                                        tb5 = Slide3.shapes.add_textbox(left_text_box5, top_text_box5, width_text_box5, height_text_box5)
                                        tf5 = tb5.text_frame
                                        p_0_5 = tf5.paragraphs[0]
                                        run_p_0_5 = p_0_5.add_run()
                                        run_p_0_5.text = FileListToSendToPPTpreparation[0]
                                        font_p_0_5 = run_p_0_5.font
                                        font_p_0_5.size = Pt(20)
                                        font_p_0_5.name = 'arial'
                                        font_p_0_5.bold = True
                                        font_p_0_5.color.rgb = RGBColor (0, 255, 0)

                                        # delete used image
                                        del FileListToSendToPPTpreparation[0]
                                        print("FileListToSendToPPTpreparation after 2 x 2 is ", FileListToSendToPPTpreparation)
                                        if FileListToSendToPPTpreparation:
                                        
                                            # Image 2 x 3
                                            img6 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                            from_left_6 = Inches(ImageOffsetX3+(height_or_width/2)-half_w*0.9) ###
                                            from_top_6 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                            add_picture_6 = Slide3.shapes.add_picture(img6, from_left_6, from_top_6, width =None, height =Inches(height_or_width))
                                            add_picture_6.rotation = -90  

                                            # text box 6
                                            left_text_box6 = Inches(TEXTsetX3)
                                            top_text_box6 = Inches(TEXTsetY2)
                                            width_text_box6 = Inches(1.7)
                                            height_text_box6 = Inches(0.4)
                                            tb6 = Slide3.shapes.add_textbox(left_text_box6, top_text_box6, width_text_box6, height_text_box6)
                                            tf6 = tb6.text_frame
                                            p_0_6 = tf6.paragraphs[0]
                                            run_p_0_6 = p_0_6.add_run()
                                            run_p_0_6.text = FileListToSendToPPTpreparation[0]
                                            font_p_0_6 = run_p_0_6.font
                                            font_p_0_6.size = Pt(20)
                                            font_p_0_6.name = 'arial'
                                            font_p_0_6.bold = True
                                            font_p_0_6.color.rgb = RGBColor (0, 255, 0)

                                            # delete used image
                                            del FileListToSendToPPTpreparation[0]
                                            print("FileListToSendToPPTpreparation after 2 x 3 is ", FileListToSendToPPTpreparation)
                                            if FileListToSendToPPTpreparation:
                                           
                                                # Image 3 x 1
                                                img7 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                                from_left_7 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                                                from_top_7 = Inches(ImageOffsetY3-(height_or_width/2)+half_w*1.1) ####
                                                add_picture_7 = Slide3.shapes.add_picture(img7, from_left_7, from_top_7, width =None, height =Inches(height_or_width))
                                                add_picture_7.rotation = -90  



                                                # text box 7
                                                left_text_box7 = Inches(TEXTsetX1)
                                                top_text_box7 = Inches(TEXTsetY3)
                                                width_text_box7 = Inches(1.7)
                                                height_text_box7 = Inches(0.4)
                                                tb7 = Slide3.shapes.add_textbox(left_text_box7, top_text_box7, width_text_box7, height_text_box7)
                                                tf7 = tb7.text_frame
                                                p_0_7 = tf7.paragraphs[0]
                                                run_p_0_7 = p_0_7.add_run()
                                                run_p_0_7.text = FileListToSendToPPTpreparation[0]
                                                font_p_0_7 = run_p_0_7.font
                                                font_p_0_7.size = Pt(20)
                                                font_p_0_7.name = 'arial'
                                                font_p_0_7.bold = True
                                                font_p_0_7.color.rgb = RGBColor (0, 255, 0)

                                                # delete used image
                                                del FileListToSendToPPTpreparation[0]
                                                print("FileListToSendToPPTpreparation after 3 x 1 is ", FileListToSendToPPTpreparation)
                                                if FileListToSendToPPTpreparation:
                                                
                                                    # Image 3 x 2
                                                    img8 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                                    from_left_8 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                                    from_top_8 = Inches(ImageOffsetY3-(height_or_width/2)+half_w*1.1) ####
                                                    add_picture_8 = Slide3.shapes.add_picture(img8, from_left_8, from_top_8, width =None, height =Inches(height_or_width))
                                                    add_picture_8.rotation = -90  

                                                    # text box 8
                                                    left_text_box8 = Inches(TEXTsetX2)
                                                    top_text_box8 = Inches(TEXTsetY3)
                                                    width_text_box8 = Inches(1.7)
                                                    height_text_box8 = Inches(0.4)
                                                    tb8 = Slide3.shapes.add_textbox(left_text_box8, top_text_box8, width_text_box8, height_text_box8)
                                                    tf8 = tb8.text_frame
                                                    p_0_8 = tf8.paragraphs[0]
                                                    run_p_0_8 = p_0_8.add_run()
                                                    run_p_0_8.text = FileListToSendToPPTpreparation[0]
                                                    font_p_0_8 = run_p_0_8.font
                                                    font_p_0_8.size = Pt(20)
                                                    font_p_0_8.name = 'arial'
                                                    font_p_0_8.bold = True
                                                    font_p_0_8.color.rgb = RGBColor (0, 255, 0)

                                                    # delete used image
                                                    del FileListToSendToPPTpreparation[0]
                                                    print("FileListToSendToPPTpreparation after 3 x 2 is ", FileListToSendToPPTpreparation)
                                                    if FileListToSendToPPTpreparation:
                                                        
                                                        # Image 3 x 3
                                                        img9 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                                        from_left_9 = Inches(ImageOffsetX3+(height_or_width/2)-half_w*0.9) ###
                                                        from_top_9 = Inches(ImageOffsetY3-(height_or_width/2)+half_w*1.1) ####
                                                        add_picture_9 = Slide3.shapes.add_picture(img9, from_left_9, from_top_9, width =None, height =Inches(height_or_width))
                                                        add_picture_9.rotation = -90  

                                                        # text box 9
                                                        left_text_box9 = Inches(TEXTsetX3)
                                                        top_text_box9 = Inches(TEXTsetY3)
                                                        width_text_box9 = Inches(1.7)
                                                        height_text_box9 = Inches(0.4)
                                                        tb9 = Slide3.shapes.add_textbox(left_text_box9, top_text_box9, width_text_box9, height_text_box9)
                                                        tf9 = tb9.text_frame
                                                        p_0_9 = tf9.paragraphs[0]
                                                        run_p_0_9 = p_0_9.add_run()
                                                        run_p_0_9.text = FileListToSendToPPTpreparation[0]
                                                        font_p_0_9 = run_p_0_9.font
                                                        font_p_0_9.size = Pt(20)
                                                        font_p_0_9.name = 'arial'
                                                        font_p_0_9.bold = True
                                                        font_p_0_9.color.rgb = RGBColor (0, 255, 0)

                                                        del FileListToSendToPPTpreparation[0]
                                                        print("Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        
                                                        if FileListToSendToPPTpreparation:
                                                            print("============== Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                                                            NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                                                           
                    NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                    MyPresentation.save(pptFilePath)
                NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
        SixImagesWithoutScallingSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup, Rotation)

    if 70 <im_real_width < 150 and 70 <im_real_height < 150:
        print("Rotation, im_real_width, im_real_height", Rotation, im_real_width, im_real_height) 
        def FourImagesWithoutScallingSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup, Rotation):
            if Rotation == 0:
                print("Rotation is", Rotation)
                def NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                    
                    pptFilePath = os.path.join(Path0, f"{JobNumber}_{JobSubNumber}_{JobDescription}.pptx")
                    print("The pptFilePath is ", pptFilePath)
                    if exists(pptFilePath) == True:
                        MyPresentation = Presentation(pptx = pptFilePath)
                    else:
                        MyPresentation = Presentation()

                    def NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                        ImageNoneRotatonOffsetX1 = 3.6
                        ImageNoneRotatonOffsetX2 = 6.8
                        ImageNoneRotatonOffsetY1 = 1.2
                        ImageNoneRotatonOffsetY2 = 4.4
                        
                        TEXTsetNoneRotatonX1 = 3.55
                        TEXTsetNoneRotatonX2 = 6.75
                        TEXTsetNoneRotatonY1 = 1.2
                        TEXTsetNoneRotatonY2 = 4.4   

                        print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        print("thePathOfTheCurrentImagesGroup is ", thePathOfTheCurrentImagesGroup)
                        print("FileListToSendToPPTpreparation[0]", FileListToSendToPPTpreparation[0])
                        # Check if the ppt exist or not.
                    
                        # Add slide 3 and add pictures in the slides
                        Slide3_Register = MyPresentation.slide_layouts[5]
                        Slide3 = MyPresentation.slides.add_slide(Slide3_Register)
                        title3  = Slide3.shapes.title
                        title3.text = thePathOfTheCurrentImagesGroup.split("\\")[-1]

                        # Add a GPS image
                        # img0 = r"C:\0Python\images\TheJob\JobB\Word.jpg"
                        img0 = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
                        from_left_0 = Inches(0.12)
                        from_top_0 = Inches(2)
                        add_picture_0 = Slide3.shapes.add_picture(img0, from_left_0, from_top_0, width =Inches(3.3), height =None)

                        # Image 1 x 1
                        # First row of images
                        img1 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")        
                        print(img1, "in ppt3")
                        from_left_1 = Inches(ImageNoneRotatonOffsetX1)
                        from_top_1 = Inches(ImageNoneRotatonOffsetY1)
                        add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =Inches(3.1), height =None)
                        # text box 2
                        left_text_box2 = Inches(TEXTsetNoneRotatonX1)
                        top_text_box2 = Inches(TEXTsetNoneRotatonY1)
                        width_text_box2 = Inches(1.7)
                        height_text_box2 = Inches(0.4)
                        tb2 = Slide3.shapes.add_textbox(left_text_box2, top_text_box2, width_text_box2, height_text_box2)
                        tf2 = tb2.text_frame
                        p_0_2 = tf2.paragraphs[0]
                        run_p_0_2 = p_0_2.add_run()
                        run_p_0_2.text = FileListToSendToPPTpreparation[0]
                        font_p_0_2 = run_p_0_2.font
                        font_p_0_2.size = Pt(20)
                        font_p_0_2.name = 'arial'
                        font_p_0_2.bold = True
                        font_p_0_2.color.rgb = RGBColor (0, 255, 0)    

                        # delete used image
                        del FileListToSendToPPTpreparation[0]
                        print("FileListToSendToPPTpreparation after 1 x 1 is ", FileListToSendToPPTpreparation)
                        if FileListToSendToPPTpreparation:
                            
                            # Image 1 x 2
                            img2 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                            from_left_2 = Inches(ImageNoneRotatonOffsetX1)
                            from_top_2 = Inches(ImageNoneRotatonOffsetY2)
                            add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =Inches(3.1), height =None)
                            # tex box 1
                            left_text_box1 = Inches(TEXTsetNoneRotatonX1)
                            top_text_box1 = Inches(TEXTsetNoneRotatonY2)
                            width_text_box1 = Inches(1.7)
                            height_text_box1 = Inches(0.4)
                            tb1 = Slide3.shapes.add_textbox(left_text_box1, top_text_box1, width_text_box1, height_text_box1)
                            tf1 = tb1.text_frame
                            p_0_1 = tf1.paragraphs[0]
                            run_p_0_1 = p_0_1.add_run()
                            run_p_0_1.text = FileListToSendToPPTpreparation[0]
                            font_p_0_1 = run_p_0_1.font
                            font_p_0_1.size = Pt(20)
                            font_p_0_1.name = 'arial'
                            font_p_0_1.bold = True
                            font_p_0_1.color.rgb = RGBColor (0, 255, 0)        

                            # delete used image
                            del FileListToSendToPPTpreparation[0]
                            print("FileListToSendToPPTpreparation after 1 x 2 is ", FileListToSendToPPTpreparation)
                            if FileListToSendToPPTpreparation:

                                # Image 2 x 1
                                img3 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                from_left_3 = Inches(ImageNoneRotatonOffsetX2)
                                from_top_3 = Inches(ImageNoneRotatonOffsetY1)
                                add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =Inches(3.1), height =None)
                                 # text box 3
                                left_text_box3 = Inches(TEXTsetNoneRotatonX2)
                                top_text_box3 = Inches(TEXTsetNoneRotatonY1)
                                width_text_box3 = Inches(1.7)
                                height_text_box3 = Inches(0.4)
                                tb3 = Slide3.shapes.add_textbox(left_text_box3, top_text_box3, width_text_box3, height_text_box3)
                                tf3 = tb3.text_frame
                                p_0_3 = tf3.paragraphs[0]
                                run_p_0_3 = p_0_3.add_run()
                                run_p_0_3.text = FileListToSendToPPTpreparation[0]
                                font_p_0_3 = run_p_0_3.font
                                font_p_0_3.size = Pt(20)
                                font_p_0_3.name = 'arial'
                                font_p_0_3.bold = True
                                font_p_0_3.color.rgb = RGBColor (0, 255, 0)

                                # delete used image
                                del FileListToSendToPPTpreparation[0]
                                print("FileListToSendToPPTpreparation after 1 x 3 is ", FileListToSendToPPTpreparation)
                                if FileListToSendToPPTpreparation:
                               
                                    # Image 2 x 2
                                    img4 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                    from_left_4 = Inches(ImageNoneRotatonOffsetX2)
                                    from_top_4 = Inches(ImageNoneRotatonOffsetY2)
                                    add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =Inches(3.1), height =None)
                                    # text box 4
                                    left_text_box4 = Inches(TEXTsetNoneRotatonX2)
                                    top_text_box4 = Inches(TEXTsetNoneRotatonY2)
                                    width_text_box4 = Inches(1.7)
                                    height_text_box4 = Inches(0.4)
                                    tb4 = Slide3.shapes.add_textbox(left_text_box4, top_text_box4, width_text_box4, height_text_box4)
                                    tf4 = tb4.text_frame
                                    p_0_4 = tf4.paragraphs[0]
                                    run_p_0_4 = p_0_4.add_run()
                                    run_p_0_4.text = FileListToSendToPPTpreparation[0]
                                    font_p_0_4 = run_p_0_4.font
                                    font_p_0_4.size = Pt(20)
                                    font_p_0_4.name = 'arial'
                                    font_p_0_4.bold = True
                                    font_p_0_4.color.rgb = RGBColor (0, 255, 0)


                                    del FileListToSendToPPTpreparation[0]
                                    print("Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
    
                                    if FileListToSendToPPTpreparation:
                                        print("============== Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                                        NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                                                       
                    NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                    MyPresentation.save(pptFilePath)
                NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
            
            if Rotation == 90:
                print("Rotation is", Rotation)
                def NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                    ImageOffsetX1 = 3.4   # The numbers need to be verified ============================================
                    ImageOffsetX2 = 6.6
                    ImageOffsetY1 = 1.2
                    ImageOffsetY2 = 4.4

                    TEXTsetX1 = 3.4
                    TEXTsetX2 = 6.6
                    TEXTsetY1 = 1.2
                    TEXTsetY2 = 4.4
                    
                    pptFilePath = os.path.join(Path0, f"{JobNumber}_{JobSubNumber}_{JobDescription}.pptx")
                    print("The pptFilePath is ", pptFilePath)
                    if exists(pptFilePath) == True:
                        MyPresentation = Presentation(pptx = pptFilePath)
                    else:
                        MyPresentation = Presentation()

                    def NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                        print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        print("thePathOfTheCurrentImagesGroup is ", thePathOfTheCurrentImagesGroup)
                        print("FileListToSendToPPTpreparation[0]", FileListToSendToPPTpreparation[0])
                        # Check if the ppt exist or not.
                    
                        # Add slide 3 and add pictures in the slides
                        Slide3_Register = MyPresentation.slide_layouts[5]
                        Slide3 = MyPresentation.slides.add_slide(Slide3_Register)
                        title3  = Slide3.shapes.title
                        title3.text = thePathOfTheCurrentImagesGroup.split("\\")[-1]

                        # Add a GPS image
                        # img0 = r"C:\0Python\images\TheJob\JobB\Word.jpg"
                        img0 = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
                        from_left_0 = Inches(0.12)
                        from_top_0 = Inches(2)
                        add_picture_0 = Slide3.shapes.add_picture(img0, from_left_0, from_top_0, width =Inches(3.3), height =None)

                        height_or_widthFactor = 1.5

                        # Image 1 x 1
                        # First row of images
                        img1 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")        
                        from_left_1 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                        from_top_1 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                        add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =None, height =Inches(height_or_width*height_or_widthFactor))
                        add_picture_1.rotation = 90
                  
                        # text box 1
                        left_text_box2 = Inches(TEXTsetX1)
                        top_text_box2 = Inches(TEXTsetY1)
                        width_text_box2 = Inches(1.7)
                        height_text_box2 = Inches(0.4)
                        tb2 = Slide3.shapes.add_textbox(left_text_box2, top_text_box2, width_text_box2, height_text_box2)
                        tf2 = tb2.text_frame
                        p_0_2 = tf2.paragraphs[0]
                        run_p_0_2 = p_0_2.add_run()
                        run_p_0_2.text = FileListToSendToPPTpreparation[0]
                        font_p_0_2 = run_p_0_2.font
                        font_p_0_2.size = Pt(20)
                        font_p_0_2.name = 'arial'
                        font_p_0_2.bold = True
                        font_p_0_2.color.rgb = RGBColor (0, 255, 0)    

                        # delete used image
                        del FileListToSendToPPTpreparation[0]
                        print("FileListToSendToPPTpreparation after 1 x 1 is ", FileListToSendToPPTpreparation)
                        if FileListToSendToPPTpreparation:
                            
                            # Image 1 x 2
                            img2 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                            #from_left_2 = Inches(5.7)
                            #from_top_2 = Inches(1.4)
                            #add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =Inches(2), height =None)
                            from_left_2 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                            from_top_2 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                            add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =None, height =Inches(height_or_width*height_or_widthFactor))
                            add_picture_2.rotation = 90
  
                            # tex box 2
                            left_text_box1 = Inches(TEXTsetX1)
                            top_text_box1 = Inches(TEXTsetY2)
                            width_text_box1 = Inches(1.7)
                            height_text_box1 = Inches(0.4)
                            tb1 = Slide3.shapes.add_textbox(left_text_box1, top_text_box1, width_text_box1, height_text_box1)
                            tf1 = tb1.text_frame
                            p_0_1 = tf1.paragraphs[0]
                            run_p_0_1 = p_0_1.add_run()
                            run_p_0_1.text = FileListToSendToPPTpreparation[0]
                            font_p_0_1 = run_p_0_1.font
                            font_p_0_1.size = Pt(20)
                            font_p_0_1.name = 'arial'
                            font_p_0_1.bold = True
                            font_p_0_1.color.rgb = RGBColor (0, 255, 0)        

                            # delete used image
                            del FileListToSendToPPTpreparation[0]
                            print("FileListToSendToPPTpreparation after 1 x 2 is ", FileListToSendToPPTpreparation)
                            if FileListToSendToPPTpreparation:

                                # Image 1 x 3
                                img3 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                #from_left_3 = Inches(7.8)
                                #from_top_3 = Inches(1.4)
                                #add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =Inches(2), height =None)
                                from_left_3 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                from_top_3 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                                add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =None, height =Inches(height_or_width*height_or_widthFactor))
                                add_picture_3.rotation = 90


                                 # text box 3
                                left_text_box3 = Inches(TEXTsetX2)
                                top_text_box3 = Inches(TEXTsetY1)
                                width_text_box3 = Inches(1.7)
                                height_text_box3 = Inches(0.4)
                                tb3 = Slide3.shapes.add_textbox(left_text_box3, top_text_box3, width_text_box3, height_text_box3)
                                tf3 = tb3.text_frame
                                p_0_3 = tf3.paragraphs[0]
                                run_p_0_3 = p_0_3.add_run()
                                run_p_0_3.text = FileListToSendToPPTpreparation[0]
                                font_p_0_3 = run_p_0_3.font
                                font_p_0_3.size = Pt(20)
                                font_p_0_3.name = 'arial'
                                font_p_0_3.bold = True
                                font_p_0_3.color.rgb = RGBColor (0, 255, 0)

                                # delete used image
                                del FileListToSendToPPTpreparation[0]
                                print("FileListToSendToPPTpreparation after 1 x 3 is ", FileListToSendToPPTpreparation)
                                if FileListToSendToPPTpreparation:
                               
                                    # Image 2 x 1
                                    img4 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                    #from_left_4 = Inches(3.5)
                                    #from_top_4 = Inches(3.5)
                                    #add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =Inches(2), height =None)
                                    from_left_4 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                    from_top_4 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                    add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =None, height =Inches(height_or_width*height_or_widthFactor))
                                    add_picture_4.rotation = 90
                     
                                    # text box 4
                                    left_text_box4 = Inches(TEXTsetX2)
                                    top_text_box4 = Inches(TEXTsetY2)
                                    width_text_box4 = Inches(1.7)
                                    height_text_box4 = Inches(0.4)
                                    tb4 = Slide3.shapes.add_textbox(left_text_box4, top_text_box4, width_text_box4, height_text_box4)
                                    tf4 = tb4.text_frame
                                    p_0_4 = tf4.paragraphs[0]
                                    run_p_0_4 = p_0_4.add_run()
                                    run_p_0_4.text = FileListToSendToPPTpreparation[0]
                                    font_p_0_4 = run_p_0_4.font
                                    font_p_0_4.size = Pt(20)
                                    font_p_0_4.name = 'arial'
                                    font_p_0_4.bold = True
                                    font_p_0_4.color.rgb = RGBColor (0, 255, 0)

                                    # delete used image
                                    del FileListToSendToPPTpreparation[0]
                                    print("Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
    
                                    if FileListToSendToPPTpreparation:
                                        print("============== Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                                        NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                                                           
                    NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                    MyPresentation.save(pptFilePath)
                NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)

            if Rotation == -90:
                print("Rotation is", Rotation)
                def NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                    ImageOffsetX1 = 3.4
                    ImageOffsetX2 = 6.6
                    ImageOffsetY1 = 1.2
                    ImageOffsetY2 = 4.4

                    TEXTsetX1 = 3.4
                    TEXTsetX2 = 6.6
                    TEXTsetY1 = 1.2
                    TEXTsetY2 = 4.4               
                    
                    pptFilePath = os.path.join(Path0, f"{JobNumber}_{JobSubNumber}_{JobDescription}.pptx")
                    print("The pptFilePath is ", pptFilePath)
                    if exists(pptFilePath) == True:
                        MyPresentation = Presentation(pptx = pptFilePath)
                    else:
                        MyPresentation = Presentation()

                    def NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                        print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        print("thePathOfTheCurrentImagesGroup is ", thePathOfTheCurrentImagesGroup)
                        print("FileListToSendToPPTpreparation[0]", FileListToSendToPPTpreparation[0])
                        # Check if the ppt exist or not.
                    
                        # Add slide 3 and add pictures in the slides
                        Slide3_Register = MyPresentation.slide_layouts[5]
                        Slide3 = MyPresentation.slides.add_slide(Slide3_Register)
                        title3  = Slide3.shapes.title
                        title3.text = thePathOfTheCurrentImagesGroup.split("\\")[-1]

                        # Add a GPS image
                        # img0 = r"C:\0Python\images\TheJob\JobB\Word.jpg"
                        img0 = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
                        from_left_0 = Inches(0.12)
                        from_top_0 = Inches(2)
                        add_picture_0 = Slide3.shapes.add_picture(img0, from_left_0, from_top_0, width =Inches(3.3), height =None)

                        # Image 1 x 1
                        height_or_widthFactorForNegative = 1.5
                        # First row of images
                        img1 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")        
                        from_left_1 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                        from_top_1 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                        add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =None, height =Inches(height_or_width*height_or_widthFactorForNegative))
                        add_picture_1.rotation = -90


                  
                        # text box 1
                        left_text_box2 = Inches(TEXTsetX1)
                        top_text_box2 = Inches(TEXTsetY1)
                        width_text_box2 = Inches(1.7)
                        height_text_box2 = Inches(0.4)
                        tb2 = Slide3.shapes.add_textbox(left_text_box2, top_text_box2, width_text_box2, height_text_box2)
                        tf2 = tb2.text_frame
                        p_0_2 = tf2.paragraphs[0]
                        run_p_0_2 = p_0_2.add_run()
                        run_p_0_2.text = FileListToSendToPPTpreparation[0]
                        font_p_0_2 = run_p_0_2.font
                        font_p_0_2.size = Pt(20)
                        font_p_0_2.name = 'arial'
                        font_p_0_2.bold = True
                        font_p_0_2.color.rgb = RGBColor (0, 255, 0)    

                        # delete used image
                        del FileListToSendToPPTpreparation[0]
                        print("FileListToSendToPPTpreparation after 1 x 1 is ", FileListToSendToPPTpreparation)
                        if FileListToSendToPPTpreparation:
                            
                            # Image 1 x 2
                            img2 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                            #from_left_2 = Inches(5.7)
                            #from_top_2 = Inches(1.4)
                            #add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =Inches(2), height =None)
                            from_left_2 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                            from_top_2 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                            add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =None, height =Inches(height_or_width*height_or_widthFactorForNegative))
                            add_picture_2.rotation = -90
  
                            # tex box 2
                            left_text_box1 = Inches(TEXTsetX1)
                            top_text_box1 = Inches(TEXTsetY2)
                            width_text_box1 = Inches(1.7)
                            height_text_box1 = Inches(0.4)
                            tb1 = Slide3.shapes.add_textbox(left_text_box1, top_text_box1, width_text_box1, height_text_box1)
                            tf1 = tb1.text_frame
                            p_0_1 = tf1.paragraphs[0]
                            run_p_0_1 = p_0_1.add_run()
                            run_p_0_1.text = FileListToSendToPPTpreparation[0]
                            font_p_0_1 = run_p_0_1.font
                            font_p_0_1.size = Pt(20)
                            font_p_0_1.name = 'arial'
                            font_p_0_1.bold = True
                            font_p_0_1.color.rgb = RGBColor (0, 255, 0)        

                            # delete used image
                            del FileListToSendToPPTpreparation[0]
                            print("FileListToSendToPPTpreparation after 1 x 2 is ", FileListToSendToPPTpreparation)
                            if FileListToSendToPPTpreparation:

                                # Image 1 x 3
                                img3 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                #from_left_3 = Inches(7.8)
                                #from_top_3 = Inches(1.4)
                                #add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =Inches(2), height =None)
                                from_left_3 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                from_top_3 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                                add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =None, height =Inches(height_or_width*height_or_widthFactorForNegative))
                                add_picture_3.rotation = -90


                                 # text box 3
                                left_text_box3 = Inches(TEXTsetX2)
                                top_text_box3 = Inches(TEXTsetY1)
                                width_text_box3 = Inches(1.7)
                                height_text_box3 = Inches(0.4)
                                tb3 = Slide3.shapes.add_textbox(left_text_box3, top_text_box3, width_text_box3, height_text_box3)
                                tf3 = tb3.text_frame
                                p_0_3 = tf3.paragraphs[0]
                                run_p_0_3 = p_0_3.add_run()
                                run_p_0_3.text = FileListToSendToPPTpreparation[0]
                                font_p_0_3 = run_p_0_3.font
                                font_p_0_3.size = Pt(20)
                                font_p_0_3.name = 'arial'
                                font_p_0_3.bold = True
                                font_p_0_3.color.rgb = RGBColor (0, 255, 0)

                                # delete used image
                                del FileListToSendToPPTpreparation[0]
                                print("FileListToSendToPPTpreparation after 1 x 3 is ", FileListToSendToPPTpreparation)
                                if FileListToSendToPPTpreparation:
                               
                                    # Image 2 x 1
                                    img4 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                    #from_left_4 = Inches(3.5)
                                    #from_top_4 = Inches(3.5)
                                    #add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =Inches(2), height =None)
                                    from_left_4 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                    from_top_4 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                    add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =None, height =Inches(height_or_width*height_or_widthFactorForNegative))
                                    add_picture_4.rotation = -90
                     
                                    # text box 4
                                    left_text_box4 = Inches(TEXTsetX2)
                                    top_text_box4 = Inches(TEXTsetY2)
                                    width_text_box4 = Inches(1.7)
                                    height_text_box4 = Inches(0.4)
                                    tb4 = Slide3.shapes.add_textbox(left_text_box4, top_text_box4, width_text_box4, height_text_box4)
                                    tf4 = tb4.text_frame
                                    p_0_4 = tf4.paragraphs[0]
                                    run_p_0_4 = p_0_4.add_run()
                                    run_p_0_4.text = FileListToSendToPPTpreparation[0]
                                    font_p_0_4 = run_p_0_4.font
                                    font_p_0_4.size = Pt(20)
                                    font_p_0_4.name = 'arial'
                                    font_p_0_4.bold = True
                                    font_p_0_4.color.rgb = RGBColor (0, 255, 0)

                                    del FileListToSendToPPTpreparation[0]
                                    print("Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
    
                                    if FileListToSendToPPTpreparation:
                                        print("============== Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                                        NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                                       
                    NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                    MyPresentation.save(pptFilePath)
                NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)

        FourImagesWithoutScallingSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup, Rotation)


    if im_real_width > 150 or im_real_height > 150:
        print("Rotation, im_real_width, im_real_height", Rotation, im_real_width, im_real_height) 
        def FourImagesWithoutScallingSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup, Rotation):
            if Rotation == 0:
                print("Rotation is", Rotation)
                def NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                    
                    pptFilePath = os.path.join(Path0, f"{JobNumber}_{JobSubNumber}_{JobDescription}.pptx")
                    print("The pptFilePath is ", pptFilePath)
                    if exists(pptFilePath) == True:
                        MyPresentation = Presentation(pptx = pptFilePath)
                    else:
                        MyPresentation = Presentation()

                    def NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                        ImageNoneRotatonOffsetX1 = 3.6
                        ImageNoneRotatonOffsetX2 = 6.8
                        ImageNoneRotatonOffsetY1 = 1.2
                        ImageNoneRotatonOffsetY2 = 4.4
                        
                        TEXTsetNoneRotatonX1 = 3.55
                        TEXTsetNoneRotatonX2 = 6.75
                        TEXTsetNoneRotatonY1 = 1.2
                        TEXTsetNoneRotatonY2 = 4.4   

                        print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        print("thePathOfTheCurrentImagesGroup is ", thePathOfTheCurrentImagesGroup)
                        print("FileListToSendToPPTpreparation[0]", FileListToSendToPPTpreparation[0])
                        # Check if the ppt exist or not.
                    
                        # Add slide 3 and add pictures in the slides
                        Slide3_Register = MyPresentation.slide_layouts[5]
                        Slide3 = MyPresentation.slides.add_slide(Slide3_Register)
                        title3  = Slide3.shapes.title
                        title3.text = thePathOfTheCurrentImagesGroup.split("\\")[-1]

                        # Add a GPS image
                        # img0 = r"C:\0Python\images\TheJob\JobB\Word.jpg"
                        img0 = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
                        from_left_0 = Inches(0.12)
                        from_top_0 = Inches(2)
                        add_picture_0 = Slide3.shapes.add_picture(img0, from_left_0, from_top_0, width =Inches(3.3), height =None)

                        # Image 1 x 1
                        # First row of images
                        img1 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")        
                        print(img1, "in ppt3")
                        from_left_1 = Inches(ImageNoneRotatonOffsetX1)
                        from_top_1 = Inches(ImageNoneRotatonOffsetY1)
                        add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =Inches(3.1), height =None)
                        # text box 2
                        left_text_box2 = Inches(TEXTsetNoneRotatonX1)
                        top_text_box2 = Inches(TEXTsetNoneRotatonY1)
                        width_text_box2 = Inches(1.7)
                        height_text_box2 = Inches(0.4)
                        tb2 = Slide3.shapes.add_textbox(left_text_box2, top_text_box2, width_text_box2, height_text_box2)
                        tf2 = tb2.text_frame
                        p_0_2 = tf2.paragraphs[0]
                        run_p_0_2 = p_0_2.add_run()
                        run_p_0_2.text = FileListToSendToPPTpreparation[0]
                        font_p_0_2 = run_p_0_2.font
                        font_p_0_2.size = Pt(20)
                        font_p_0_2.name = 'arial'
                        font_p_0_2.bold = True
                        font_p_0_2.color.rgb = RGBColor (0, 255, 0)    

                        # delete used image
                        del FileListToSendToPPTpreparation[0]
                        print("FileListToSendToPPTpreparation after 1 x 1 is ", FileListToSendToPPTpreparation)
                        if FileListToSendToPPTpreparation:
                            
                            # Image 1 x 2
                            img2 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                            from_left_2 = Inches(ImageNoneRotatonOffsetX1)
                            from_top_2 = Inches(ImageNoneRotatonOffsetY2)
                            add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =Inches(3.1), height =None)
                            # tex box 1
                            left_text_box1 = Inches(TEXTsetNoneRotatonX1)
                            top_text_box1 = Inches(TEXTsetNoneRotatonY2)
                            width_text_box1 = Inches(1.7)
                            height_text_box1 = Inches(0.4)
                            tb1 = Slide3.shapes.add_textbox(left_text_box1, top_text_box1, width_text_box1, height_text_box1)
                            tf1 = tb1.text_frame
                            p_0_1 = tf1.paragraphs[0]
                            run_p_0_1 = p_0_1.add_run()
                            run_p_0_1.text = FileListToSendToPPTpreparation[0]
                            font_p_0_1 = run_p_0_1.font
                            font_p_0_1.size = Pt(20)
                            font_p_0_1.name = 'arial'
                            font_p_0_1.bold = True
                            font_p_0_1.color.rgb = RGBColor (0, 255, 0)        

                            # delete used image
                            del FileListToSendToPPTpreparation[0]
                            print("FileListToSendToPPTpreparation after 1 x 2 is ", FileListToSendToPPTpreparation)
                            if FileListToSendToPPTpreparation:

                                # Image 2 x 1
                                img3 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                from_left_3 = Inches(ImageNoneRotatonOffsetX2)
                                from_top_3 = Inches(ImageNoneRotatonOffsetY1)
                                add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =Inches(3.1), height =None)
                                 # text box 3
                                left_text_box3 = Inches(TEXTsetNoneRotatonX2)
                                top_text_box3 = Inches(TEXTsetNoneRotatonY1)
                                width_text_box3 = Inches(1.7)
                                height_text_box3 = Inches(0.4)
                                tb3 = Slide3.shapes.add_textbox(left_text_box3, top_text_box3, width_text_box3, height_text_box3)
                                tf3 = tb3.text_frame
                                p_0_3 = tf3.paragraphs[0]
                                run_p_0_3 = p_0_3.add_run()
                                run_p_0_3.text = FileListToSendToPPTpreparation[0]
                                font_p_0_3 = run_p_0_3.font
                                font_p_0_3.size = Pt(20)
                                font_p_0_3.name = 'arial'
                                font_p_0_3.bold = True
                                font_p_0_3.color.rgb = RGBColor (0, 255, 0)

                                # delete used image
                                del FileListToSendToPPTpreparation[0]
                                print("FileListToSendToPPTpreparation after 1 x 3 is ", FileListToSendToPPTpreparation)
                                if FileListToSendToPPTpreparation:
                               
                                    # Image 2 x 2
                                    img4 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                    from_left_4 = Inches(ImageNoneRotatonOffsetX2)
                                    from_top_4 = Inches(ImageNoneRotatonOffsetY2)
                                    add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =Inches(3.1), height =None)
                                    # text box 4
                                    left_text_box4 = Inches(TEXTsetNoneRotatonX2)
                                    top_text_box4 = Inches(TEXTsetNoneRotatonY2)
                                    width_text_box4 = Inches(1.7)
                                    height_text_box4 = Inches(0.4)
                                    tb4 = Slide3.shapes.add_textbox(left_text_box4, top_text_box4, width_text_box4, height_text_box4)
                                    tf4 = tb4.text_frame
                                    p_0_4 = tf4.paragraphs[0]
                                    run_p_0_4 = p_0_4.add_run()
                                    run_p_0_4.text = FileListToSendToPPTpreparation[0]
                                    font_p_0_4 = run_p_0_4.font
                                    font_p_0_4.size = Pt(20)
                                    font_p_0_4.name = 'arial'
                                    font_p_0_4.bold = True
                                    font_p_0_4.color.rgb = RGBColor (0, 255, 0)


                                    del FileListToSendToPPTpreparation[0]
                                    print("Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
    
                                    if FileListToSendToPPTpreparation:
                                        print("============== Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                                        NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                                                       
                    NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                    MyPresentation.save(pptFilePath)
                NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
            
            if Rotation == 90:
                print("Rotation is", Rotation)
                def NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                    ImageOffsetX1 = 3.4   # The numbers need to be verified ============================================
                    ImageOffsetX2 = 6.6
                    ImageOffsetY1 = 1.2
                    ImageOffsetY2 = 4.4

                    TEXTsetX1 = 3.4
                    TEXTsetX2 = 6.6
                    TEXTsetY1 = 1.2
                    TEXTsetY2 = 4.4
                    
                    pptFilePath = os.path.join(Path0, f"{JobNumber}_{JobSubNumber}_{JobDescription}.pptx")
                    print("The pptFilePath is ", pptFilePath)
                    if exists(pptFilePath) == True:
                        MyPresentation = Presentation(pptx = pptFilePath)
                    else:
                        MyPresentation = Presentation()

                    def NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                        print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        print("thePathOfTheCurrentImagesGroup is ", thePathOfTheCurrentImagesGroup)
                        print("FileListToSendToPPTpreparation[0]", FileListToSendToPPTpreparation[0])
                        # Check if the ppt exist or not.
                    
                        # Add slide 3 and add pictures in the slides
                        Slide3_Register = MyPresentation.slide_layouts[5]
                        Slide3 = MyPresentation.slides.add_slide(Slide3_Register)
                        title3  = Slide3.shapes.title
                        title3.text = thePathOfTheCurrentImagesGroup.split("\\")[-1]

                        # Add a GPS image
                        # img0 = r"C:\0Python\images\TheJob\JobB\Word.jpg"
                        img0 = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
                        from_left_0 = Inches(0.12)
                        from_top_0 = Inches(2)
                        add_picture_0 = Slide3.shapes.add_picture(img0, from_left_0, from_top_0, width =Inches(3.3), height =None)

                        height_or_widthFactor = 1.5

                        # Image 1 x 1
                        # First row of images
                        img1 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")        
                        from_left_1 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                        from_top_1 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                        add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =None, height =Inches(height_or_width*height_or_widthFactor))
                        add_picture_1.rotation = 90
                  
                        # text box 1
                        left_text_box2 = Inches(TEXTsetX1)
                        top_text_box2 = Inches(TEXTsetY1)
                        width_text_box2 = Inches(1.7)
                        height_text_box2 = Inches(0.4)
                        tb2 = Slide3.shapes.add_textbox(left_text_box2, top_text_box2, width_text_box2, height_text_box2)
                        tf2 = tb2.text_frame
                        p_0_2 = tf2.paragraphs[0]
                        run_p_0_2 = p_0_2.add_run()
                        run_p_0_2.text = FileListToSendToPPTpreparation[0]
                        font_p_0_2 = run_p_0_2.font
                        font_p_0_2.size = Pt(20)
                        font_p_0_2.name = 'arial'
                        font_p_0_2.bold = True
                        font_p_0_2.color.rgb = RGBColor (0, 255, 0)    

                        # delete used image
                        del FileListToSendToPPTpreparation[0]
                        print("FileListToSendToPPTpreparation after 1 x 1 is ", FileListToSendToPPTpreparation)
                        if FileListToSendToPPTpreparation:
                            
                            # Image 1 x 2
                            img2 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                            #from_left_2 = Inches(5.7)
                            #from_top_2 = Inches(1.4)
                            #add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =Inches(2), height =None)
                            from_left_2 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                            from_top_2 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                            add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =None, height =Inches(height_or_width*height_or_widthFactor))
                            add_picture_2.rotation = 90
  
                            # tex box 2
                            left_text_box1 = Inches(TEXTsetX1)
                            top_text_box1 = Inches(TEXTsetY2)
                            width_text_box1 = Inches(1.7)
                            height_text_box1 = Inches(0.4)
                            tb1 = Slide3.shapes.add_textbox(left_text_box1, top_text_box1, width_text_box1, height_text_box1)
                            tf1 = tb1.text_frame
                            p_0_1 = tf1.paragraphs[0]
                            run_p_0_1 = p_0_1.add_run()
                            run_p_0_1.text = FileListToSendToPPTpreparation[0]
                            font_p_0_1 = run_p_0_1.font
                            font_p_0_1.size = Pt(20)
                            font_p_0_1.name = 'arial'
                            font_p_0_1.bold = True
                            font_p_0_1.color.rgb = RGBColor (0, 255, 0)        

                            # delete used image
                            del FileListToSendToPPTpreparation[0]
                            print("FileListToSendToPPTpreparation after 1 x 2 is ", FileListToSendToPPTpreparation)
                            if FileListToSendToPPTpreparation:

                                # Image 1 x 3
                                img3 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                #from_left_3 = Inches(7.8)
                                #from_top_3 = Inches(1.4)
                                #add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =Inches(2), height =None)
                                from_left_3 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                from_top_3 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                                add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =None, height =Inches(height_or_width*height_or_widthFactor))
                                add_picture_3.rotation = 90


                                 # text box 3
                                left_text_box3 = Inches(TEXTsetX2)
                                top_text_box3 = Inches(TEXTsetY1)
                                width_text_box3 = Inches(1.7)
                                height_text_box3 = Inches(0.4)
                                tb3 = Slide3.shapes.add_textbox(left_text_box3, top_text_box3, width_text_box3, height_text_box3)
                                tf3 = tb3.text_frame
                                p_0_3 = tf3.paragraphs[0]
                                run_p_0_3 = p_0_3.add_run()
                                run_p_0_3.text = FileListToSendToPPTpreparation[0]
                                font_p_0_3 = run_p_0_3.font
                                font_p_0_3.size = Pt(20)
                                font_p_0_3.name = 'arial'
                                font_p_0_3.bold = True
                                font_p_0_3.color.rgb = RGBColor (0, 255, 0)

                                # delete used image
                                del FileListToSendToPPTpreparation[0]
                                print("FileListToSendToPPTpreparation after 1 x 3 is ", FileListToSendToPPTpreparation)
                                if FileListToSendToPPTpreparation:
                               
                                    # Image 2 x 1
                                    img4 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                    #from_left_4 = Inches(3.5)
                                    #from_top_4 = Inches(3.5)
                                    #add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =Inches(2), height =None)
                                    from_left_4 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                    from_top_4 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                    add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =None, height =Inches(height_or_width*height_or_widthFactor))
                                    add_picture_4.rotation = 90
                     
                                    # text box 4
                                    left_text_box4 = Inches(TEXTsetX2)
                                    top_text_box4 = Inches(TEXTsetY2)
                                    width_text_box4 = Inches(1.7)
                                    height_text_box4 = Inches(0.4)
                                    tb4 = Slide3.shapes.add_textbox(left_text_box4, top_text_box4, width_text_box4, height_text_box4)
                                    tf4 = tb4.text_frame
                                    p_0_4 = tf4.paragraphs[0]
                                    run_p_0_4 = p_0_4.add_run()
                                    run_p_0_4.text = FileListToSendToPPTpreparation[0]
                                    font_p_0_4 = run_p_0_4.font
                                    font_p_0_4.size = Pt(20)
                                    font_p_0_4.name = 'arial'
                                    font_p_0_4.bold = True
                                    font_p_0_4.color.rgb = RGBColor (0, 255, 0)

                                    # delete used image
                                    del FileListToSendToPPTpreparation[0]
                                    print("Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
    
                                    if FileListToSendToPPTpreparation:
                                        print("============== Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                                        NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                                                           
                    NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                    MyPresentation.save(pptFilePath)
                NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)

            if Rotation == -90:
                print("Rotation is", Rotation)
                def NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                    ImageOffsetX1 = 3.4
                    ImageOffsetX2 = 6.6
                    ImageOffsetY1 = 1.2
                    ImageOffsetY2 = 4.4

                    TEXTsetX1 = 3.4
                    TEXTsetX2 = 6.6
                    TEXTsetY1 = 1.2
                    TEXTsetY2 = 4.4               
                    
                    pptFilePath = os.path.join(Path0, f"{JobNumber}_{JobSubNumber}_{JobDescription}.pptx")
                    print("The pptFilePath is ", pptFilePath)
                    if exists(pptFilePath) == True:
                        MyPresentation = Presentation(pptx = pptFilePath)
                    else:
                        MyPresentation = Presentation()

                    def NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup):
                        print("FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                        print("thePathOfTheCurrentImagesGroup is ", thePathOfTheCurrentImagesGroup)
                        print("FileListToSendToPPTpreparation[0]", FileListToSendToPPTpreparation[0])
                        # ActualHeightWithDefinedWidth = Inches(3.0) # None
                        SetCurrentHeightsWidthByFixedWidth = Inches(2.8) # when the heighs is much larger: refer to 4767pptCalculator.csv
                        
                        # Check if the ppt exist or not.
                    
                        # Add slide 3 and add pictures in the slides
                        Slide3_Register = MyPresentation.slide_layouts[5]
                        Slide3 = MyPresentation.slides.add_slide(Slide3_Register)
                        title3  = Slide3.shapes.title
                        title3.text = thePathOfTheCurrentImagesGroup.split("\\")[-1]

                        # Add a GPS image
                        # img0 = r"C:\0Python\images\TheJob\JobB\Word.jpg"
                        img0 = os.path.join(thePathOfTheCurrentImagesGroup, "Word.jpg")
                        from_left_0 = Inches(0.12)
                        from_top_0 = Inches(2)
                        add_picture_0 = Slide3.shapes.add_picture(img0, from_left_0, from_top_0, width =Inches(3.3), height =None)


                        # Image 1 x 1
                        height_or_widthFactorForNegative = 1.5
                        # First row of images
                        img1 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                        im1x1 = cv2.imread(img1)
                        him1x1 = im1x1.shape[0]
                        wim1x1 = im1x1.shape[1]
                        print("wim1x1 and him1x1 after rotation are ", wim1x1, him1x1)
                        from_left_1 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                        from_top_1 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                        add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =SetCurrentHeightsWidthByFixedWidth, height =None)
                        #add_picture_1 = Slide3.shapes.add_picture(img1, from_left_1, from_top_1, width =None, height =Inches(height_or_width*height_or_widthFactorForNegative))
                        add_picture_1.rotation = -90


                  
                        # text box 1
                        left_text_box2 = Inches(TEXTsetX1)
                        top_text_box2 = Inches(TEXTsetY1)
                        width_text_box2 = Inches(1.7)
                        height_text_box2 = Inches(0.4)
                        tb2 = Slide3.shapes.add_textbox(left_text_box2, top_text_box2, width_text_box2, height_text_box2)
                        tf2 = tb2.text_frame
                        p_0_2 = tf2.paragraphs[0]
                        run_p_0_2 = p_0_2.add_run()
                        run_p_0_2.text = FileListToSendToPPTpreparation[0]
                        font_p_0_2 = run_p_0_2.font
                        font_p_0_2.size = Pt(20)
                        font_p_0_2.name = 'arial'
                        font_p_0_2.bold = True
                        font_p_0_2.color.rgb = RGBColor (0, 255, 0)    

                        # delete used image
                        del FileListToSendToPPTpreparation[0]
                        print("FileListToSendToPPTpreparation after 1 x 1 is ", FileListToSendToPPTpreparation)
                        if FileListToSendToPPTpreparation:
                            
                            # Image 1 x 2
                            img2 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                            # print("shape of imWord", hWord, wWord)
                            #from_left_2 = Inches(5.7)
                            #from_top_2 = Inches(1.4)
                            #add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =Inches(2), height =None)
                            from_left_2 = Inches(ImageOffsetX1+(height_or_width/2)-half_w*0.9) ###
                            from_top_2 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) #### 
                            add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =SetCurrentHeightsWidthByFixedWidth, height =None)
                            #add_picture_2 = Slide3.shapes.add_picture(img2, from_left_2, from_top_2, width =None, height =Inches(height_or_width*height_or_widthFactorForNegative))
                            add_picture_2.rotation = -90
  
                            # tex box 2
                            left_text_box1 = Inches(TEXTsetX1)
                            top_text_box1 = Inches(TEXTsetY2)
                            width_text_box1 = Inches(1.7)
                            height_text_box1 = Inches(0.4)
                            tb1 = Slide3.shapes.add_textbox(left_text_box1, top_text_box1, width_text_box1, height_text_box1)
                            tf1 = tb1.text_frame
                            p_0_1 = tf1.paragraphs[0]
                            run_p_0_1 = p_0_1.add_run()
                            run_p_0_1.text = FileListToSendToPPTpreparation[0]
                            font_p_0_1 = run_p_0_1.font
                            font_p_0_1.size = Pt(20)
                            font_p_0_1.name = 'arial'
                            font_p_0_1.bold = True
                            font_p_0_1.color.rgb = RGBColor (0, 255, 0)        

                            # delete used image
                            del FileListToSendToPPTpreparation[0]
                            print("FileListToSendToPPTpreparation after 1 x 2 is ", FileListToSendToPPTpreparation)
                            if FileListToSendToPPTpreparation:

                                # Image 1 x 3
                                img3 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                #from_left_3 = Inches(7.8)
                                #from_top_3 = Inches(1.4)
                                #add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =Inches(2), height =None)
                                from_left_3 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                from_top_3 = Inches(ImageOffsetY1-(height_or_width/2)+half_w*1.1) ####
                                add_picture_3 = Slide3.shapes.add_picture(img3, from_left_3, from_top_3, width =SetCurrentHeightsWidthByFixedWidth, height =None)
                                add_picture_3.rotation = -90


                                 # text box 3
                                left_text_box3 = Inches(TEXTsetX2)
                                top_text_box3 = Inches(TEXTsetY1)
                                width_text_box3 = Inches(1.7)
                                height_text_box3 = Inches(0.4)
                                tb3 = Slide3.shapes.add_textbox(left_text_box3, top_text_box3, width_text_box3, height_text_box3)
                                tf3 = tb3.text_frame
                                p_0_3 = tf3.paragraphs[0]
                                run_p_0_3 = p_0_3.add_run()
                                run_p_0_3.text = FileListToSendToPPTpreparation[0]
                                font_p_0_3 = run_p_0_3.font
                                font_p_0_3.size = Pt(20)
                                font_p_0_3.name = 'arial'
                                font_p_0_3.bold = True
                                font_p_0_3.color.rgb = RGBColor (0, 255, 0)

                                # delete used image
                                del FileListToSendToPPTpreparation[0]
                                print("FileListToSendToPPTpreparation after 1 x 3 is ", FileListToSendToPPTpreparation)
                                if FileListToSendToPPTpreparation:
                               
                                    # Image 2 x 1
                                    img4 = os.path.join(thePathOfTheCurrentImagesGroup, FileListToSendToPPTpreparation[0]+"."+"jpg")
                                    #from_left_4 = Inches(3.5)
                                    #from_top_4 = Inches(3.5)
                                    #add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =Inches(2), height =None)
                                    from_left_4 = Inches(ImageOffsetX2+(height_or_width/2)-half_w*0.9) ###
                                    from_top_4 = Inches(ImageOffsetY2-(height_or_width/2)+half_w*1.1) ####
                                    add_picture_4 = Slide3.shapes.add_picture(img4, from_left_4, from_top_4, width =SetCurrentHeightsWidthByFixedWidth, height =None)
                                    add_picture_4.rotation = -90
                     
                                    # text box 4
                                    left_text_box4 = Inches(TEXTsetX2)
                                    top_text_box4 = Inches(TEXTsetY2)
                                    width_text_box4 = Inches(1.7)
                                    height_text_box4 = Inches(0.4)
                                    tb4 = Slide3.shapes.add_textbox(left_text_box4, top_text_box4, width_text_box4, height_text_box4)
                                    tf4 = tb4.text_frame
                                    p_0_4 = tf4.paragraphs[0]
                                    run_p_0_4 = p_0_4.add_run()
                                    run_p_0_4.text = FileListToSendToPPTpreparation[0]
                                    font_p_0_4 = run_p_0_4.font
                                    font_p_0_4.size = Pt(20)
                                    font_p_0_4.name = 'arial'
                                    font_p_0_4.bold = True
                                    font_p_0_4.color.rgb = RGBColor (0, 255, 0)

                                    del FileListToSendToPPTpreparation[0]
                                    print("Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
    
                                    if FileListToSendToPPTpreparation:
                                        print("============== Last leftover of FileListToSendToPPTpreparation is ", FileListToSendToPPTpreparation)
                                        NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                                       
                    NoRotationSlidesWithTypicalImagesNextLayer(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)
                    MyPresentation.save(pptFilePath)
                NoRotationSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup)

        FourImagesWithoutScallingSlidesWithTypicalImages(JobNumber, JobSubNumber, JobDescription, FileListToSendToPPTpreparation, thePathOfTheCurrentImagesGroup, Rotation)





