
06-Sep-2006, 15:47
|
|
New Member
|
|
Join Date: Sep 2006
Location: Utah
Posts: 2
|
|
|
binding to drive ms word like a user.
I had to test a macro integration for work and they wanted it automated. I hope some one here will be able to use it. Or learn from it in some way.
Python Code:
#word
#Simple word binding to allow test intagration to work.
# Author: Erin
#
# Changelog
# Author Date Description
# Erin 1-Sep-2006 Initial Draft
from win32com.client import Dispatch
import winGuiAuto, win32gui, win32api, win32con, time
class Word:
'''Binding for ms word. This is a simple one that just allows for a rudamentery
Editing of the documetn as well as a couple functions to do a save and save as.
and deal with the netdocuments word object.'''
def __init__(self, documentName):
self.wordHwnd = None
self.shell = Dispatch("WScript.Shell")
self.docName = documentName
def findWordObject(self):
'''this little function will find the instance of word that matches the
name given it. every function will call this. Since the handle will change
as the word jets opend and closed over the process of the test.'''
self.wordHwnd = winGuiAuto.findTopWindow(self.docName)
def focus(self):
'''this fixes ishpeks hack with the browser'''
ablPos = win32gui.GetWindowRect(self.wordHwnd)
width = ablPos[2] - ablPos[0]
hieght = ablPos[3] - ablPos[1]
win32gui.SetWindowPos(self.wordHwnd, win32con.HWND_TOPMOST, 0,0,width,hieght, win32con.SWP_NOMOVE)
win32gui.SetWindowPos(self.wordHwnd, win32con.HWND_NOTOPMOST, 0,0,width,hieght, win32con.SWP_NOMOVE)
def waiteForDocToOpen(self, lenToWaite=10):
for ii in xrange(1,lenToWaite):
try:
dochand = winGuiAuto.findTopWindow(self.docName)
return True
except:
pass
return False
def bringWordToFrount(self):
'''makes shure word is the program taking input.'''
#this should always work but who knows if i got it right.
self.findWordObject()
#self.focus()
winGuiAuto.leftClickObjByMouse(self.wordHwnd, 8, 40)
def textInsert(self,desiredText):
'''inserts text in to the current possion of the word document'''
self.bringWordToFrount()
self.shell.SendKeys(desiredText)
time.sleep(4)
def textClear(self):
'''deletes all the text in the file'''
self.bringWordToFrount()
self.shell.SendKeys("^a")
time.sleep(2)
self.shell.SendKeys("{DELETE}")
time.sleep(2)
def docEdit(self, desiredText):
'''this will write new info into the document'''
self.textClear()
self.textInsert(desiredText)
def docAppend(self, desiredText):
'''will add text at the end of the document.'''
self.bringWordToFrount()
self.shell.SendKeys("^{END}")
self.shell.SendKeys("{ENTER}")
self.textInsert(desiredText)
def docSave(self):
self.bringWordToFrount()
self.shell.SendKeys("%fs")
time.sleep(3)
def docExit(self, desiredAction="save",autoChecking=False):
'''this will close out a word documetn desired action can take
3 values 'save'-clicks the yes, 'nosave'-dosn't save the document
'cancle'-clicks the cancle button.'''
self.doCloseStuff("%fx",desiredAction)
def docMenuClose(self, desiredAction="save",autoChecking=False):
'''this will close out a word documetn desired action can take
3 values 'save'-clicks the yes, 'nosave'-dosn't save the document
'cancle'-clicks the cancle button.'''
self.doCloseStuff("%fc",desiredAction)
def docClose(self, desiredAction="save"):
'''this will close out a word documetn desired action can take
3 values 'save'-clicks the yes, 'nosave'-dosn't save the document
'cancle'-clicks the cancle button.'''
self.doCloseStuff("%{F4}",desiredAction)
def doCloseStuff(self desiredKeys,, desiredAction):
'''this will close out a word documetn desired action can take
3 values 'save'-clicks the yes, 'nosave'-dosn't save the document
'cancle'-clicks the cancle button.'''
desiredText = {"save":'&Yes',"nosave":'&No','cancle':'Cancel'}
self.bringWordToFrount()
self.shell.SendKeys(desiredKeys)
time.sleep(3)
hwnd = winGuiAuto.findTopWindow("Microsoft Office word")
dButton = winGuiAuto.findControl(hwnd,desiredText[desiredAction.strip().lower()])
winGuiAuto.leftClickObjByMouse(dButton)
class WordException(Exception):
pass
|