您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何使用Python读取MS-Word文件中表的内容?

如何使用Python读取MS-Word文件中表的内容?

这是在Python 2.7中对我有效的方法

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

希望这会有所帮助。

一个根据标题返回Column index的函数示例:

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
python 2022/1/1 18:35:45 有235人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶