Word : Deleting Lines in between a Table and a Heading using Python


I have a scenario in which there is a Heading/Constant Text like <strong>"Call Tree:"</strong> present all over the Word document and after that there are some lines, and once the line are over, there is a <strong>Table</strong>. So I want the lines between the table and the Heading/Constant Text <strong>"Call Tree:"</strong> to be deleted using python/win32 component.

For Example :

<strong>Input is :</strong>

... Call Tree : Line 1 ... Line 2 ... .... .... .... .... Line N .... Table # 1 .....

<strong>Output is</strong> (i.e. all the lines in between the table and "Call Tree" are deleted):

... Call Tree : Table # 1 .....

I'm not sure, if there is any way by which I can select multiple lines between a Constant Text i.e "Call Tree" and a Table. I know, that selection of a line and deletion can be done using this :

.. app = win32com.client.DispatchEx("Word.Application") app.Visible = 0 app.DisplayAlerts = 0 # select a Text app.Selection.Find.Execute("TEXT TO BE SELECTED") # extend it to end app.Selection.EndKey(Unit=win32com.client.constants.wdLine,Extend=win32com.client.constants.wdExtend) # check what has been selected app.Selection.Range() # and then delete it app.Selection.Delete() ..

But I'm not sure, how to select multiple lines like this, with this criteria.

Any idea/suggestion on this ?


You can iterate over the lines after you find the text using MoveRight and EndKey, then you can test if the selection is a table using the property Tables. Someting like

import win32com.client as com from win32com.client import constants as wcons app = com.Dispatch('Word.Application') # Find the text app.Selection.Find.Execute('Call Tree', False, False, False, False, False, True, wcons.wdFindContinue, False, False,False,) # Extend the selection to the end app.Selection.EndKey(Unit=wcons.wdLine) while True: # Move the selection to the next character app.Selection.MoveRight(Unit=wcons.wdCharacter, Count=1) # And Select the whole line app.Selection.EndKey(Unit=wcons.wdLine, Extend=wcons.wdExtend) # If I hit the table... if app.Selection.Tables.Count: print "Table found... Stop" # I leave the loop break # Otherwise delete the selection print "Deleting ...",app.Selection.Text app.Selection.Delete() # And delete the return app.Selection.TypeBackspace()

I would recommend trying to do what you want first in word using macros (Try recording the macros if you are not familiar with VBA) and then translate the code to Python.


  • Initializing and destroying Python multiprocessing workers
  • Python program displaying messages in different language than english
  • Word : Deleting Lines in between a Table and a Heading using Python
  • Can I use win32com client for macOS?
  • How to connect to TT X_TRADER API in order to create an automated trading system using python?
  • Deleting worksheets with looping
  • No module named win32com
  • Excel VBA , open workbook , copy and add to last line on another workbook
  • Open Sharepoint Excel Files With VBA
  • Using python win32com can't make two separate tables in MS Word 2007
  • Python to delete a row in excel spreadsheet
  • Excel VBA code to move worksheets with image add screen updating and it errors
  • Javascript delete cookie before reload or redirect
  • Reusing the CQ5 Form into the mywebsite components is not showing up the End of the Form section
  • Swift - pruning elements from an Array, converting integer strings to integers
  • Accessing another variable in the same class with a click event
  • Why can't UI components be accessed from a backgroundworker?
  • Flash radiobutton: how do I get the selected radiobutton?
  • Upload file that is in the cpan database
  • How can I restyle a word when rendering a pdf with pdf.js?
  • How to getText() from the input field of an angularjs Application
  • Security issues with PHP's Readfile method
  • Conversion from string “a” to type 'Boolean' is not valid
  • onBackPressed() not being executed
  • CakePHP 2.0.4 - findBy magic methods with conditions
  • req.body is undefined - nodejs
  • C# - Serializing and deserializing static member
  • Sending data from AppleScript to FileMaker records
  • Symfony2: How to get request parameter
  • Delete MySQLi record without showing the id in the URL
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • Data Validation Drop Down Box Arrow Disappearing
  • How to include full .NET prerequisite for Wix Burn installer
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is it possible to post an object from jquery to bottle.py?
  • Is there any way to bind data to data.frame by some index?
  • Programmatically clearing map cache
  • How can i traverse a binary tree from right to left in java?