import win32com.client as win32
word = win32.Dispatch("Word.Application")
word.Visible = 0
word.Documents.Open("MyDocument")
doc = word.ActiveDocument
要查看您的文档有多少张表:
doc.Tables.Count
然后,您可以通过索引选择所需的表。请注意,与python不同,COM索引从1开始:
table = doc.Tables(1)
要选择一个单元格:
table.Cell(Row = 1, Column= 1)
table.Cell(Row =1, Column =1).Range.Text
希望这会有所帮助。
def Column_index(header_text):
for i in range(1 , table.Columns.Count+1):
if table.Cell(Row = 1,Column = i).Range.Text == header_text:
return i
那么您可以通过这种方式访问??所需的单元格,例如:
table.Cell(Row =1, Column = Column_index("The Column Header") ).Range.Text