Speak what I type

This script uses the Microsoft Speech SDK to speak what you type in
from the keyboard. To stop the script use STRG+Z. MS Speech Engine has
to be installed and makepy should be executed for the MS Speech Object
Library.

import sys
from win32com.client import constants
import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")
print "Type word or phrase, then enter."
print "Ctrl+Z then enter to exit."
while 1:
   try:
      s = raw_input()
      speaker.Speak(s)
   except:
      if sys.exc_type is EOFError:
         sys.exit()

Shellexecute – Printing with the default application

This script prints the file in the first argument with the windows default application.

Example:

If you save the code in the file print.py:

print.py test.doc -> Opens Microsoft Word (if it is installed) and prints the file test.py to the default printer.

print.py mytextfile.txt -> Same. But prints with Notepad

from win32api import ShellExecute
from sys import argv

ShellExecute(0, "print", argv[1], None, "", 1)

Directory Watcher

You can use this script, if you want to watch for activity on a
directory. It shows you every write and delete of a file in a specified
directory.

import os, time
path_to_watch = "c:\temp"
before = dict ([(f, None) for f in os.listdir (path_to_watch)]) 
while 1: 
  time.sleep (10) 
  after = dict ([(f, None) for f in os.listdir (path_to_watch)]) 
  added = [f for f in after if not f in before] 
  removed = [f for f in before if not f in after] 
  if added: print "Added: ", ", .join (added) 
  if removed: print "Removed: ", ", ".join (removed) 
  before = after

Show all processes with IDs

This script shows all running processes with their process IDs. It uses the wmi-class from Tim Golden. Please put it into the directory where you run this script from.

import wmi 
c = wmi.WMI () 
for process in c.Win32_Process ():
    print process.ProcessId, process.Name 

Executing Javascript function in a web page in IE

The following code executes a Javascript function on a web page using IE and COM.

import win32com.client, pythoncom
from time import sleep

ie=win32com.client.Dispatch('internetexplorer.application')
ie.Visible=1
ie.Navigate('http://www.cpplab.com/Articles/JSCalls/TestPage/JSCallTestPage.htm')
sleep(5) # Give it time to get there.

id=ie.Document.Script._oleobj_.GetIDsOfNames('Multiply') # 'Multiply' is the function name.
#5 and 2 below are the parameters passed to the function 'Multiply'.
res=ie.Document.Script._oleobj_.Invoke(id, 0, pythoncom.DISPATCH_METHOD, True, 5, 2)

Creating multiple instances of IE COM browser objects.

The following code allows you to create more than one IE browser session and interact with each different one.

# Use the makepy utility for the "Microsoft Internet Controls (1.1)"

# object library to get early binding.
from win32com.client import Dispatch
import pythoncom

ie1 = pythoncom.CoCreateInstance("InternetExplorer.Application", None,\
pythoncom.CLSCTX_SERVER,
pythoncom.IID_IDispatch)
ie1 = Dispatch(ie1)

ie2 = pythoncom.CoCreateInstance("InternetExplorer.Application", None,\
pythoncom.CLSCTX_SERVER,
pythoncom.IID_IDispatch)
ie2 = Dispatch(ie2)

# and so on.....

Printing Webpages from Internet Explorer with Python

This script calls the webpage www.heise.de in Internet Explorer and prints it to the standard printer.

Attention: use the makepy.py utility (C:\\Python23\\Lib\\site-packages\\win32com\\client\\) on the “Microsoft Internet Controls (1.1)” object library to get early binding !!! Otherwise you cannot print !

# print a webpage in Internet Explorer (COM Example)
# Attention: use the makepy utility for the "Microsoft Internet Controls (1.1)"
# object library to get early binding !!!

from win32com.client import Dispatch
from time import sleep

ie = Dispatch(“InternetExplorer.Application”)
ie.Visible = 1
ie.Navigate(“http://www.heise.de”)
if ie.Busy:
sleep(2)
# print the current IE document without prompting the user for the printerdialog
ie.ExecWB(win32com.client.constants.OLECMDID_PRINT, \
win32com.client.constants.OLECMDEXECOPT_DONTPROMPTUSER)

Print the Volume of a body from CATIA V5

try:
    import win32com.client
except: 
    print "Ooops Pywin32 needed !" 
app=win32com.client.Dispatch('CATIA.Application') 
doc=app.ActiveDocument.Product 
a=doc.Analyze.Volume 
print "The Volume is: ", a

Generating BOM (bill of material) from CATIA V5

To test this sample you should have installed Pywin32 and start CATIA V5 before running this script.

import win32com.client 
catapp = win32com.client.Dispatch("CATIA.Application") 
doc=catapp.ActiveDocument.Product
product_count = doc.Products.Count 
print "This example prints all Parts and subproducts of \
  a CATProduct up to the 2nd level." 
for products in range(product_count): 
  products = products + 1 
  print doc.Products.Item(products).Name, ":" 
  part_count = doc.Products.Item(products).Products.Count 
  for parts in range(part_count): 
    parts = parts +1 
    print " ", doc.Products.Item(products).Products.Item(parts).Name
For a HTML-generated Bill of materialyou should better use the BOM-generation from CATIA V5:
import win32com.client 
catapp = win32com.client.Dispatch("CATIA.Application") 
doc=catapp.ActiveDocument.Product
doc.Product.ExtractBOM(2, "c:\BOM.html")

CATIA V4 and CATPart to VRML Converter

A CATPart to VRML Batchconverter:

The next example uses the \"CATIA V5 Interfaces Object Library\" to
connect to CATIA V5, open a CATPart and export it to VRML. You can do
that with other formats like IGES, STEP, … too (if you have the
licence).

You need Python and Pywin for this example and have also running CATIA V5:

# Demo for converting a CATPart to VRML
# Please start CATIA V5 before running this script

from win32com.client.dynamic import Dispatch
catapp = Dispatch("CATIA.Application")
catapp.Documents.Open("D:\work\Testmodell.CATPart")
catapp.ActiveDocument.ExportData('D:\work\Testmodell.wrl', 'wrl')

A CATIA V4 model to VRML Batchconverter:

You can`t do this interactive in CATIA V5 but with Python. Simply
open a CATIA V4 model and export it to VRML (I did this with R14):

# Demo for converting a CATIA V4 model to VRML
# Please start CATIA V5 before running this script
# Demo for converting a CATIA V4 model to VRML
# Please start CATIA V5 before running this script
from win32com.client.dynamic import Dispatch
catapp = Dispatch("CATIA.Application")
catapp.Documents.Open("D:\work\Testmodell.model")
catapp.ActiveDocument.ExportData('D:\work\Testmodell.wrl', 'wrl')