这是我对这个问题的扩展问题:
如何使用Python读取MS-Word文件中表格的内容?
@YusuMishi 提供的解决方案很棒,但它没有捕获页眉和页脚中的页眉。
Let me elaborate on that:
使用代码
import win32com.client as win32
import os
word = win32.Dispatch("Word.Application")
word.Visible = 0
p = os.path.abspath("Catch my tables.docx")
word.Documents.Open(p)
doc = word.ActiveDocument
print doc.Tables.Count
我会得到2
打印 (Table 1
and Table 2
)
我如何浏览其中的信息Table 0
and Table N
在这里获取文档
访问页眉和页脚有点棘手。操作方法如下:
HeaderTable = doc.Sections(1).Headers(1).Range.Tables(1)
FooterTable = doc.Sections(1).Footers(1).Range.Tables(1)
您可以通过以下方式获取表计数:
HeaderTablesCount = doc.Sections(1).Headers(1).Range.Tables.Count
FooterTablesCount = doc.Sections(1).Footers(1).Range.Tables.Count
并以这种方式从单元格中获取文本:
HeaderTable.Cell(1,1).Range.Text
FooterTable.Cell(1,1).Range.Text
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)