I have spent the past few days trying to automate the process of flattening a PDF and this function is the one I have found works best. The only issue is that the a.kill() command is not being read, resulting in the browser remaining open after the function is complete. How can I close the browser window after the save process is finished?
import time
import subprocess
import keyboard
def chrome():
test = "test"
name = "file:///C://Users//akcgo//Documents//CARB//ARBER//PDFS//Company A.pdf"
a = subprocess.Popen("C://Program Files (x86)//Google//Chrome//Application//chrome.exe")
time.sleep(1)
keyboard.write(name)
time.sleep(1)
keyboard.press_and_release('enter')
time.sleep(1)
keyboard.press_and_release(['ctrl', 'p'])
time.sleep(1)
keyboard.press_and_release('enter')
time.sleep(1)
keyboard.write(test)
time.sleep(.5)
keyboard.press_and_release('enter')
time.sleep(2)
a.kill()
# keyboard.press_and_release(['ctrl', 'w']) doesn't work either
You could get the subprocess' PID by using pid = a.pid
, then calling os.kill(pid, signal.SIGTERM)
or os.kill(pid, signal.SIGKILL)
.