Search code examples
pythonpdfacrobatpywinautohebrew

Open and save a PDF file using Acrobat Reader in the background using Python


Following the Opening pdf file question

I am looking for a way to also command Adobe Acrobat Reader to save the file programmatically using Python.

I am not looking for the pikepdf way of saving the file.

Reason: This PDF file, created with fill-pdf, needs to go through special formatting done by Acrobat Reader upon opening. Upon exit Acrobat Reader asks whether to save the formatting it did, I need this "Yes, Save" to be via code.

Edit: How to proceed from here using pywinauto?

import time
from pywinauto.application import Application

pdf_file = r'C:\Path\To\Total.pdf'
acrobat_path = r"C:\Path\To\Acrobat.exe"

app = Application(backend=u'uia').start(cmd_line = acrobat_path + ' ' + pdf_file)
print("started")
time.sleep(1)
app = Application(backend=u'uia').connect(path=acrobat_path)
print("connected")

Solution

  • solution with pyautogui:

    import os
    os.startfile(path)
    filename = os.path.basename(path)
    focus = pg.getWindowsWithTitle(filename)
    while len(focus) == 0:
        focus = pg.getWindowsWithTitle(filename)
    focus [0].show() # show() for python3.9, activate() for python3.7
    time.sleep(1)
    pg.hotkey('ctrl', 's')
    time.sleep(1)
    pg.hotkey('ctrl', 'q')
    print("Blessed Be God")