简体   繁体   English

powershell-MS-Word文档解析-获取表的标题文本

[英]powershell - MS-Word document parsing - Get a table's caption text

I am parsing a word 2007 using powershell. 我正在使用powershell解析一个单词2007。 I would like get each table's caption and print it to screen. 我想获取每个表格的标题并将其打印到屏幕上。

Here is an idea of what I would like to do, but the print caption line does not work. 这是我想做的事情,但是打印标题行不起作用。

$wd = New-Object -ComObject Word.Application
$wd.Visible = $true
$doc = $wd.Documents.Open($filename)
foreach ($table in $doc.Tables)
{
      #print table caption
      Write-Host $table.Caption.Range.Text #This does not work

      #Print Table contents
      foreach ($row in $table.Rows)
      {
          Write-Host $row.Range.Text
      }
}

Is it possible to get the caption associated with a table? 是否可以获取与表格关联的标题?

Captions aren't properties of table objects, they're just text. 标题不是表对象的属性,它们只是文本。 You could do something like this for finding table captions, though: 但是,您可以执行以下操作来查找表格标题:

$style = $doc.Styles | ? { $_.NameLocal -eq 'Caption' }
$wd.Selection.Find.Style = $style
$wd.Selection.Find.Text  = 'Table'  # if table captions are labeled "Table"
$wd.Selection.Find.Execute()

Since wildcard matches are non-greedy in Word, you may have to do something like this: 由于通配符匹配在Word中是非贪婪的,因此您可能必须执行以下操作:

$style = $doc.Styles | ? { $_.NameLocal -eq 'Caption' }
$wd.Selection.Find.Style = $style
$wd.Selection.Find.Wrap  = 0
while ($wd.Selection.Find.Execute()) {
  if ($wd.Selection.Text -like 'Table*') {
    # do stuff
  }
  $wd.Selection.MoveRight()
}

The above matches just by style, so it should select the entire caption. 上面的内容仅按样式进行匹配,因此应选择整个标题。 The MoveRight() unselects the caption afterwards, so the next iteration will find the next match until the end of the document, where the search stops ( .Find.Wrap = 0 ). MoveRight()之后取消选择标题,因此下一次迭代将找到下一个匹配项,直到文档结束,在该处搜索停止( .Find.Wrap = 0 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM