問題:
Python を使用していくつかの Word ファイルをバッチ処理して、次のことを行う必要があります。
.doc ファイルであるかどうかを確認する
もしそうなら名前を変えてください
.docx ファイルとして保存します
これにより、docx lib を使用してドキュメントに含まれるテーブルから情報を抽出できるようになります。
コメントを含む docx ファイルを保存しようとすると、コメント付きでファイルを保存するかどうかを確認するポップアップが表示されるため、問題が発生します。オペレータがポップアップで [OK] をクリックして手動で確認するまで、コードの実行が一時停止されます。
これにより、オペレータの入力なしにコードが自動的に実行されるのを防ぎます。
注: コメントは今後の計算には使用しないため、.docx ファイルに保存する必要はありません。
私がやっていること:
これが私が今持っているコードです。ex の終了前に停止します。コメントを保持することに同意することを Word で確認するまで実行されます (ドキュメント ファイルにコメントが含まれている場合)。
import win32com.client
doc_file = "path\of\document.doc"
docx_file = "path\of\new_document.docx"
word = win32com.client.Dispatch("Word.application")
#get the file extension
file_extension = '.'+doc_file.split('\').pop().split('.').pop()
#test file extension and convert it to docx if original document is a .doc
if file_extension.lower() == '.doc':
wordDoc = word.Documents.Open(doc_file, False, False, False)
wordDoc.SaveAs2(docx_file, FileFormat = 12)
wordDoc.Close()
#test file extension and print a message in the console if not a .doc document
else:
print('Extension of document {0} is not .doc, will not be treated'.format(doc_file))
word.Quit()
私が試したこと:
作成した .docx ファイルではコメントを後で使用しないため、保存する前にコメントを削除する解決策を探しましたが、満足のいく解決策が見つかりませんでした。
もしかしたら私が間違ったアプローチを使っているだけかもしれません。ダイアログ ボックスなどを閉じる非常に簡単な方法があるのですが、どういうわけかそれが見つかりませんでした。
ありがとうございます!
------------------------
これでうまくいくようですが、すべてのコメントが削除されます:
import win32com.client
doc_file = "path\of\document.doc"
docx_file = "path\of\new_document.docx"
word = win32com.client.Dispatch("Word.application")
#get the file extension
file_extension = '.'+doc_file.split('\').pop().split('.').pop()
#test file extension and convert it to docx if original document is a .doc
if file_extension.lower() == '.doc':
wordDoc = word.Documents.Open(doc_file, False, False, False)
# Accept all revisions
word.ActiveDocument.Revisions.AcceptAll()
# Delete all comments
if word.ActiveDocument.Comments.Count >= 1:
word.ActiveDocument.DeleteAllComments()
wordDoc.SaveAs2(docx_file, FileFormat = 12)
wordDoc.Close()
#test file extension and print a message in the console if not a .doc document
else:
print('Extension of document {0} is not .doc, will not be treated'.format(doc_file))
word.Quit()
変更を受け入れて削除する以下の部分を追加しました。元のコードのコメント:
# Accept all revisions
word.ActiveDocument.Revisions.AcceptAll()
# Delete all comments
if word.ActiveDocument.Comments.Count >= 1:
word.ActiveDocument.DeleteAllComments()
ここで解決策を見つけました: Python - win32com.client を使用して Word ドキュメントのすべての変更を受け入れる
しかし、最初の質問にはまだ完全には答えていません。なぜなら、私自身の状況ではコメントが必要ないため、コメントが削除されるだけだからです。しかし、コメントが必要な場合に備えて、どのように進めるべきかまだわかりません。
2
次に、ここで興味深いものを見つけました: stackoverflow.com/questions/21081870/…しかし残念なことに、sを与えたリンクは解決策は壊れており、解決策自体はディスカッション スレッドで報告されません。
– EGI
2020 年 9 月 4 日 9:06
幸いなことに、この内容は次の場所にアーカイブされています: betaarchive.com/wiki/index.php/Microsoft_KB_Archive/259971 とにかく、現時点ではこれをコードに実装する方法がわかりません。
– EGI
2020 年 9 月 4 日 9:30
------------------------
今日、偶然これを発見しました:
import win32com.client
doc_file = "path\of\document.doc"
docx_file = "path\of\new_document.docx"
word = win32com.client.Dispatch("Word.application")
#Disable save with comments warning
word.Options.WarnBeforeSavingPrintingSendingMarkup = False
#get the file extension
file_extension = '.'+doc_file.split('\').pop().split('.').pop()
#test file extension and convert it to docx if original document is a .doc
if file_extension.lower() == '.doc':
wordDoc = word.Documents.Open(doc_file, False, False, False)
wordDoc.SaveAs2(docx_file, FileFormat = 12)
wordDoc.Close()
#test file extension and print a message in the console if not a .doc document
else:
print('Extension of document {0} is not .doc, will not be treated'.format(doc_file))
word.Quit()
さらに簡単な解決策は次のとおりです。wordconv.exe は、オフィスのインストール環境の WinWord.exe の隣にあります。
コマンドラインは次のようになります:
wordconv.exe -oice -nme inputfilePath outputFilePath