0

Good afternoon everyone. This is my first post on StackOverflow so please be clement in case I miss some information, or break any of the rules.

I'm trying to write a script in Python in the form of a def class, that prints hardcopies for all PDF files in a list (each PDF is a string with its absolute path) and, after each printing job is completed, perform certain commands (waits some seconds and plays a sound).

In other words, something ideally like this:

import win32print
import win32api
import time

# The following is placeholder import of a function I've already created and tested
from view_controller import play_sound

def print_PDFs(files): 
    # <files> is a list of strings. Each string is the absolute path of a single PDF.
    # if instead is passed a string, that is converted into a list with a single string.
    if type(files) is not list:
        files = [files]

    for file in files:
        # Add <file> to the printing queue and start printing

        # When the job is complete, therefore all physical pages are printed,
        play_sound('complete')
        print('Next job in 20 seconds...')
        sleep(20)

Let me give you some context in order to understand what I'm trying to achieve.

This would be a part of a bigger program written in Python, that generates a bunch of PDF files according to given criteria (I won't dig further here, it doesn't matter); then the idea is to play a sound to warn me it has finished, to make the printer wait 20 seconds before passing to the next one so I have time to put aside what I'm doing in the office in that moment and staple what has just come out printed, and finally go on with the next pdf in the list.

I tried many solutions, working with win32print and win32api, ShellExecutable, basically everything there is online and all I could invent myself. But the Windows API seems to me very quirky. This is what I tried: https://www.blog.pythonlibrary.org/2013/12/19/pywin32-monitor-print-queue/ and of course: http://timgolden.me.uk/pywin32-docs/win32print.html

I managed to create a script that prints all files in the lists, and it works fine; what is hard for me is a) to get to know FROM THE PRINTER when it's finished; b) make sure it goes all by itself; sometimes Tim Golden script makes Adobe Acrobat Reader pop up and ask user confirmation... in real life that would mean stopping the program going without me even knowing.

Another thing: it must work on Windows, that's the OS they have in our offices. And printers are network printers, but I guess it won't be hard adapting any script to that playing around with win32api.

Thank you for any suggestion or inspiration that may come, I'm completely forlorn on this!

  • If you want to know when a printer has finished printing, you'll have to research what APIs the printer has. Usually this is SNMP, so look at that first, but larger (MFD) printers often have other proprietary APIs, or web UI's that can be scraped for info. – Nick Westgate Aug 18 '21 at 23:43
  • Wow didn't imagine that would be so difficult to do... – Francesco Anastasio Aug 21 '21 at 16:23

0 Answers0