【powershell】MS Word を高速で横断検索する
MS Word 内部テキストの高速抽出 と 正規表現によるマッチ箇所の強調表示 を組み合わせて、.docx
ファイルに対する grep 検索のようなものを作ってみました。
Select-Object
や Where-Object
による柔軟なフィルタリングと組み合わせることでいろいろな活用方法が考えられそうです。
コード
事前準備として、上記記事で紹介した Get-TextOfDocxDocument
と hilight
を $profile
に記入するかドットソースを使うなどして読み込んでおきます。
function Invoke-GrepOnDocx {
<#
.SYNOPSIS
docx に対するGrep
・パイプライン経由での入力にのみ対応
・docx 以外は自動で無視する
.PARAMETER pattern
検索パターン
.PARAMETER case
指定時は大文字小文字を区別
.EXAMPLE
ls | Invoke-GrepOnDocx "ほげ"
#>
param (
[string]$pattern,
[switch]$case
)
$input | Where-Object Extension -eq ".docx" | ForEach-Object {
$fileName = $_.Name
$docxContent = Get-TextOfDocxDocumant -path $_.Fullname
if ($docxContent.Status -eq "FILEOPENED") {
Write-Error ("'{0}' is used by other process!" -f $fileName)
return
}
$grep = $docxContent.Lines | Select-String -Pattern $pattern -AllMatches -CaseSensitive:$case
foreach ($g in $grep) {
"`n{0}:" -f $fileName | Write-Host -ForegroundColor DarkGray
$g.Line | hilight -Pattern $pattern -case:$case -backgroundcolor "Yellow"
}
$total = $grep.Matches.Count
if ($total) {
"========== {0} ==========" -f $total | Write-Host -ForegroundColor Cyan
}
}
}
実行イメージ
Author And Source
この問題について(【powershell】MS Word を高速で横断検索する), 我々は、より多くの情報をここで見つけました https://qiita.com/AWtnb/items/1bb1493bc0e0fac83d95著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .