GIDForums  

Go Back   GIDForums > Computer Programming Forums > Python Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Sep-2006, 15:47
derkin derkin is offline
New Member
 
Join Date: Sep 2006
Location: Utah
Posts: 2
derkin is on a distinguished road

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


  #2  
Old 07-Sep-2006, 00:31
crystalattice's Avatar
crystalattice crystalattice is offline
Flame War Instigator
 
Join Date: Apr 2004
Location: San Diego
Posts: 1,543
crystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nicecrystalattice is just really nice

Re: binding to drive ms word like a user.


I haven't tried integrating Python w/ Office yet but this looks interesting. At a minimum it's need to see how the bindings work. Thanks.
__________________
Common Sense v2.0-Striving to make the world a little bit smarter.
  #3  
Old 07-Sep-2006, 07:45
derkin derkin is offline
New Member
 
Join Date: Sep 2006
Location: Utah
Posts: 2
derkin is on a distinguished road

Re: binding to drive ms word like a user.


This is more of an automated testing approach.

If I wanted to write a program to integrate with word I would use com since words com system is fairly good.
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help getting started on a database search program Heresy C Programming Language 13 03-May-2006 13:56
Selling Server darkneal128 Managed / Dedicated Servers 4 28-Mar-2006 17:01
System drive set to E: not C: ldriskell Computer Software Forum - Windows 7 27-Jul-2005 00:17
Finding a word in a 2d grid The_Kingpin C Programming Language 4 24-Feb-2005 19:53
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 22-Nov-2004 23:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:44.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.