Installation of Python IDE
1. Python has multiple types of IDEs (Integrated Development Environments) such as PyCharm, Visual Studio Code, and Visual Studio Community. Here we use Visual Studio Code. Visual Studio Code is a source code editor developed by Microsoft. After the installation is complete, start Visual Studio Code and install Python and Pylance. It is important that Python, pip, and Pylance are installed during installation. You need to install Python and Pylance manually while pip will be install with Python 3.4 or later automatically.
2. Install Pywin32 by use of pip. Pywin32 is an additional library that allows you to call Windows APIs from Python. Here, a library called win32com.client included in Pywin32 is used to create a COM (Component Object Model) client or server. It is a library that is also needed when using Active X to control Excel and Word, and is frequently used. When installing Pywin32, start the command prompt with administrator privileges and start the installation with the following command.
py -m pip install pywin32
Run Python Code on Your Environment
Firstly, let's create a simple *.py file and check that python can be executed as expected.
1. After launching Visual Studio Code, select New File
to create a new file. Explorer allows you to see currently open files and files on your desktop. There are multiple programming languages available on Visual Studio Code, so first save this file as *.py. Here, save it as hello.py.
2. Enter the following code in hello.py. If you press the execute button on the upper right, you will get the response hello, python as shown below.
3. (Optional) If you have Excel and can be controlled by ActiveX, you can start Excel from Python by creating the following Python code. You have to put valid file path for "C:\Users\user\Desktop\test.xls". It will create a *.xls file and write a text on the selected cell.
Excel = win32com.client.Dispatch("Excel.Application")
ExcelWorkbook = Excel.Workbooks.Add()
ExcelWorkbook.ActiveSheet.Cells(1, 1).Value = "hello, excel"
If this script works, you can use Active X through Pywin32 on your environment.
Launch DIAdem from Python
1. To create a DIAdem COM by win32com.client of Pywin32, you need following codes. You can control DIAdem from Python via Active X by creating a new *.py file and enter the code below.
DIAdem = win32com.client.Dispatch("DIAdem.ToCommand")
DIAdem.bDontCloseApplication = True
2. The VBScript file selected by 'C:\Users\username\Desktop\Msgbox.vbs' is executed on DIAdem. You can create Msgbox.vbs by Notepad by saving the following text in Notepad as Msgbox.vbs. When you double click Msgbox.vbs, you wil see a message box with the text.
MsgBox("This is DIAdem Script.")
3. After creating Msgbox.vbs, you need to adjust the file path of C:\Users\username\Desktop\Msgbox.vbs in the python code.
4. Executing Python, DIAdem will be started, Msgbox.vbs will be executed. The message box will be displayed.