6

I am trying to open a pdf file, print the file, and close Adobe Acrobat in Python 2.7.

import os

fd = os.startfile("temp.pdf", "print")
os.close(fd)

After running the code, I get the following error on the os.close(fd) line:

TypeError: an integer is required
Mike C.
  • 1,761
  • 2
  • 22
  • 46
  • 11
    `os.startfile` does not open a file, and it does not return a file descriptor that you can then pass to `os.close`. What, exactly, are you trying to do? – Daniel Roseman Jul 17 '18 at 15:35
  • just remove `os.close()` you'll be fine. Just `os.startfile...` – Jean-François Fabre Jul 17 '18 at 15:44
  • For clarification, I want the pdf file to print, then Adobe Acrobat to close. If I only use os.startfile, Acrobat does not close. – Mike C. Jul 17 '18 at 15:53
  • Try using [`subprocess.Popen`](https://docs.python.org/3/library/subprocess.html) using `start` command with with `shell=True`, waiting for enough time, then calling `.terminate()` on the process. – 9000 Jul 17 '18 at 15:59

1 Answers1

13

Here's the solution that I came up with:

    os.startfile("temp.pdf", "print")
    sleep(5)
    for p in psutil.process_iter(): #Close Acrobat after printing the PDF
        if 'AcroRd' in str(p):
            p.kill()
Mike C.
  • 1,761
  • 2
  • 22
  • 46
  • 3
    A bit late, but is there a way to pass arguments here, such as which printer, number of copies, or orientation? – NMALM Nov 24 '20 at 22:00