[英]Python - Unable to Edit Table Contents in Microsoft Word using win32com
我正在嘗試通過使用python和win32com客戶端組件刪除其內容(第一行內容除外)來更新Microsoft Word-2010表。 我什至查看了MSDN庫( http://msdn.microsoft.com/zh-cn/library/bb244515.aspx ),發現Delete可以幫助我解決此類問題。 (還看看@ 如何使用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
..///
但是在執行上述步驟時,不會刪除表行(也不會刪除單元格[1,1])。 有什么我想念的嗎? 歡迎任何建議。
用於清除表格內容的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()
... ///
請忽略注釋掉的部分(我也在努力使這些東西起作用)
所有,
這就是我所想的。 我將分享為修復它而編寫的代碼。
在此過程中,我學習了如何清除表(特定行和列)的內容,如何添加行,獲取單詞表中的行數等。 我還弄清楚,由於用於python / win32的API上沒有太多可用的文檔(MSDN庫除外),所以我認為習慣於這些API的一種方法是理解VB代碼(主要是目前的@ MSDN http://msdn.microsoft.com/zh-cn/library/bb244515.aspx ),並嘗試為python-win32創建相應的類似代碼。 多數民眾贊成在我的理解。
..///
########################
#
# 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()
..///
在行末添加空括號:
doc.Tables(1).Rows(1).Delete()
這些括號是調用方法所必需的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.