pythonwindowspdfpywin32file-conversion

How to suppress popup dialog box when converting pdf to docx using pywin32


I'm running a python script on a windows laptop to convert some sample pdf files to docx. However, for each file, a dialog box pops up that prompts me to click OK when the script tries to convert said file to docx. Any idea how I might suppress this popup or configure the script so that no click action is needed from the user?

Code:

import win32com.client

#open win32 client in hidden mode

word = win32com.client.Dispatch("Word.Application")
word.Visible = False
word.DisplayAlerts = False #tried setting this to False and 0 to suppress dialog. didn't work

pdf_path = r"C:\path\to\pdf"
doc_path = r"C:\path\to\docx"

#open pdf doc with client
pdf_doc = word.Documents.Open(pdf_path)

#convert pdf to docx
pdf_doc.SaveAs2(doc_path, FileFormat=16) #16 is the file format for docx. tried running this with pdf_doc.SaveAs. didn't work.

pdf_doc.Close() #also tried running this as pdf_doc.Close(False). didn't work

word.Quit()

Dialog popup


Solution

  • Changing

    pdf_doc = word.Documents.Open(pdf_path)
    

    to

    pdf_doc = word.Documents.Open(pdf_path, False, False, False)
    

    fixed the issue for me.