使用Python读取MS-Word文件的页眉和页脚中的表竞争 [英] Reading Table Contet In Header And Footer In MS-Word File Using Python

查看:478
本文介绍了使用Python读取MS-Word文件的页眉和页脚中的表竞争的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对该问题的扩展问题:

This is my extended question for the question:

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

@YusuMishi提供的解决方案很棒,但是它无法在页眉和页脚中捕获页眉.

The solution provided by @YusuMishi is great, but it does not catch the headers in the header and footer.

让我详细说明一下:

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 1Table 2)

如何浏览Table 0Table N

在此处获取文档

推荐答案

访问页眉和页脚有点棘手.这是操作方法:

Accessing Headers and Footers is a bit tricky. Here is how to do it:

HeaderTable = doc.Sections(1).Headers(1).Range.Tables(1)
FooterTable = doc.Sections(1).Footers(1).Range.Tables(1)

您可以通过以下方式获取表计数:

You can get the table count this way:

HeaderTablesCount = doc.Sections(1).Headers(1).Range.Tables.Count
FooterTablesCount = doc.Sections(1).Footers(1).Range.Tables.Count

以这种方式从单元格中获取文本:

And get the text from cells this way:

HeaderTable.Cell(1,1).Range.Text
FooterTable.Cell(1,1).Range.Text

这篇关于使用Python读取MS-Word文件的页眉和页脚中的表竞争的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆