Difference between revisions of "Python COM Examples"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) (New page: <pre> ============================================================================ Try this Python-based COM automation “Hello World” code in XSI just for kicks! import win32com.clie...) |
PeterHarding (talk | contribs) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Examples= | |||
Here is a simple Python-based COM automation 'Hello World' script. | |||
<pre> | |||
import win32com.client | import win32com.client | ||
from win32com.client import constants as c | from win32com.client import constants as c | ||
Line 12: | Line 12: | ||
sheet.Range("A1").Value = "Hello World!" | sheet.Range("A1").Value = "Hello World!" | ||
sheet.Range("A2").Value = str(Application.SIFilter(None, c.siObjectFilter)) | sheet.Range("A2").Value = str(Application.SIFilter(None, c.siObjectFilter)) | ||
book.SaveAs("c:\ | book.SaveAs("c:\simple_example.xls") | ||
sheet = None | sheet = None | ||
Line 18: | Line 18: | ||
excel.Quit() | excel.Quit() | ||
excel = None | excel = None | ||
</pre> | |||
This will create an Excel spreadsheet in the root C:\ drive, called 'simple_example.xls'. | |||
<pre> | |||
def index(): | def index(): | ||
import win32com.client | import win32com.client | ||
Line 33: | Line 33: | ||
if __name__ == "__main__": | if __name__ == "__main__": | ||
print index() | print index() | ||
</pre> | |||
=COM Automation of Internet Explorer= | |||
A couple of examples using either a wait function or a simple sleep for synchronisation. | |||
<pre> | |||
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 | ||
</pre> | |||
or with a wait function | or with a wait function | ||
<pre> | |||
from win32com.client import Dispatch | |||
import time | import time | ||
def wait(ie): | def wait(ie): | ||
Line 76: | Line 68: | ||
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 | ||
</pre> | |||
[[Category:Python]] | |||
[[Category:PyWin]] | |||
[[Category:Development]] | |||
Latest revision as of 12:37, 14 December 2009
Examples
Here is a simple Python-based COM automation 'Hello World' script.
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:\simple_example.xls") sheet = None book = None excel.Quit() excel = None
This will create an Excel spreadsheet in the root C:\ drive, called 'simple_example.xls'.
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()
COM Automation of Internet Explorer
A couple of examples using either a wait function or a simple sleep for synchronisation.
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/") # ie.Visible = True wait(ie) doc = ie.Document print doc.body.innerHTML