简体   繁体   English

Python-无法使用Win32com在Microsoft Word中编辑表内容

[英]Python - Unable to Edit Table Contents in Microsoft Word using win32com

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. 我正在尝试通过使用python和win32com客户端组件删除其内容(第一行内容除外)来更新Microsoft Word-2010表。 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. 我什至查看了MSDN库( http://msdn.microsoft.com/zh-cn/library/bb244515.aspx ),发现Delete可以帮助我解决此类问题。 (Also had a look @ How to read contents of an Table in MS-Word file Using Python? ) (还看看@ 如何使用Python读取MS-Word文件中表的内容?

../// ..///

# 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). 但是在执行上述步骤时,不会删除表行(也不会删除单元格[1,1])。 Is there something that I'm missing ? 有什么我想念的吗? Any suggestions are welcome. 欢迎任何建议。

Python function for clearing the table contents is pasted herein with 用于清除表格内容的Python函数粘贴在此处

..// ..//

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) 请忽略注释掉的部分(我也在努力使这些东西起作用)

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. 我还弄清楚,由于用于python / win32的API上没有太多可用的文档(MSDN库除外),所以我认为习惯于这些API的一种方法是理解VB代码(主要是目前的@ MSDN http://msdn.microsoft.com/zh-cn/library/bb244515.aspx ),并尝试为python-win32创建相应的类似代码。 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()

../// ..///

Add empty parentheses at the end of the line: 在行末添加空括号:

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

Those parentheses are necessary to call a method. 这些括号是调用方法所必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM