Difference between revisions of "Python COM Examples"

From PeformIQ Upgrade
Jump to navigation Jump to search
Line 21: Line 21:


You will have created a file in your root C: drive called myBook.xls that is an Excel file containing one cell in which the words “Hello World!” are written. Please note that this COM usage is not limited to Python. You can do this in whatever language that supports COM. I’ll leave it up to the reader to learn more about the specifics of the different COM objects that are out there.
You will have created a file in your root C: drive called myBook.xls that is an Excel file containing one cell in which the words “Hello World!” are written. Please note that this COM usage is not limited to Python. You can do this in whatever language that supports COM. I’ll leave it up to the reader to learn more about the specifics of the different COM objects that are out there.
============================================================================


<pre>
<pre>
Line 42: Line 40:
You are trying to read the page before it is completely loaded  
You are trying to read the page before it is completely loaded  


either add a wait function or a simple sleep (see below)


either add a wait function or a simple sleep ( see below)
<pre>
 
</pre>


from win32com.client import Dispatch  
from win32com.client import Dispatch  
import time  
import time  


ie = Dispatch("InternetExplorer.Application")  
ie = Dispatch("InternetExplorer.Application")  


ie.Navigate("https://secure.authorize.net/")  
ie.Navigate("https://secure.authorize.net/")  
# ie.Visible = True  
# ie.Visible = True  


time.sleep(1)  
time.sleep(1)  
doc = ie.Document  
doc = ie.Document  


print doc.body.innerHTML  
print doc.body.innerHTML  
Line 71: Line 63:
from win32com.client import Dispatch  
from win32com.client import Dispatch  
import time  
import time  


def wait(ie):  
def wait(ie):  
Line 79: Line 69:
     while ie.Document.readyState != "complete":  
     while ie.Document.readyState != "complete":  
         time.sleep(0.5)  
         time.sleep(0.5)  


ie = Dispatch("InternetExplorer.Application")  
ie = Dispatch("InternetExplorer.Application")  


ie.Navigate("https://secure.authorize.net/")  
ie.Navigate("https://secure.authorize.net/")  
# ie.Visible = True  
# ie.Visible = True  


wait(ie)  
wait(ie)  


doc = ie.Document  
doc = ie.Document  


print doc.body.innerHTML  
print doc.body.innerHTML  

Revision as of 11:31, 14 December 2009

Examples

Try this Python-based COM automation “Hello World” code in XSI just for kicks!

import win32com.client
from win32com.client import constants as c

excel = win32com.client.Dispatch("Excel.Application")
book = excel.Workbooks.Add()
sheet = book.Worksheets(1)
sheet.Range("A1").Value = "Hello World!"
sheet.Range("A2").Value = str(Application.SIFilter(None, c.siObjectFilter))
book.SaveAs("c:\myBook.xls")

sheet = None
book = None
excel.Quit()
excel = None

You will have created a file in your root C: drive called myBook.xls that is an Excel file containing one cell in which the words “Hello World!” are written. Please note that this COM usage is not limited to Python. You can do this in whatever language that supports COM. I’ll leave it up to the reader to learn more about the specifics of the different COM objects that are out there.

def index():
	import win32com.client
	a=win32com.client.Dispatch('Excel.Application')
	a.Visible = 1
	a.Workbooks.Add()
	a.ActiveSheet.Cells(1,1).Value = 'aha'
	return "aha"

if __name__ == "__main__":
	print index()


But for your problem:

You are trying to read the page before it is completely loaded

either add a wait function or a simple sleep (see below)


from win32com.client import Dispatch 
import time 

ie = Dispatch("InternetExplorer.Application") 

ie.Navigate("https://secure.authorize.net/") 
# ie.Visible = True 

time.sleep(1) 
doc = ie.Document 

print doc.body.innerHTML 

or with a wait function

from win32com.client import Dispatch import time

def wait(ie):

   while ie.Busy: 
       time.sleep(0.5) 
   while ie.Document.readyState != "complete": 
       time.sleep(0.5) 

ie = Dispatch("InternetExplorer.Application")

ie.Navigate("https://secure.authorize.net/")

  1. ie.Visible = True

wait(ie)

doc = ie.Document

print doc.body.innerHTML