title: Selecting and deleting a Line Present in a Word Document Using Python / Win32
body: <p>I'm trying to automate Word(2010) documents (all most 40-50 docs) using python and win32 component. Specifically in that, need to select a part of line and replace it all together with some content. For Example, if in the original file there is "<strong>Label:  096-4296-05A</strong> ", I want it to be replaced by "<strong>Label: <em>_</em>__<em>_</em>__<em>_</em>____</strong> ". Using search and replace will WORK only if the numbers present in all the files are same, but actually they are not. So in that case, I want to have a generic approach to perform this task.</p>
  
  <p>So what I'm thinking is, if by someway I could select the line containing "<strong>Label 096-4296-05A</strong> " and delete it and then again write a new line like "<strong>Label <em>_</em>__<em>_</em>__<em>_</em></strong>".</p>
  
  <p>For this I did had a look @ Selection Object <a href="http://msdn.microsoft.com/en-us/library/bb221235%28v=office.12%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/bb221235%28v=office.12%29.aspx</a> and <a href="http://msdn.microsoft.com/en-us/library/bb208865%28v=office.12%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/bb208865%28v=office.12%29.aspx</a> and even tried to write some equivalent python code for VB.</p>
  
  <p>Here is what I have written till now :</p>
  
  <p>...///</p>
  
  <pre><code>########################
  #
  #   Purpose : Replace all occurrences of `find_str` with `replace_str`
  #             in `word_file
  #
  #######################
  
  def delete_and_add_line(word_file, find_str, replace_str):
      wdFindContinue = 1
      wdReplaceAll = 2
  
      # Dispatch() attempts to do a GetObject() before creating a new one.
      # DispatchEx() just creates a new one.
      app = win32com.client.DispatchEx("Word.Application")
  
      app.Visible = 0
      app.DisplayAlerts = 0
      app.Documents.Open(IP_Directory_Dest + "\\" + word_file) ## (word_file)
  
      # expression.Execute(FindText, MatchCase, MatchWholeWord,
      #   MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
      #   Wrap, Format, ReplaceWith, Replace)
      app.Selection.Find.Execute(find_str, True, True, \
          False, False, False, True, \
          wdFindContinue, False, replace_str, wdReplaceAll)
  
      app.Selection.EndKey(Extend=win32com.client.constants.wdExtend)##.Select()
  
      # determine if the text is selected or not
      if (app.Selection.Type == win32com.client.constants.wdSelectionIP ):
          print 'Nothing is selected'
      else:
          print 'Text Selected '
  
      # to delete the selected line
      app.Selection.Delete()
  
      app.ActiveDocument.Close(SaveChanges=True)
      app.Quit()
  </code></pre>
  
  <p>...///</p>
  
  <p>When I execute this code, I found that app.Selection.Find.Execute is successfully able to find and replace the text which is provided to it.
  Even it prints "Text Selected", which means that the text to end of line is selected , but it never deletes the Selected Line. </p>
  
  <p>Also, I'm not sure if this is the correct way to fully select a line till its End (using Select with this gives me attribute error "AttributeError: 'int' object has no attribute 'Select'")</p>
  
  <pre><code>   **### **IS THIS THE CORRECT WAY TO SELECT A LINE TILL ITS END** ???**
     app.Selection.EndKey(Extend=win32com.client.constants.wdExtend)##.Select()
  </code></pre>
  
  <p>Let me know if I'm missing something over here. Any suggestions are welcome.</p>