'How to set Azure classification (Internal, Confidential, etc.) with python

I have a script I've written that has a section where it copies the 1st sheet of one excel file into a new file and then saves. However, when the file is saved for the 1st time, I am prompted to select a label for the document (Internal, Confidential, etc.). I couldn't figure out a way to interact with this window directly, so I wrote a script to detect the window and then use the mouse to click the necessary inputs. When I tested this script on its own, it would work fine, but when I try using it in the script that copies the excel file, it does not work:

from win32com.client import Dispatch
import pyautogui
import pygetwindow as gw

def copyExcel():
    xl = Dispatch("Excel.Application")
    path1 = r"C:\Users\Me\Desktop\New folder\Oldbook.xlsx"
    path2 = r"C:\Users\Me\Desktop\New folder\Newbook.xlsx"
    wb1 = xl.Workbooks.Open(Filename=path1)
    wb2 = xl.Workbooks.Open(Filename=path2)
    ws1 = wb1.Worksheets(1)
    ws1.Copy(Before=wb2.Worksheets(1))
    wb1.Close()
    wb2.Close(SaveChanges=True)
    closeAzureProtection()

def closeAzureProtection():
    if 'Microsoft Azure Information Protection' in 
            pyautogui.getAllTitles():
            print("Closing Azure")
            #Minimize all windows
            pyautogui.keyDown('winleft')
            pyautogui.press('d')
            pyautogui.keyUp('winleft')
 
            #Find AzureProtection Window and move to main monitor
            maip = gw.getWindowsWithTitle("Microsoft Azure Information 
            Protection")[0]
            maip.activate()
            maip.moveTo(10,10)

            #Click on Confidential
            pyautogui.click(333, y=83, duration = 0.25)
            pyautogui.PAUSE = 1
            
            #Click on Ok
            pyautogui.click(388, y=129, duration = 0.25)

            print("Azure Closed")
    else:
            print("Azure Not Found")

copyExcel()

Any ideas on how to fix this would be appreciated!



Sources

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

Source: Stack Overflow

Solution Source