pythonms-wordcomwin32comoffice-automation

Programmatically click the ok button in ms-word


I am trying to automatically convert pdf files to docx files in python, using (copied from this):

import win32com.client
word = win32com.client.Dispatch("Word.Application")
word.visible = 1
pdfdoc = 'NewDoc.pdf'
todocx = 'NewDoc.docx'
wb1 = word.Documents.Open(pdfdoc)
wb1.SaveAs(todocx, FileFormat=16)  # file format for docx
wb1.Close()
word.Quit()

This opens Word and the following prompt appears:

enter image description here

I want to run my script without user intervention, so is it possible to programmatically click the OK button?


Solution

  • The Open method has optional parameters that can be specified to avoid the dialog:

    wb1 = word.Documents.Open(pdfdoc, false)
    

    The ConfirmConversions parameter is set to true to display the Convert File dialog box if the file isn't in Microsoft Word format, otherwise just set it to false.