1

I'm trying to update a microsoft word - 2010 table by deleting its contents (except the first row contents) using python and win32com client component. I even looked at the MSDN library (http://msdn.microsoft.com/en-us/library/bb244515.aspx ) and found that Delete could help me in this, something like this. (Also had a look @ How to read contents of an Table in MS-Word file Using Python? )

..///

# delete row 1 of table 1
doc.Tables(1).Rows(1).Delete
# delete cell 1,1 of table 1
doc.Tables(1).Cell(1, 1).Delete

..///

but on doing the above steps, the table row isn't deleted (neither the cell [1,1] is deleted). Is there something that I'm missing ? Any suggestions are welcome.

Python function for clearing the table contents is pasted herein with

..//

def updateTable(name):

    #tell word to open the document
    word.Documents.Open (IP_Directory_Dest + "\\" + name)

    #open it internally (i guess...)
    doc = word.Documents(1)

##    doc.Content.Text = "This is the string to add to the document."
##    doc.Content.MoveEnd()

##    doc.Content.Select
##    doc.Tables(1).Rows(2).Select
##    Selection.InsertRowsBelow

##    doc.Tables [1]. Rows [1]. Cells [1]. Range.Text = '123123 '
##    doc.Tables [1]. Rows.Add () # add a line

    # specifically select TABLE # 1
    table = doc.Tables(1)
    # count the number of rows in TABLE # 1
    numRows = table.Rows.Count

    # count number of columns
    numCols = table.Columns.Count

    print ('Number of Rows in TABLE',numRows)
    print ('Number of Columns in TABLE',numCols)

    # print the row 1 of TABLE # 1 -- for checking
    print ('### 1 - CHECK this ONE ... ',table.Rows(1).Range.Text)

    # delete row 1 of table 1
    doc.Tables(1).Rows(1).Delete
    # delete cell 1,1 of table 1
    doc.Tables(1).Cell(1, 1).Delete

    # again count the number of rows in table
    numRows = table.Rows.Count

    print numRows

    # print the row 1 of TABLE # 1 -- after Deleting the first ROW --> for checking
    print ('### 2 - CHECK this ONE ... ',table.Rows(1).Range.Text)

    # get the number of tables
    numberTables = doc.Tables.Count
    # count the number of tables in document
    print numberTables

    #delete ALL tables
    tables = doc.Tables
    for table in tables:
        # to get  the content of Row # 1, Column # 1 of table
        print table.Cell(Row =1, Column = 1).Range.Text
##        table.Delete(Row =2)
        # this one deletes the whole table (which is not needed)
        #table.Delete()

    #re-save in IP folder
    doc.SaveAs(IP_Directory_Dest + "\\" + name)

    #close the stream
    doc.Close()

...///

Please ignore the commented out sections (I was also trying to make the stuff work)

Community
  • 1
  • 1
Varun
  • 107
  • 3
  • 10

2 Answers2

1

All,

So this is what I have figured out. I'm going to share the code which I have written to fix it.

While doing this, I learned how to clear content of a table (specific row and column), how to add a row, get the count of columns and rows in a word table et al. Also I figured out, since there in not much documentation available on the API's used for python/win32 (except the MSDN library), the one way which i thought to get used to these APIs is to understand the VB code (mostly its present @ MSDN http://msdn.microsoft.com/en-us/library/bb244515.aspx ) and try to make a corresponding similar code for python-win32 too. Thats my understanding.

..///

########################
#
#   Purpose : To update the Table contents present in file
#       @ name       : name of the document to process.
#       @ tableCount : Specific Table number to edit.
#
#######################

def updateTable(name,tableCount):

    #tell word to open the document
    word.Documents.Open (IP_Directory_Dest + "\\" + name)

    #open it internally
    doc = word.Documents(1)

    # answer to Question # 2 (how to update a specific cell in a TABLE)
    # clearing Table # 1, Row # 1, cell # 1 content
    doc.Tables (1). Rows (1). Cells (1). Range.Text = ''

    #Clearing Table # 1, Row # 1, Col # 4 content to blank
    doc.Tables (1). Cell(1, 4). Range.Text = ''

    # specifically select TABLE # tableCount
    table = doc.Tables(tableCount)
    # count the number of rows in TABLE # 1
    numRows = table.Rows.Count

    # count number of columns
    numCols = table.Columns.Count

    print ('Number of Rows in TABLE',numRows)
    print ('Number of Columns in TABLE',numCols)


    # NOTE : ROW # 1 WILL NOT BE DELETED, AS IT IS COMMON FOR BOTH IP AND IR
    # delete and add the same number of rows
    for row in range(numRows):
        # add a row at the end of table
        doc.Tables(tableCount).Rows.Add ()
        # delete row 2 of table (so as to make the effect of adding rows equal)
        doc.Tables(tableCount).Rows(2).Delete()


    #re-save in IP folder
    doc.SaveAs(IP_Directory_Dest + "\\" + name)

    #close the stream
    doc.Close()

..///

Varun
  • 107
  • 3
  • 10
0

Add empty parentheses at the end of the line:

doc.Tables(1).Rows(1).Delete()

Those parentheses are necessary to call a method.

David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Thanks a lot David. That actually helped in deleting the row 1 of table 1. However, any suggestion on how to clear the contents of table (keeping the table format as it is) and not deleting the row as such. Thanks. – Varun Apr 16 '13 at 06:26
  • @Varun: What happens when you remove that line and just do the line `doc.Tables(1).Cell(1, 1).Delete()`? – David Robinson Apr 16 '13 at 06:29
  • David, it only deletes the cell (1,1) of the table 1. – Varun Apr 16 '13 at 06:35
  • @Varun: Try looping over the cells in that row and deleting each: `for i in range(doc.Tables(1).Columns.Count): doc.Tables(1).Cell(1, i).Delete()` – David Robinson Apr 16 '13 at 06:43
  • @ David: Sure, i will try that and let you know the outcome. Thanks a lot :). – Varun Apr 16 '13 at 06:59
  • @ David: I added a loop to traverse through all the rows of tables, and completed my task. I added a loop something like this (to add a row and delete a row at the same time): ..// # NOTE : ROW # 1 WILL NOT BE DELETED, AS IT IS COMMON FOR BOTH IP AND IR # delete and add the same number of rows for row in range(numRows): # add a row at the end of table doc.Tables(tableCount).Rows.Add () # delete row 2 of table (so as to equal the effect of adding rows) doc.Tables(tableCount).Rows(2).Delete() .../// At last, thanks for all your help. Appreciated. – Varun Apr 16 '13 at 09:14
  • @ David : btw any idea on how to clear the contents of a table cell? i.e if my cell(1,1) has "ABCD" written into it, I want my cell(1,1) to be blank. Doing something like this " doc.Tables(1).Cell(1, 1).Delete() ", will actually delete the cell totally. Any suggestions are welcome. – Varun Apr 18 '13 at 11:18
  • @ David : I guess, I have figured it out. It will be done as follows : ../// # clearing Table # 1, Row # 1, cell # 1 content doc.Tables (1). Rows (1). Cells (1). Range.Text = '' #Clearing Table # 1, Row # 1, Col # 4 content to blank doc.Tables (1). Cell(1, 4). Range.Text = '' ../// – Varun Apr 19 '13 at 06:26