質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Word

Microsoft WordはMicrosoftが開発した業務用の文書生成用のソフトウェアです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

6647閲覧

wordのdocxファイルからコメントとドキュメントの対を取得したい。

hiroto0227

総合スコア14

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Word

Microsoft WordはMicrosoftが開発した業務用の文書生成用のソフトウェアです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2018/01/23 02:27

wordファイルのdocxからドキュメントとコメントを取得したいです。wordというのは複数のxmlファイルをzipしたものであるのですが、どんな構成になっていて、どのパラメータがどれなのか分かりません。

今回はxml解析にはpythonを使用します。
docxを解凍して、comments.xmlにコメントが、document.xmlに本文が書かれていると調べて見つかりました。しかし、comments.xmlのコメント文とdocument.xmlの本文がお互いにどこと対応しているのかをみつけられません。xmlパーサのetreeを用いてgetiterator()関数で回したところ、テキストがぐちゃぐちゃになってしまうので、よくわからなくなってしまいました。

もし、コメントと本文を取る方法を知っていたら、教えてください。

以下は今まで行ったコードの例です。

python

1from lxml import etree 2import zipfile 3 4docxZip = zipfile.ZipFile(file_path) 5commentsXML = docxZip.read('word/comments.xml') #zipファイルを解凍しcomments.xmlを読み込む 6et = etree.XML(commentsXML) 7 8for i, e in enumerate(et.getiterator()): 9 if e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}id'): 10 comment['comment_id'] = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}id') 11 if e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}author'): 12 comment['author'] = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}author') 13 if e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}date'): 14 comment['date'] = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}date') 15 if e.attrib.get('{http://schemas.microsoft.com/office/word/2010/wordml}paraId'): 16 comment['para_id'] = e.attrib.get('{http://schemas.microsoft.com/office/word/2010/wordml}paraId') 17 if e.attrib.get('{http://schemas.microsoft.com/office/word/2010/wordml}textId'): 18 comment['text_id'] = e.attrib.get('{http://schemas.microsoft.com/office/word/2010/wordml}textId') 19 if e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidR'): 20 comment['rs_id'] = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidR') 21 22documentsXML = docxZip.read('word/document.xml') #zipファイルを解答しdocument.xmlを読み込む 23et = etree.XML(documentsXML) 24 if e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}id'): 25 document['document_id'] = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}id') 26 if e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}author'): 27 document['author'] = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}author') 28 if e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}date'): 29 document['date'] = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}date') 30 if e.attrib.get('{http://schemas.microsoft.com/office/word/2010/wordml}paraId'): 31 document['para_id'] = e.attrib.get('{http://schemas.microsoft.com/office/word/2010/wordml}paraId') 32 if e.attrib.get('{http://schemas.microsoft.com/office/word/2010/wordml}textId'): 33 document['text_id'] = e.attrib.get('{http://schemas.microsoft.com/office/word/2010/wordml}textId') 34 if e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidR'): 35 document['rs_id'] = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidR')

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

wakame

2018/01/23 09:50

特にこだわりがないのであれば外部ライブラリを使う方がいいのかなと思います。
guest

回答1

0

コメントの取得についてはそれらしい記述を見つけました。
ただ手元の環境で動作確認できず動作確認していないので参考程度に。
Extract DOCX Comments

投稿2018/01/23 10:59

wakame

総合スコア1170

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問