No answers on stackoverflow <https://stackoverflow.com/questions/53315433/why-do-i-get-error-1804-from-win32print-setjob> so maybe it is a bug I should report here.

I try to change a job name in Win32 spooler using Python 3.6 and win32print on Windows 10. [And I tell the spooler to save all my print jobs, so there are a bunch in there.]

Here is the code I'm using. Note that the variables js and rename are the control variables if you need to tweak this to run in your environment. js contains a substring of an existing document name, and the idea is that the document name of the first matching spool job having a document name containing that fragment will be changed to the content of the rename variable by the code.

import sys, os
import time
import win32print
#####################################
def print_job_lister( js='' ):
    """
    Finds a job whose name contains the passed substring.
    """

    res = None
    for p in win32print.EnumPrinters( win32print.PRINTER_ENUM_LOCAL, None, 1 ):
        flags, desc, name, comment = p
        showprinter = True

        phandle = win32print.OpenPrinter( name )
        print_jobs = win32print.EnumJobs( phandle, 0, -1, 1 )
        for job in print_jobs:
            document = job["pDocument"]
            if js in document:
                if showprinter:
                    print( f'printer => {name}')
                    showprinter = False
                print( f'  Document => {document}')
                print( f'  JobId => {job["JobId"]}  Status => {job["Status"]}'
                       f'  Pages => {job["TotalPages"]}')
                if not res:
                    res = ( name, job["JobId"])
        win32print.ClosePrinter( phandle )
    return res
#####################################
if __name__ == '__main__':
    js = '20181114'
    rename = 'foobar'
    res = print_job_lister( js )
    print( res )
    if res:
        phandle = win32print.OpenPrinter( res[ 0 ])
        jobinfo = win32print.GetJob( phandle, res[ 1 ], 1 )
        jobinfo['Position'] = win32print.JOB_POSITION_UNSPECIFIED
        if rename:
            jobinfo['pDocument'] = rename
            print( f'abc {win32print.JOB_POSITION_UNSPECIFIED} {jobinfo}')
            win32print.SetJob( phandle, res[ 1 ], 1, jobinfo, 0 )
        win32print.ClosePrinter( phandle )

Here is the error I'm getting:

{'JobId': 27, 'pPrinterName': 'HL6180dw', 'pMachineName': '\\\\STEPHEN', 'pUserName': 'Glenn', 'pDocument': 'duh', 'pDatatype': 'NT EMF 1.008', 'pStatus': None, 'Status': 8210, 'Priority': 1, 'Position': 0, 'TotalPages': 22, 'PagesPrinted': 0, 'Submitted': pywintypes.datetime(2018, 11, 14, 21, 1, 27, 882000, tzinfo=TimeZoneInfo('GMT Standard Time', True))}
Traceback (most recent call last):
  File "D:\my\py\spool.py", line 109, in <module>
    win32print.SetJob( phandle, res[ 1 ], 1, jobinfo, 0 )
pywintypes.error: (1804, 'SetJob', 'The specified datatype is invalid.')

_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to