How print Document in order using python?

465 views Asked by At

I am using win32api python package for printing document. I have two different folders on the basis of print type. Folder 1 for single side print and Folder 2 for double side print. That was obtained by win32api package but the problem is sequential print.

attributes['pDevMode'].Duplex is used for setting printing mode. 1 is for single side and 2 for dobule side. which are eventually folder names.

win32api.ShellExecute(0,'print',file_path,'.','/route',0) printing documents but not in sequence.

So I followed other SOLUTION FROM HERE. Which is giving me this error

shell.ShellExecuteEx(fmask = win32com.shell.shellcon.SEE_MASK_NOASYNC, lpVerb='print', lpFile=file_path, lpParameters=name)
NameError: name 'win32com' is not defined

Code is as follow

import win32api
import win32print
import os
import win32com.shell.shell as shell
# https://stackoverflow.com/questions/47435973/print-pdf-file-in-duplex-mode-via-python
#https://stackoverflow.com/questions/18025882/how-to-determine-if-win32api-shellexecute-was-successful-using-hinstance                              
Base_Path = os.path.dirname(os.path.realpath(__file__))

name = win32print.GetDefaultPrinter()
print("------Base_Path-->",Base_Path,"-----")
print("-----GetDefaultPrinter--->",name,"-----")

for folder in "12":

    src = os.path.join(Base_Path, "admin", folder)
    for file in sorted(os.listdir(src)):
        file_path = os.path.join(src, file)
        print("------",file_path,"-----")
        printdefaults = {"DesiredAccess": win32print.PRINTER_ALL_ACCESS}
        handle = win32print.OpenPrinter(name, printdefaults)
        level = 2
        attributes = win32print.GetPrinter(handle, level)
        attributes['pDevMode'].Duplex
        attributes['pDevMode'].Duplex = int(folder)
        win32print.SetPrinter(handle, level, attributes, 0)
        win32print.GetPrinter(handle, level)['pDevMode'].Duplex
        #win32api.ShellExecute(0,'print',file_path,'.','/route',0)
        shell.ShellExecuteEx(fmask = win32com.shell.shellcon.SEE_MASK_NOASYNC, lpVerb='print', lpFile=file_path, lpParameters=name)


    win32print.ClosePrinter(handle)
    print("-----DONE FOLDEFR-------------")

print("--------DONE PRINTING-------")
0

There are 0 answers