faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

4 of 9 people (44%) answered Yes
Recently 2 of 7 people (29%) answered Yes

Entry

Examing file using COM/PythonWin

Jul 5th, 2000 10:00
Nathan Wallace, Hans Nowak, Snippet 126, Dirk-Ulrich Heise


"""
Packages: operating_systems.windows
"""
"""
Here's a sample that explains how to run across all lines in a file
using the editor of Visual C++ 5, via PythonWin and its COM magic.
"""
# EXALINES.PY         
# 06.07.99
# d heise
#
# here, EXALINES stands for "example" as well as for 
# "examine", it seems...
#
# To see this script in action, follow these steps:
# 1.) Download and install a PythonWin (www.python.org)
# 2.) Open the Developer studio (VC++ 5) and load one
#     of your text files
# 3.) Bring up PythonWin
# 4.) Put this file into a suitable directory, where 
#     PythonWin can find it. PythonWin allows you to edit
#     its search path: Main Menu "Tools","edit PythonPath",
#     select Pythonpath in the treeview, double clic
#     the (default) under "name" and insert your directory.
# 5.) In PythonWins prompt, do "import exalines"
#     (Attention, NT/Python is case sensitive for the file name.)
#
# Try to find out how many lines a text files has 
# iterate oevr the lines and print out all that exceed
# a given length      
import win32com.client                           
import string         
FALSE = 0                                        
TRUE = 1                                         
MAXLEN = 78           
  # this defines a length lines shouldn't exceed
def Do():             
  global MAXLEN       
  o = win32com.client.Dispatch("MSDev.Application")                
  doc = o.ActiveDocument
  t = doc.Type        
  print "Type of the document is ="+t                
  if t == "Text":     
    # Now try to find out how many lines it has.
    sel = doc.Selection
    sel.SelectAll()   
    n = sel.BottomLine
    print "The BottomLine is ",n
    sel.StartOfDocument()
    for i in range(1,n+1): 
      # for all lines in the document
      # sel.CurrentLine = i -- no, because CurrentLine is read only
      sel.GoToLine(i) 
      sel.SelectLine()                 
        # attention, this one moves the cursor a line down SOMETIMES!   
        # don't know the rules at the moment
      sel.Untabify()  
      s = sel.Text                                                   
      # cut off whitespace and CRLFs at the right:                   
      s= string.rstrip(s)                                            
      # print s                                                        
      if len(s) > MAXLEN:
        print "Line ",i," is too long: ",s
      # sel.LineDown()                                               
      # in general, it can be more cute to use GoToLine, see above
Do()                  
print "EXALINES loaded. Do EXALINES.Do() to get the action again."
# eof