Problem:
I need to batch some Word files with python to:
- check if they are .doc files
- if so change their name
- save them as .docx files
So that I can then extract some info from the tables contained in the document with 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. 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.
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.
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!