John0211
John0211

Reputation: 1

Fetch printer status using python on windows

I want to fetch the printer status connected to a computer (Windows) like door open/cover open, paper jam, no toner, etc. using python in realtime. I have a use case where I need to fetch these details and display on the screen.

I tried below code but it did not work in real time. It only fetches statuses like Paused, Idle and Offline. I also want to fetch all other statuses like cover open/door open , no toner, etc.

def GetPrinterStatus(self, printerName):
        pythoncom.CoInitialize()
        status = ""
        try:
            wmi = win32com.client.Dispatch("WbemScripting.SWbemLocator")
            service = wmi.ConnectServer(".", "root\\cimv2")
            printers = service.ExecQuery(f"SELECT * FROM Win32_Printer WHERE Name = '{printerName}'")
            for printer in printers:
                if printer.PrinterState == 0 or printer.PrinterState == 0:
                    status = status + "IDLE"
                elif printer.PrinterState == 1 or printer.PrinterState == 1:
                    status = status + "PAUSED"
                elif printer.PrinterState == 2 or printer.PrinterState == 2:
                    status = status + "Printer Error"
                elif printer.PrinterState == 4 or printer.PrinterState == 4:
                    status = status + "Pending Deletion"
                elif printer.PrinterState == 8 or printer.PrinterState == 8:
                    status = status + "Paper Jam"
                elif printer.PrinterState == 16 or printer.PrinterState == 10:
                    status = status + "Paper Out"
                elif printer.PrinterState == 32 or printer.PrinterState == 20:
                    status = status + "Manual Feed"
                elif printer.PrinterState == 64 or printer.PrinterState == 40:
                    status = status + "Page Problem"
                elif printer.PrinterState == 128 or printer.PrinterState == 80:
                    status = status + "OFFLINE"
                elif printer.PrinterState == 256 or printer.PrinterState == 100:
                    status = status + "I/O Active"
                elif printer.PrinterState == 512 or printer.PrinterState == 200:
                    status = status + "BUSY"
                elif printer.PrinterState == 1024 or printer.PrinterState == 400:
                    status = status + "PRINTING"
                elif printer.PrinterState == 2048 or printer.PrinterState == 800:
                    status = status + "Output Bin Full"
                elif printer.PrinterState == 4096 or printer.PrinterState == 1000:
                    status = status + "Not Available"
                elif printer.PrinterState == 8192 or printer.PrinterState == 2000:
                    status = status + "WAITING"
                elif printer.PrinterState == 16384 or printer.PrinterState == 4000:
                    status = status + "PROCESSING"
                elif printer.PrinterState == 32768 or printer.PrinterState == 8000:
                    status = status + "Initializing"
                elif printer.PrinterState == 65536 or printer.PrinterState == 10000:
                    status = status + "Warming Up"
                elif printer.PrinterState == 131072 or printer.PrinterState == 20000:
                    status = status + "Toner Low"
                elif printer.PrinterState == 262144 or printer.PrinterState == 40000:
                    status = status + "No Toner"
                elif printer.PrinterState == 524288 or printer.PrinterState == 80000:
                    status = status + "Page Punt"
                elif printer.PrinterState == 1048576 or printer.PrinterState == 100000:
                    status = status + "User Intervention"
                elif printer.PrinterState == 2097152 or printer.PrinterState == 200000:
                    status = status + "Out of Memory"
                elif printer.PrinterState == 4194304 or printer.PrinterState == 400000:
                    status = status + "Printer Door Open"
                elif printer.PrinterState == 8388608 or printer.PrinterState == 800000:
                    status = status + "Unknown Server"
                elif printer.PrinterState == 6777216 or printer.PrinterState == 1000000:
                    status = status + "Power Save"
        finally:
            pythoncom.CoUninitialize()
            return status

Upvotes: 0

Views: 35

Answers (0)

Related Questions