简体   繁体   English

如何避免包含注释的 .docx 文件的 MS-Word 对话框以在保存时暂停 python 执行?

[英]How to avoid MS-Word dialog box of a .docx file containing comments to pause python execution at saving?

Problem:问题:

I need to batch some Word files with python to:我需要用 python 批处理一些 Word 文件:

  1. check if they are .doc files检查它们是否是 .doc 文件
  2. if so change their name如果是这样改变他们的名字
  3. save them as .docx files将它们另存为 .docx 文件

So that I can then extract some info from the tables contained in the document with docx lib.这样我就可以使用 docx lib 从文档中包含的表格中提取一些信息。

I encounter an issue when trying to save docx files containing comments since a popup appears to ask me to confirm if I want to save the file with comments.我在尝试保存包含注释的 docx 文件时遇到问题,因为出现了一个弹出窗口,要求我确认是否要保存带有注释的文件。 It pauses the code execution untill an operator manually confirm by clicking OK into the popup.它会暂停代码执行,直到操作员通过在弹出窗口中单击“确定”来手动确认。 It prevents the code to be run automatically without any operator input.它可以防止代码在没有任何操作员输入的情况下自动运行。

Note: The comments don't need to be kept in the .docx files since I won't use them for further computation.注意:注释不需要保存在 .docx 文件中,因为我不会将它们用于进一步计算。

What I do:我所做的:

Here's the code I have right now, that stops before end of execution untill you confirm in word you accept to keep the comments (in case your doc file contained some):这是我现在拥有的代码,它在执行结束前停止,直到您用文字确认接受保留评论(以防您的文档文件包含一些):

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()

What I've tried:我试过的:

I tried to look for solutions to remove the comments before saving since I do not use them later in the .docx file I created, but I didn't find any satisfying solution.我试图在保存之前寻找删除注释的解决方案,因为我稍后不会在我创建的 .docx 文件中使用它们,但我没有找到任何令人满意的解决方案。

Maybe I'm just using the wrong approach and there's a super simple way to dismiss the dialog box or something, but somehow didn't find it.也许我只是使用了错误的方法,并且有一种超级简单的方法可以关闭对话框或其他东西,但不知何故没有找到它。

Thanks!谢谢!

This seems to do the job, but removes all comments:这似乎完成了这项工作,但删除了所有评论:

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()

I just added the part below that accepts the modifications and remove the comments in original code:我刚刚添加了下面接受修改并删除原始代码中的注释的部分:

    # Accept all revisions
    word.ActiveDocument.Revisions.AcceptAll()
    # Delete all comments
    if word.ActiveDocument.Comments.Count >= 1:
        word.ActiveDocument.DeleteAllComments()

I found the solution here: Python - Using win32com.client to accept all changes in Word Documents我在这里找到了解决方案: Python - Using win32com.client to accept all changes in Word Documents

But it still doesn't fully answer the initial question.但它仍然没有完全回答最初的问题。 Because it just gets rid of comments since in my own situation I don't need them.因为它只是摆脱了评论,因为在我自己的情况下我不需要它们。 But in case you need the comments, I still don't know how to proceed.但是,如果您需要评论,我仍然不知道如何进行。

I stumbled upon this today:我今天偶然发现了这个:

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()

An even easier solution is to use wordconv.exe which is located in your office installation beside the WinWord.exe一个更简单的解决方案是使用 wordconv.exe,它位于您办公室安装中 WinWord.exe 旁边

The commandline is like this:命令行是这样的:

wordconv.exe -oice -nme inputfilePath outputFilePath 

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

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