gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:01:09 24 4
gpt4 key购买 nike

我正在尝试使用 python 和 win32com 客户端组件删除其内容(第一行内容除外)来更新 microsoft word - 2010 表。我什至查看了 MSDN 库 ( http://msdn.microsoft.com/en-us/library/bb244515.aspx ),发现删除可以在这方面帮助我,类似这样。(也看过@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

..///

但是执行上述步骤后,表格行不会被删除(单元格 [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/en-us/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()

..///

关于Python - 无法使用 win32com 在 Microsoft Word 中编辑表格内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16029904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com