【问题标题】:Reading Table Contet In Header And Footer In MS-Word File Using Python使用 Python 在 MS-Word 文件的页眉和页脚中读取表格内容
【发布时间】:2013-05-11 01:18:13
【问题描述】:

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

How to read contents of an Table in MS-Word file Using Python?

@YusuMishi 提供的解决方案很棒,但是在页眉和页脚中没有捕捉到页眉。

让我详细说明一下:

使用代码

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

我将打印2Table 1Table 2

如何查看Table 0Table N中的信息

Get the document here

【问题讨论】:

    标签: python python-2.7 ms-word pywin32 win32com


    【解决方案1】:

    访问页眉和页脚有点棘手。操作方法如下:

    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
    

    【讨论】:

    【解决方案2】:

    不幸的是,我不是 程序员,但对 MS-Word VBA 和对象层次结构有所了解。这将是太多的文字要放在评论中(我宁愿把这个提示放在哪里)。

    如果您搜索您的表,您必须分析不同的Document.StoryRanges 才能找到您的表。有FootersHeaders,但它们又分为不同的类型。因此,要找到您的表 0,您可以使用以下结构:

    This is VBA code!! I hope you could adjust to your needs. And do it separately for you footers.

    doc.StoryRanges(wdEvenPagesHeaderStory).Tables.Count
    doc.StoryRanges(wdFirstPageHeaderStory).Tables.Count
    doc.StoryRanges(wdPrimaryHeaderStory).Tables.Count
    

    【讨论】:

      猜你喜欢
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2017-09-20
      • 1970-01-01
      • 2015-11-03
      相关资源
      最近更新 更多