LoginSignup
2
5

More than 5 years have passed since last update.

Python(pywin32)でWordを操作する[10] - Word文書をgrepする(ページ数表示, 行数表示, ワイルドカード展開)

Last updated at Posted at 2018-06-28

概要

前回のdocgrep.pyに以下を追加します。

  • ワイルドカード展開
  • ページ数、行数の表示

使い方

python docgrep.py [-o] <expr> files... 
引数 省略可否 説明
-o 省略可能 ヒットしたWord文書を表示する。省略した場合はprint出力。
<expr> 必須 検索文字列(正規表現)
files... 必須 ファイル名(ワイルドカード指定可能(e.g.*.docx))

出力イメージ

> python docgrep.py "test.*" *.docx
file: C:\xxxx\yyy.docx
page=6, line=56 : testA
...
file: C:\xxxx\zzz.docx
page=10, line=44 : testB
...

コード

docgrep.py
# -*- coding: utf-8 -*-
import argparse
import pathlib
import glob

import win32com.client
import re

def docgrep(expr="", files=[], args_o=False):

    pattern = re.compile(expr)
    wd=win32com.client.DispatchEx("Word.Application")

    quit =True
    for doc in files:
        print(f"file: {doc}")
        found = False
        wdoc = wd.Documents.Open(FileName=doc, ConfirmConversions=None, ReadOnly=True)
        wdoc.ActiveWindow.View.Type=3   # wdPrintView 3

        for sentence in wdoc.Sentences:
            txt = sentence.Text
            m = pattern.search(txt)
            if m:
                found = True
                quit = False
                if not args_o:
                    page = sentence.Information(1) # wdActiveEndAdjustedPageNumber 1 or wdActiveEndPageNumber 3
                    line = sentence.Information(10)  # wdFirstCharacterLineNumber 10
                    print(f"page={page}, line={line} : {txt}")
                else:
                    sentence.Select()
                    break
        if not found or not args_o:
            wdoc.Close(SaveChanges=0)

    if quit or not args_o:
        wd.Quit()
    else:
        wd.Visible = True

if __name__ == "__main__":
    parser = argparse.ArgumentParser(usage="%(prog)s [-o] <expr> files...")
    parser.add_argument("-o", action="store_true")
    parser.add_argument("expr",  type=str)
    parser.add_argument("files", type=str, nargs="*")

    args = parser.parse_args()

    fset=set()
    for f in args.files:
        glob_result = glob.glob(f)
        for f in glob_result:
            fset.add(str(pathlib.Path(f).resolve()))

    docgrep(expr=args.expr, files=list(fset), args_o=args.o)

前回と今回利用したWordオブジェクト

*別途記載

関連

Python(pywin32)でWordを操作する[1] - Wordオブジェクトモデル
Python(pywin32)でWordを操作する[2] - Wordを起動/終了する
Python(pywin32)でWordを操作する[3] - 新規ドキュメント作成
Python(pywin32)でWordを操作する[4] - 文字列を入力/取得/削除する
Python(pywin32)でWordを操作する[5] - ドキュメントをファイルに保存する、Wordのオプション変更
Python(pywin32)でWordを操作する[6] - 特定のタイトルが付いているウィンドウの操作
Python(pywin32)でWordを操作する[7] - 既存文書を開く/閉じる(Documents.Open(), Document.Close())
Python(pywin32)でWordを操作する[8] - 段落単位の文字列取得, 統計(ページ数, 段落数,etc)取得
Python(pywin32)でWordを操作する[9] - Word文書をgrepする(docgrep.rb移植)
Python(pywin32)でWordを操作する[10] - Word文書をgrepする(ページ数表示, 行数表示, ワイルドカード展開)

参考

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5